Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 452
jknielse Avatar asked Apr 08 '14 20:04

jknielse


People also ask

Which is natural sort order?

In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order, except that multi-digit numbers are treated atomically, i.e., as if they were a single character.

How do you sort in ascending order in bash?

Sort a File Numerically To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order. If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

How do I sort in alphabetical order in bash?

Bash Sort Files Alphabetically By default, the ls command lists files in ascending order. To reverse the sorting order, pass the -r flag to the ls -l command, like this: ls -lr . Passing the -r flag to the ls -l command applies to other examples in this tutorial.

How do I sort by alphabetical order in Linux?

In the Linux system, you will find one command named sort. This command can sort your data alphabetically. Here flag -k is used to select a field for sorting.


1 Answers

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 
like image 126
devnull Avatar answered Oct 02 '22 17:10

devnull