I've encountered this problem before. Bruno is correct, sort criteria begin at the start of the line.What I've done was pipe my file through awk, moving the last word to be the 'first' word on the line... pipe that into sort, then back into awk again putting the 'first' word last.
From: Bruno Wolff III bruno@wolff.to To: jd1008 jd1008@gmail.com Cc: Fedora Community Users Support users@lists.fedoraproject.org Sent: Wednesday, November 11, 2015 4:23 PM Subject: Re: An interesting sort problem
On Wed, Nov 11, 2015 at 13:54:50 -0700, jd1008 jd1008@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.