4

Sort is sorting differently than I would expect. I have this file, call it text.txt:

a   1
A   1
a   11

(the space is always one \t)

I want to sort them alphabetically by the first column. However, when I do

sort -k 1 text.txt

all I got is the text.txt file, not sorted. If I do it by the deprecated + - notation, meaning

sort +0 -1 text.txt

it works as it should, meaning that I get this output:

a   1
a   11
A   1

This strange behaviour occurs only when I have lines that differs only by case. What am I doing wrong?

Karel Bílek
  • 1,951

2 Answers2

4

You have to specify the end column, too:

$ sort -k1,1 text.txt
a       1
a       11
A       1

To quote the GNU sort man page:

   -k, --key=POS1[,POS2]
          start a key at POS1 (origin 1), end it at POS2 (default  end  of
          line)
maxschlepzig
  • 57,532
2

You most certainly hit upon a bug in sort! If you had no spaces in the file, there would be no way to sort it properly:

$ cat aaa
a1
A1
a11

$ sort aaa
a1
A1
a11

$ sort -k1,1 aaa
a1
A1
a11

Even more visible with the following:

$ cat bbb
A B b 0
a B b 0
A b b 1

$ sort bbb
a B b 0
A B b 0
A b b 1

$ sort -k1,2 bbb
a B b 0
A b b 1
A B b 0