Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

sorting - Bash Sort like Windows 'natural sort order'

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.

The issue I'm having is that piping the file names to sort does not produce the desired order. None of the flags of sort give 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:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

For your input, it'd produce:

test1.pdf
test2.pdf
test10.pdf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...