Sorting on the last field of a line

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

    awk '{print $NF,$0}' file | sort | cut -f2- -d' '
    

    Basically, this command does:

    1. Repeat the last field at the beginning, separated with a whitespace (default OFS)
    2. Sort, resolve the duplicated filenames using the full path ($0) for sorting
    3. Cut the repeated first field, f2- means from the second field to the last

提交回复
热议问题