Hi, I'm hoping someone can help me, I've never used Perl before and I am more used to SQL and MS Access, I've just inherited a system that can incorporate Perl scripts to enable sorting of data etc. I have a CSV file that I have created from another format of file and it looks something like the following.
CAT_HEADER,SUPPLIER_CODE,CUSTOMER_CODE
CAT_LINE,0001P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0002P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0001P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0001P,ABC34567,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC34567,20190924,,1,Z,2.24
The 0001P and 0002P are price bands and the ABC123... etc are product codes.
I want to sort the so that all of the price band lines are grouped together.
So what I want to end up with is:
CAT_HEADER,SUPPLIER_CODE,CUSTOMER_CODE
CAT_LINE,0001P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0001P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0001P,ABC34567,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0002P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC34567,20190924,,1,Z,2.24
I have the following basic script that can re-sort based on the column number in the CSV file which is:
=======================================================================================================
use strict;
use warnings;
my $fSource;
my $fDest;
if (! open($fSource, "<", $ARGV[0])) {
printf("Unable to open file %s to read", $ARGV[0]);
exit(1);
}
if (! open($fDest, ">", $ARGV1)) {
printf("Unable to open file %s to write\n", $ARGV1);
exit(1);
}
# Sort the file
print("Sorting data by price band\n");
print $fDest sort { (split ',', $a)1 cmp (split ',', $b)1 } <$fSource>;
print("Sort complete\n");
close($fSource);
close($fDest);
=======================================================================================================
This almost works but it includes my first record so what I end up with is:
CAT_LINE,0001P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0001P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0001P,ABC34567,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC12345,20190924,,1,Z,3.36
CAT_LINE,0002P,ABC23456,20190924,,1,Z,2.24
CAT_LINE,0002P,ABC34567,20190924,,1,Z,2.24
CAT_HEADER,SUPPLIER_CODE,CUSTOMER_CODE
I need the header record to always be the first, so essentially I need my script to ignore/skip the first record.
Any help would be appreciated - thank you.
In reply to Help Sorting a CSV File by NorthernFox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |