linux系統中sort命令對字元數字組合字串進行排序
阿新 • • 發佈:2022-01-02
1、測試資料
root@DESKTOP-1N42TVH:/home/test2# cat test.txt
Chr1
Chr11
Chr7
Chr8
Chr10
Chr2
Chr5
Chr3
Chr4
Chr12
Chr6
Chr9
2、排序
root@DESKTOP-1N42TVH:/home/test2# cat test.txt Chr1 Chr11 Chr7 Chr8 Chr10 Chr2 Chr5 Chr3 Chr4 Chr12 Chr6 Chr9 root@DESKTOP-1N42TVH:/home/test2# sort test.txt ## 直接排序,2 大於 10? Chr1 Chr10 Chr11 Chr12 Chr2 Chr3 Chr4 Chr5 Chr6 Chr7 Chr8 Chr9 root@DESKTOP-1N42TVH:/home/test2# sort -n test.txt ## -n選項按數值排序,仍然不能滿足需求 Chr1 Chr10 Chr11 Chr12 Chr2 Chr3 Chr4 Chr5 Chr6 Chr7 Chr8 Chr9 root@DESKTOP-1N42TVH:/home/test2# sort -g test.txt ## -g Chr1 Chr10 Chr11 Chr12 Chr2 Chr3 Chr4 Chr5 Chr6 Chr7 Chr8 Chr9
3、sort -V 對字串數值組合排序
root@DESKTOP-1N42TVH:/home/test2# cat test.txt Chr1 Chr11 Chr7 Chr8 Chr10 Chr2 Chr5 Chr3 Chr4 Chr12 Chr6 Chr9 root@DESKTOP-1N42TVH:/home/test2# sort -V test.txt ## -V選項實現對字元和數值組合的排序 Chr1 Chr2 Chr3 Chr4 Chr5 Chr6 Chr7 Chr8 Chr9 Chr10 Chr11 Chr12 root@DESKTOP-1N42TVH:/home/test2# sort -Vr test.txt ## 逆向排序 Chr12 Chr11 Chr10 Chr9 Chr8 Chr7 Chr6 Chr5 Chr4 Chr3 Chr2 Chr1