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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.