Linux教程

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

本文主要是介绍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

 

这篇关于linux系统中sort命令对字符数字组合字符串进行排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!