[NTLUG:Discuss] Sort ?

Jay F. Cox baa204 at saturn.angelo.edu
Wed Aug 11 16:01:34 CDT 1999


S Jackson wrote:
> 
> This may be a General Unix question, but is it possible to sort a file
> and sort forward on one field, and reverse on another at the same time?
> I hope that question made sense. Thanx
> 
> Jackson

learn perl ;-)

echo -e 'b z blah\na q gah\na a 1234\nb w 12341\nb n zoo \nb a' |perl
-we 'while(<>){/(\w+)(\W+)(\w+)(.*)/s;push @a,[$1,$2,$3,$4]} print @{$_}
foreach(sort { $a->[0] cmp $b->[0]|| $b->[2] cmp $a->[2]} @a);'
a q gah
a a 1234
b z blah
b w 12341
b n zoo 
b a

Even though this code is definitely not for perl newbies (and maybe a
little bit masochistic), I'm not sure I see any way using using the unix
sort command.  Perhaps there's a way using awk.

--

Jay Cox
--
optimist, n:
	A bagpiper with a beeper.




More information about the Discuss mailing list