Script / sort question; sort on last field of a line?
Chris F.A. Johnson
cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Thu May 4 22:10:45 UTC 2006
On Fri, 5 May 2006, Peter wrote:
>
> On Thu, 4 May 2006, Chris F.A. Johnson wrote:
>
>> > > sort -rk ... should do it
>> >
>> > This is by far the easiest solution posted, although I'm not sure why
>> > you
>> > would need "-r". That reverses the sort order (i.e. to reverse
>> > alphabetic)
>> > which wasn't in the problem description.
>> >
>> > Just to clarify Peter's answer. If you have N fields in a line, use:
>> >
>> > sort -k N
>>
>> That doesn't help if you don't know how many fields there are,
>> or if there is a variable number of fields.
>
> Oh, you mean he wants to sort variable length (in fields) lines ? There are
> such algorythms. I think that they are called 'phone book sorting' or similar
> (for obvious reasons). They are also used to sort f.ex. street names and
> people names (which have titles e.g. Dr. Eng. Fr. etc prepended). One way to
> do it with sort -k is to force the input to canonical length (in fields).
> This can be the length, in fields, of the longest line (again in fields).
> This requires two passes.
Or there is the method I posted earlier:
awk '{ printf "%s\t%s\n", $NF, $0 }' | sort | cut -f2-
Use $(NF-1) for penultimate field.
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list