Bash/Linux Sort by 3rd column using custom field seperator

后端 未结 3 655
梦谈多话
梦谈多话 2021-01-16 18:20

I can\'t seem to sort the following data as I would like;

find output/ -type f -name *.raw | sort 
output/rtp.0.0.raw
output/rtp.0.10.raw
output/rtp.0.11.raw         


        
3条回答
  •  梦毁少年i
    2021-01-16 18:50

    This makes it:

    $ sort -t'.' -n -k3 a
    output/rtp.0.0.raw
    output/rtp.0.1.raw
    output/rtp.0.2.raw
    output/rtp.0.3.raw
    output/rtp.0.4.raw
    output/rtp.0.5.raw
    output/rtp.0.6.raw
    output/rtp.0.7.raw
    output/rtp.0.8.raw
    output/rtp.0.9.raw
    output/rtp.0.10.raw
    output/rtp.0.11.raw
    output/rtp.0.12.raw
    output/rtp.0.13.raw
    output/rtp.0.14.raw
    output/rtp.0.15.raw
    output/rtp.0.16.raw
    output/rtp.0.17.raw
    output/rtp.0.18.raw
    output/rtp.0.19.raw
    output/rtp.0.20.raw
    

    As you see we need different options:

    • -t'.' to set the dot . as the field separator.
    • -n to make it numeric sort.
    • -k3 to check the 3rd column.

    Update

    This also makes it:

    $ sort -t'.' -V -k2 a
    output/rtp.0.0.raw
    output/rtp.0.1.raw
    output/rtp.0.2.raw
    output/rtp.0.3.raw
    output/rtp.0.4.raw
    output/rtp.0.5.raw
    output/rtp.0.6.raw
    output/rtp.0.7.raw
    output/rtp.0.8.raw
    output/rtp.0.9.raw
    output/rtp.0.10.raw
    output/rtp.0.11.raw
    output/rtp.0.12.raw
    output/rtp.0.13.raw
    output/rtp.0.14.raw
    output/rtp.0.15.raw
    output/rtp.0.16.raw
    output/rtp.0.17.raw
    output/rtp.0.18.raw
    output/rtp.0.19.raw
    output/rtp.0.20.raw
    

    As you see we need different options:

    • -t'.' to set the dot . as the field separator.
    • -V to make it sort based on version.
    • -k2 to check the 2nd column.

提交回复
热议问题