[Discuss] sort by value of last field in a line
Noel Burton-Krahn
noel at burton-krahn.com
Thu Nov 22 22:08:04 PST 2007
perl -e 'print(sort { (split(" ", $a))[-1] cmp (split(" ", $b))[-1] } <>)'
1. the <> returns a list of all lines from stdin or filenames in argv
2. the sort sorts them by the split criteria
3. the split() breaks each line into fields, and the [-1] picks the last
field
Hooray for perl, the self-compressing language!
~Noel
----- Original Message -----
From: "Xavier Mossman" <xavier.mossman at gmail.com>
To: <discuss at vlug.org>
Sent: Thursday, November 22, 2007 6:10 PM
Subject: [Discuss] sort by value of last field in a line
> hmm this doesn't seem to work for me. multiline file with variable no.
> of space seperated fields per line. need to sort lines on the value of
> the last field in a line. output needs to show all fields for each
> line. getting rid of dups in last field would be so nice
> sort won't let you specify sort on last field when you don't know how
> many fields per line. gawk isn't so friendly for sorting by field
> value - end up trying to sort a multi-dim array so yu can preserve the
> original index - then still need to sort the original array.
> anyone wanna show me up on this? don't care whats used as long as its
> standard shell stuff
>
> tx Xav.
> _______________________________________________
> Discuss mailing list
> Discuss at vlug.org
> http://ladybug.vlug.org/cgi-bin/mailman/listinfo/discuss
>
More information about the Discuss
mailing list