Bash: sort text file by last field value

前端 未结 6 1157
自闭症患者
自闭症患者 2020-12-24 14:09

I have a text file containing ~300k rows. Each row has a varying number of comma-delimited fields, the last of which is guaranteed numerical. I want to sort the file by this

6条回答
  •  伪装坚强ぢ
    2020-12-24 15:03

    Use awk to put the numeric key up front. $NF is the last field of the current record. Sort. Use sed to remove the duplicate key.

    awk -F, '{ print $NF, $0 }' yourfile | sort -n -k1 | sed 's/^[0-9][0-9]* //'
    

提交回复
热议问题