Sort CSV based on a certain column?

前端 未结 7 961
自闭症患者
自闭症患者 2020-12-18 09:50

I\'m sure I\'ve done this in the past and there is something small I\'m forgetting, but how can I sort a CSV file on a certain column? I\'m interested in answers with and wi

7条回答
  •  不知归路
    2020-12-18 10:40

    In the spirit of there always being another way to do it, bear in mind that plain old GNU sort might be enough.

    $ sort -t, -k2 -n unsorted.txt
    name,21,male
    name,24,male
    name,25,female
    name,27,female
    

    Where the command line args are:

    -t, # use comma as the record separator
    -k2 # sort on the second key (record) in the line
    -n  # sort using numerical comparison (like using <=> instead of cmp in perl)
    

    If you want a Perl solution, wrap it in qx() ;-)

提交回复
热议问题