NorthernFox has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help Sorting a CSV File
by hippo (Archbishop) on Oct 04, 2019 at 14:50 UTC | |
by AnomalousMonk (Archbishop) on Oct 04, 2019 at 15:44 UTC | |
|
Re: Help Sorting a CSV File
by clueless newbie (Curate) on Oct 04, 2019 at 15:09 UTC | |
by Tanktalus (Canon) on Oct 06, 2019 at 23:42 UTC | |
by erix (Prior) on Oct 07, 2019 at 14:20 UTC | |
|
Re: Help Sorting a CSV File
by 1nickt (Canon) on Oct 04, 2019 at 14:40 UTC | |
|
Re: Help Sorting a CSV File
by AnomalousMonk (Archbishop) on Oct 04, 2019 at 17:09 UTC | |
|
Re: Help Sorting a CSV File
by dbuckhal (Chaplain) on Oct 05, 2019 at 05:16 UTC | |
|
Re: Help Sorting a CSV File
by erix (Prior) on Oct 04, 2019 at 20:28 UTC | |
|
Re: Help Sorting a CSV File
by dasgar (Priest) on Oct 09, 2019 at 14:47 UTC |