Bash: sort text file by last field value

前端 未结 6 1169
自闭症患者
自闭症患者 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 14:56

    Maybe reverse the fields of each line in the file before sorting? Something like

    perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")' |
      sort -t, -n -k1 |
      perl -ne 'chomp; print(join(",",reverse(split(","))),"\n")'
    

    should do it, as long as commas are never quoted in any way. If this is a full-fledged CSV file (in which commas can be quoted with backslash or space) then you need a real CSV parser.

提交回复
热议问题