Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The sort utility in Ubuntu 10.04 (Lucid) always sort by case-insensitive, just like if you specify --ignore-case to it.

The two sort just give the same result: 

echo -e "c\nb\nB\na" | sort
echo -e "c\nb\nB\na" | sort --ignore-case

But sometimes I want to sort by case-sensitive, so the upper-case letters come first, then lower-case letter. Is it possible?

share|improve this question

2 Answers 2

up vote 12 down vote accepted

Override the collation order.

echo -e "c\nb\nB\na" | LC_COLLATE=C sort
share|improve this answer
    
This works, but by definition only if no foreign chars. are in play; they will sort after the 7-bit ASCII letters; try echo $'B\nÄ\nb\na' | LC_COLLATE=C sort. Shouldn't the fact that GNU sort with a non-C locale always performs case-INsensitive sorting be considered a bug? –  mklement0 May 28 at 17:38

Interestingly, yet another sort order is available like this:

echo -e "c\nb\nB\na" | LC_COLLATE=C sort --ignore-case

which puts the uppercase letter before its corresponding lowercase letter.

Here is a comparison of their outputs (I added "d" and "D") in the en_US.UTF-8 locale (except where overridden):

  1. echo -e "d\nD\nc\nb\nB\na" | sort
  2. echo -e "d\nD\nc\nb\nB\na" | sort --ignore-case
  3. echo -e "d\nD\nc\nb\nB\na" | LC_COLLATE=C sort
  4. echo -e "d\nD\nc\nb\nB\na" | LC_COLLATE=C sort --ignore-case

Output:

1   2   3   4
-   -   -   -
a   a   B   a
b   b   D   B
B   B   a   b
c   c   b   c
d   d   c   D
D   D   d   d
share|improve this answer
    
Interesting; I see this behavior in GNU sort v5.93 (comes with OS X 10.9.3(!)) and v8.13, but NOT in v8.21 and v8.22. I guess the results of 2. and 4. can still be considered equivalent (but that would obviously change with the addition of foreign characters). –  mklement0 May 31 at 5:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.