bash Bash真正的数字顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4234347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash true numerical order
提问by danidacar
How can I order files in a directory by their true numeric order.
如何按真正的数字顺序对目录中的文件进行排序。
file1.txt
file2.txt
file11.txt
...
I think it's called : Natural Order
我认为它叫做:自然秩序
回答by icanhasserver
Use the -voption:
使用-v选项:
ls -v file*
file1
file2
file11
file12
Another option may be using sort -V, assuming that one is available on your platform:
另一种选择可能是使用sort -V,假设您的平台上有一个可用:
ls file* |sort -V
回答by Jens
For this particular list of files, sort numerically starting at the fifth character of the first field.
对于这个特定的文件列表,从第一个字段的第五个字符开始按数字排序。
$ ls file*|sort -k1.5n
file1
file2
file11
file12
回答by thejh
If all filenames are fileSOMENUMBER.txt, try this:
如果所有文件名都是fileSOMENUMBER.txt,请尝试以下操作:
ls -1|sed 's:^[^0-9]*\([0-9]*\).*$::g'|sort -n|sed 's:^\(.*\)$:file.txt:g'

