Bash Sort 类似于 Windows 的“自然排序顺序”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22948042/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:09:00  来源:igfitidea点击:

Bash Sort like Windows 'natural sort order'

bashsortingcygwin

提问by jknielse

I've got a bunch of PDFs that I'm trying to append together, and I've got a program that, given a list of files, will append them to one PDF.

我有一堆 PDF,我正试图将它们附加在一起,而且我有一个程序,可以根据文件列表将它们附加到一个 PDF 中。

The issue I'm having is that piping the file names to sortdoes not produce the desired order. None of the flags of sortgive me what I want either. I've got some examples below:

我遇到的问题是将文件名通过管道进行排序不会产生所需的顺序。的国旗没有那种给我什么,我想无论是。我在下面有一些例子:

Desired sort order:

所需的排序顺序:

test1.pdf
test2.pdf
test10.pdf

Achieved sort order using sort:

使用sort实现排序顺序:

test1.pdf
test10.pdf
test2.pdf

For more info on exactly what constitutes the sort order I desire, see:

有关确切构成我想要的排序顺序的更多信息,请参阅:

http://msdn.microsoft.com/en-us/library/bb759947.aspx

http://msdn.microsoft.com/en-us/library/bb759947.aspx

回答by devnull

Assuming you're using GNU sort, use the-Voption:

假设您使用的是 GNU 排序,请使用以下-V选项:

   -V, --version-sort
          natural sort of (version) numbers within text

For your input, it'd produce:

对于您的输入,它会产生:

test1.pdf
test2.pdf
test10.pdf