[英]Linux sort folders by numerical value
We have a data set from 我们有一个数据集
ls -1 /opt/test/kortinfo/
1.0.0
1.0.1
1.0.2
1.1.0
1.10.0 // Error
1.2.0
1.3.0
1.4.0
1.5.0
1.5.1
1.5.2
1.6.0
1.7.0
1.8.0
1.8.1
1.9.0
As we can see here the folder 1.10.0
is misplaced since it is larger than 1.9.0
正如我们在这里看到的,文件夹
1.10.0
放错了位置,因为它大于1.9.0
We tried to pipe it through sort -g
, sort -n
and sort -ng
but neither will correctly display the folders sorted by their number values. 我们尝试通过
sort -g
, sort -n
和sort -ng
传递它,但是它们都不能正确显示按其数字值排序的文件夹。
ls -1 /opt/test/kortinfo/ | grep -v "siste" | sort -ng
1.0.0
1.0.1
1.0.2
1.1.0
1.10.0 // Still the same error
1.2.0
1.3.0
1.4.0
1.5.0
1.5.1
1.5.2
1.6.0
1.7.0
1.8.0
1.8.1
1.9.0
Does anyone know how I can get these sorted correctly by number value? 有谁知道我该如何正确地按数字值对它们进行排序?
Use the -v
switch of ls
: 使用
ls
的-v
开关:
ls -v
man ls
says: man ls
说:
-v natural sort of (version) numbers within text
-v文本中自然的(版本)数字排序
As a reference, one could also solve this problem with padding the version numbers with brace expansion, if applicable. 作为参考,如果适用,还可以通过用大括号扩展填充版本号来解决此问题。
Here's an example 这是一个例子
touch 1.{01..10}.{01..05}
This will create the versions with appropriate padding of the version representations and simplify your life significantly. 这将使用适当的版本表示填充来创建版本,并显着简化您的生活。
The result: 结果:
mace-windu:useme [ ~/Desktop/1 ] ls -1
1.01.01
1.01.02
1.01.03
1.01.04
1.01.05
1.02.01
1.02.02
1.02.03
1.02.04
1.02.05
1.03.01
1.03.02
1.03.03
1.03.04
1.03.05
1.04.01
1.04.02
1.04.03
1.04.04
1.04.05
1.05.01
1.05.02
1.05.03
1.05.04
1.05.05
1.06.01
1.06.02
1.06.03
1.06.04
1.06.05
1.07.01
1.07.02
1.07.03
1.07.04
1.07.05
1.08.01
1.08.02
1.08.03
1.08.04
1.08.05
1.09.01
1.09.02
1.09.03
1.09.04
1.09.05
1.10.01
1.10.02
1.10.03
1.10.04
1.10.05
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.