Unix vs Excel sort

2

I have the following list of files sorted according to Excel:

a1.txt
a10.txt
a11.txt
a12.txt
a2.txt

If I use sort on Unix/Linux, I get the following order:

a10.txt
a11.txt
a12.txt
a1.txt
a2.txt

where you can see a different behavior for the character .. Is there an option to get the same order as Excel? I tried -b, -g, -n, ... without success.

tristof

Posted 2015-12-09T11:53:19.967

Reputation: 95

Hm, I would think -n would work – Raystafarian – 2015-12-09T11:59:15.480

Answers

4

env LC_ALL=C sort 

From the man page of sort:

  *** WARNING *** The locale specified by the  environment  affects  sort
   order.  Set LC_ALL=C to get the traditional sort order that uses native
   byte values.

See this StackOverflow question for more details

davidmneedham

Posted 2015-12-09T11:53:19.967

Reputation: 1 724

Thanks, I had tried it before on a csh shell by typing set LC_ALL=C but it did not work. After changing it to setenv LC_ALL C, it now works perfectly. Thanks! – tristof – 2015-12-09T16:25:44.877

@tristof - I found that this did not work with bash on Ubuntu, but sort -V did the job. I am indebted to this link.

– AFH – 2015-12-09T22:54:03.367

Running bash version 4.3.11(1) on Linux Mint here, and env LC_ALL=C sort newfile.txt gives the desired sort order of a1.txt a10.txt a11.txt a12.txt a2.txt – davidmneedham – 2015-12-10T03:05:45.247

1

Give a try to the following command : sort -d ;)

Germain

Posted 2015-12-09T11:53:19.967

Reputation: 131

1Unfortunately, I had tried the dictionary order -d and it does not work. Thanks anyway! – tristof – 2015-12-09T16:22:43.530