Script / sort question; sort on last field of a line?

Peter plp-ysDPMY98cNQDDBjDh4tngg at public.gmane.org
Thu May 4 21:55:06 UTC 2006


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.

Peter
--
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