linux系统中sort命令对字符数字组合字符串进行排序

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

 

上一篇:gz文件压缩、解压缩保留源文件


下一篇:python函数