Sort by third column leaving first and second column intact in Linux?

后端 未结 1 994
自闭症患者
自闭症患者 2020-12-16 11:38

I need to sort a flat file by third column leaving first column intact [First column is already sorted] (in linux). (second column may change)

Example i/p file:-

相关标签:
1条回答
  • 2020-12-16 12:41

    Try this:

    sort  -t: -k1,1 -k3 data.txt
    

    gives:

    bast:disp-san-d5-06:piranha 
    bast:display-san-12:redbird
    bast:display-san-07:waverider
    bast:display-san-12:waverider
    

    This will sort with the 1st field as primary key, and the 3rd field as secondary key splitting the line into fields by :

    Details:

    data.txt contains the 4 lines from your post.

    You can specify multiple fields as sorting keys, see the man page

    -k1,1 means sort on the first field (start at field 1 and end at field 1, otherwise it would continue using the rest of the line for determining the sort)

    -k3 means sort on the 3rd field as secondary key. Since there are no other fields behind it is not necessary to specify -k3,3 but it wouldn't hurt either.

    -t: means delimit fields in lines with the : character, otherwise blank is used by default

    More information see this SO question Sorting multiple keys with Unix sort and the sort man page

    0 讨论(0)
提交回复
热议问题