Chas. Owens wrote:
On Mon, Jun 15, 2009 at 11:28, Aimee Cardenas<aim...@sfbrgenetics.org> wrote:

I have a file with two important columns in it separated by spaces.  I'll
call these col1 and col2.  I need to sort the data by col2 and then print
col1 & col2 to a file.  I know I CAN do it with perl, but which way is
faster for this kind of processing?  Perl or Awk?  I know with awk, I can
find a one-liner for the command line and I could probably do that with
perl, too (I'm just now refreshing myself on perl command line options), but
I just thought I'd get some experienced opinions on the best way to do it.

Thanks in advance.  May your sky be filled with perly clouds!  ;-)

Aimee

Sounds like a job for a Schwartzian Transform[1]:

lexical sort
perl -e 'print map { join(" ", @$_), "\n" } sort { $a->[0] cmp $b->[0]
} map { [split] } <>' filename

The OP said they want to sort "by col2" so that should be sort { $a->[1] cmp $b->[1] }


numeric sort
perl -e 'print map { join(" ", @$_), "\n" } sort { $a->[0] <=> $b->[0]
} map { [split] } <>' filename

sort { $a->[1] <=> $b->[1] }


Or perhaps a GRT instead:

perl -le'print for map join( "", reverse /(\S+)(\s+)(\S+)/ ), sort map join( "", reverse /(\S+)(\s+)(\S+)/ ), <>' filename




John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to