Bash: sort text file by last field value

前端 未结 6 1156
自闭症患者
自闭症患者 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:45

    I'm going to throw mine in here as an alternative (and I couldn't get awk to work) :)

    sample file:

    Call of Doody                           1322
    Seam the Ripper                         1329
    Mafia Bots 1                            1109
    Chicken Fingers                         1243
    Batup Light                             1221
    Hunter F Tomcat                         1140
    Tober                                   0833
    

    code:

    for i in `sed -e 's/.* \(\d\)*/\1/' file.txt | sort`; do grep $i file.txt; done > file_sort.txt
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 15:00

    Perl one-liner:

    @lines=<STDIN>;foreach(sort{($a=~/.*,(\d+)/)[0]<=>($b=~/.*,(\d+)/)[0]}@lines){print;}
    
    0 讨论(0)
  • 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]* //'
    
    0 讨论(0)
  • 2020-12-24 15:05

    Python one-liner:

    python -c "print ''.join(sorted(open('filename'), key=lambda l: int(l.split(',')[-1])))"
    
    0 讨论(0)
  • 2020-12-24 15:06
    vim file.in -c '%sort n /.*,\zs/' -c 'saveas file.out' -c 'q'
    
    0 讨论(0)
提交回复
热议问题