An interesting sort problem

Bruno Wolff III bruno at wolff.to
Wed Nov 11 21:23:02 UTC 2015


On Wed, Nov 11, 2015 at 13:54:50 -0700,
  jd1008 <jd1008 at gmail.com> wrote:
>The sort command does not provide for a way to say that the key is the 
>last word in a line,
>where the file contains lines of varying number of words,
>and where words are groups of characters without spaces or tabs.
>
>Thus
>
>sort -k <what should this position be> <filename> ....
>
>How can one tell sort to chose the last word in the line as the sort key?

You can't. Field specifiers are relative to the start of the line. There 
isn't a way to specify them relative to the end of the line.

>
>The key is always the entirety of the last word.

If you just need grouping and not a particular order you might try something 
like:
rev <filename> | sort | rev

If you need something specific, you'll probably need to write your own 
program. It should be reasonably short in perl.


More information about the users mailing list