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

Could someone suggest a perl equiv. to the UNIX sort command: sort -t ","  -k 4,6 File.txt Sorting ON: only the 4th to 6th field, but sorting: the whole line.

Replies are listed 'Best First'.
Re: Sort on Part of a Line
by AR (Friar) on Jan 04, 2011 at 16:10 UTC
    sorting is so rich in Perl. Perhaps you'd like to read the handful of sorting tutorials that our fellow monks have written.
Re: Sort on Part of a Line
by rovf (Priest) on Jan 04, 2011 at 16:04 UTC
    See sort

    Inside the code block, you would use split to split your string into fields, and then compare the fields you need.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Sort on Part of a Line
by salva (Canon) on Jan 04, 2011 at 16:12 UTC
    use Sort::Key::Multi qw(s3_keysort); # s3 => three keys of type string my @lines = <>; my @sorted = s3_keysort { (split /,/)[3,4,5] } @lines;
      Thank You
Re: Sort on Part of a Line
by cdarke (Prior) on Jan 04, 2011 at 16:22 UTC
    Like this?
    #sort -t "," -k 4,6 File.txt sub custom { my ($a4,$a6) = (split(',',$a))[4,6]; my ($b4,$b6) = (split(',',$b))[4,6]; return ($a4 == $b4)?$a4 <=> $b4:$a6 <=> $b6 } # open(my $fh, File.txt) or die "Unable to open File.txt: $!"; # DATA used for convenience my @result = sort custom <DATA>; print @result,"\n"; __DATA__ 11,22,33,44,55,66,77,88,99 9,1,2,3,4,5,6,7 2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8 0,0,0,0,0,0,0,0 1,2,3,5,5,6,7,8 1,2,3,4,5,7,7,8
    Produces:
    0,0,0,0,0,0,0,0 9,1,2,3,4,5,6,7 1,2,3,4,5,6,7,8 1,2,3,5,5,6,7,8 1,2,3,4,5,7,7,8 2,3,4,5,6,7,8,9 11,22,33,44,55,66,77,88,99
      This is great. Just had to change from Numeric to String compare. Thanx Much
      return ($a3 eq $b3)?$a3 cmp $b3:$a5 cmp $b5
      This seems close, but not quite. Here is a sample of the data I wish to sort by field 4 & 6 ( from 0, that is 3 & 5 )
      132,"Samayoa, Blanca",Blanca,,Samayoa,3/8/1996,,Yes,Canada 136,"McIntosh, John",John,,McIntosh,3/18/1996,,Yes,Canada 137,"McGrath, Bryan D",Bryan,D,McGrath,3/25/1996,,Yes,Canada 138,"Katba-Roth, Nevine",Nevine,,Katba-Roth,3/25/1996,,Yes,Canada 140,"+Hogeboom, Alan A",Alan A,,Hogeboom,4/15/1996,9/27/2010,No,Canada 144,"Cheung, Kitty",Kitty,,Cheung,5/13/1996,,Yes,Canada 146,"+Jativa, Kimberly",Kimberly,,Jativa,7/23/2007,3/23/2006,No,Canada 154,"Kamani, Azmina",Azmina,,Kamani,6/3/1996,,Yes,Canada 157,"Dello Iacono, Gina",Gina,,Dello Iacono,6/5/1996,,Yes,Canada 158,"Kwan, Julie",Julie,,Kwan,6/10/1996,,Yes,Canada 164,"Liu, Chiung S",Chiung,Sui,Liu,7/2/1996,,Yes,Canada 167,"D'Souza, Natasha",Natasha,,D'Souza,8/19/1996,,Yes,Canada 170,"Parise, Benedetta",Benedetta,,Parise,8/23/1996,,Yes,Canada 171,"Gosse, Brenda",Brenda,,Gosse,9/20/1999,,Yes,Canada 171199,"+Neskovic, Veljko",Veljko,,Neskovic,7/23/2007,8/21/2004,No,Can +ada 180,"+Prisciak, Damian",Damian,,Prisciak,7/23/2007,9/3/2002,No,Canada 192,"DeLazzari, Regina A",Regina,A,DeLazzari,12/2/1996,,Yes,Canada 193,"Dote, Merlinda",Merlinda,,Dote,12/2/1996,,Yes,Canada 1932,"+Raju, Sailaja",Sailaja,,Raju,7/23/2007,2/26/2005,No,Canada 194,"+Ing, Kent",Kent,,Ing,3/3/1998,11/15/2009,No,Canada
Re: Sort on Part of a Line
by johna (Monk) on Jan 04, 2011 at 17:02 UTC
    Depending on the size of your data set, using a Schwartzian Transform might make things more efficient...