Denis Rouzaud
Denis Rouzaud

Reputation: 2600

Sort point character first

I would like to sort a list of file names using sort. For instance:

file.ext
file1.ext
z_file2.ext

Using sort, I get

file1.ext
file.ext
z_file2.ext

How can I do so that file. is sorted before fileXXXX. ?

Upvotes: 0

Views: 60

Answers (3)

Quentin
Quentin

Reputation: 511

You can use -d option

From manpage:

-d, --dictionary-order consider only blanks and alphanumeric characters

$ cat toto
file.ext
file1.ext
z_file2.ext

$ sort -d toto
file1.ext
file.ext
z_file2.ext

Upvotes: 0

karakfa
karakfa

Reputation: 67467

You have to separate the filenames from the digits, sort them accordingly and merge back

$ sed -r 's/([0-9]*)\./ &/' file | sort -k1,1 -k2n | sed 's/ //'
file.ext
file1.ext
z_file2.ext
z_file11.ext

Upvotes: 0

rici
rici

Reputation: 241701

As suggested in a comment, your problem is that your locale produces an odd sort order. Setting the locale to C for the sort should fix the problem:

LC_ALL=C sort

For a more precise fix, assuming you want to use locale-aware collation order but still separate the sort key at the extension, specify . as the field delimiter and use two sort keys:

sort -t. -k1,1 -k2

Upvotes: 3

Related Questions