Sorting on the last field of a line

后端 未结 11 1009
暗喜
暗喜 2020-12-05 04:06

What is the simplest way to sort a list of lines, sorting on the last field of each line? Each line may have a variable number of fields.

Something like



        
11条回答
  •  暖寄归人
    2020-12-05 05:00

    A one-liner in perl for reversing the order of the fields in a line:

    perl -lne 'print join " ", reverse split / /'
    

    You could use it once, pipe the output to sort, then pipe it back and you'd achieve what you want. You can change / / to / +/ so it squeezes spaces. And you're of course free to use whatever regular expression you want to split the lines.

提交回复
热议问题