On Mon, Jun 15, 2009 at 11:28, Aimee Cardenas<[email protected]> wrote:
> Hi, All!
>
> 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


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


1. http://en.wikipedia.org/wiki/Schwartzian_transform

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to