mbm has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, Let me be clear with my question this time. I am new to perl and still + managed to code many things in it. That shows how simple yet effecti +ve language perl is! I have a csv file with following contents. "Add workflow, Delete workflow",resource1,Sat 04/25/09,manager1 "Updateworkflow, Delete workflow",resource2,Wed 04/15/09,manager2 "Retrieve workflow, Delete workflow",resource3,Sat 04/25/09,manager3 "Add workflow, Delete workflow",resource4,Wed 03/25/09,manager4 "Add workflow",resource5,Tue 04/14/09,manager5 I need this csv file to be sorted and the sorted file should be like t +his: "Add workflow, Delete workflow",resource4,Wed 03/25/09,manager4 "Add workflow",resource5,Tue 04/14/09,manager5 "Updateworkflow, Delete workflow",resource2,Wed 04/15/09,manager2 "Add workflow, Delete workflow",resource1,Sat 04/25/09,manager1 "Retrieve workflow, Delete workflow",resource3,Sat 04/25/09,manager3 Can anyone help me accomplish this without using any modules? If it ca +n't be done without using any modules, then pls let me know which mod +ule to use and how do we install it? I have perl 5.6.0 in my HP box. ~Manjunath Alcatel-Lucent

Replies are listed 'Best First'.
Re: sort csv file on "date" column
by Corion (Patriarch) on Apr 25, 2009 at 07:33 UTC
Re: sort csv file on "date" column
by jwkrahn (Abbot) on Apr 25, 2009 at 07:47 UTC
    $ echo '"Add workflow, Delete workflow",resource1,Sat 04/25/09,manager +1 "Updateworkflow, Delete workflow",resource2,Wed 04/15/09,manager2 "Retrieve workflow, Delete workflow",resource3,Sat 04/25/09,manager3 "Add workflow, Delete workflow",resource4,Wed 03/25/09,manager4 "Add workflow",resource5,Tue 04/14/09,manager5' | \ perl -e' print for map substr( $_, 6 ), sort map join( "", ( m<,\w{3} (\d\d)/(\d\d)/(\d\d),> )[ 2,0,1 ] +, $_ ), <> ' "Add workflow, Delete workflow",resource4,Wed 03/25/09,manager4 "Add workflow",resource5,Tue 04/14/09,manager5 "Updateworkflow, Delete workflow",resource2,Wed 04/15/09,manager2 "Add workflow, Delete workflow",resource1,Sat 04/25/09,manager1 "Retrieve workflow, Delete workflow",resource3,Sat 04/25/09,manager3
      Hi Jwkrahn, thanks a lot. The code you proposed looks so simple and doing the wor +k so nicely. Thanks again. Can you please describe the logic here if you dont mind? Regards, Manjunath Alcatel-Lucent

        The logic is to rearrange the date field (for example: 03/25/09) from month-day-year to year-month-day so it can be sorted by the built-in sort function without modification.   The algorithm employed is the GRT (Guttman-Rosler Transform) which prepends the reformatted date to the current record, sorts the current record, and then removes the reformatted date from the beginning of the current record.