15

I tried to sort the content of a file in Ubuntu desktop 14.04 (Trusty Tahr). In my case, the expected result should be same as original content, but the actual result is not. Why?

# cat test.txt
a++-a
a++-b
a++-c
ab
ac
# cat test.txt | sort
a++-a
ab
a++-b
ac
a++-c
3
  • 4
    I hand you a small award trophy for your useless use of cat. Mar 17, 2015 at 23:55
  • 3
    The comment of @DavidFoerster is a funny way of pointing out that you can replace cat test.txt | sort by sort test.txt :) Mar 18, 2015 at 12:35
  • @VolkerSiegel: True, though there are useful formulations using cat to start. For example cat FILE | grep dev | sort will display only lines with "dev" in them (in sorted order). Using sort FILE | grep dev produces the same output but coloured.
    – AlainD
    Sep 9, 2019 at 9:56

2 Answers 2

21

You could use LC_ALL variable, set it to LC_ALL=C before calling sort

$ LC_ALL=C sort test.txt
a++-a
a++-b
a++-c
ab
ac

Read this answer, if you want to know what is this magically LC_ALL=C. Here is short summary:

The C locale is a special locale that is meant to be the simplest locale. You could also say that while the other locales are for humans, the C locale is for computers. In the C locale, characters are single bytes, the charset is ASCII, the sorting order is based on the byte values.

Also, as @KenMollerup pointed, quote from man 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.

So when using sort with LC_ALL=C, sort compare symbols bytewise. Otherwise sort will ignore all non alphanumerical characters.

2
  • Sorry didnt see this, I reacted to the comment! Mar 17, 2015 at 15:38
  • @KenMollerup thanks for pointing to man sort. I didn't notice it
    – c0rp
    Mar 17, 2015 at 15:41
8

Sort uses alphabetical and numeric sorting, same as us, special characters like + - < > ... are ignored, numbers are treated numerical so 1, 2, 3.. comes before 11, 12 1066 1104 -- see!

So your list is seen as: aa, ab, ab, ac, ac

4
  • Is there any option for sort, such that it will not ignore special characters such that test.txt will sort the desired way? Mar 17, 2015 at 15:28
  • 6
    See this in man 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. Mar 17, 2015 at 15:35
  • @KenMollerup, please add more information to your answer. Add quote from man sort, add examples.
    – c0rp
    Mar 17, 2015 at 15:42
  • Yes but I was tooo slow, see c0rp's answer below. Mar 17, 2015 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .