Sorting on the last field of a line

后端 未结 11 991
暗喜
暗喜 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:08

    Here is a python oneliner version, note that it assumes the field is integer, you can change that as needed.

    echo file.txt | python3 -c 'import sys; list(map(sys.stdout.write, sorted(sys.stdin, key=lambda x: int(x.rsplit(" ", 1)[-1]))))'
    

提交回复
热议问题