in reply to sorting CSV files

Am I the only one this reeks of homework to? In general, people are happy to help if you at least start with some code and specific questions about it (and why it's not working).

Maybe you can turn in the following one liner (for Windows)...?

C:\temp>perl -F, -ane "BEGIN{$c = shift} push @a, [@F]; END{print join +(',' => @$_) for sort {($a->[abs($c)-1] cmp $b->[abs($c)-1]) * $c} (@ +a)};" -- -3 grades.csv Jim,72,IL Rob,63,FL Mike,67,CA Chan,32,AZ
... where the only argument (before the input file) specifies on which column to sort and in which direction (negative column value for descending sort, positive for ascending).

Replies are listed 'Best First'.
Re^2: sorting CSV files
by Anonymous Monk on Apr 28, 2008 at 07:19 UTC
    Hi, Thanks for wonderful one liner file sorting. Can you please explain this in detail that how it works ?

      I can, but I won't. The OP looked like a homework assignment. My goal was to post a working solution that was as cryptic, compressed, unreadable and definitely un-turn-in-able as possible.

      If you are genuinely interested, I would recommend reading:

      • perlrun for an explanation of -a -F, and -n.
      • perlmod to understand what BEGIN and END blocks do.
      • perlfunc for an explanation of the sort function.

        Thanks. :)