Bug in sort or misunderstanding

3

Suppose I have this file

b 10 foo
a 10 bar
a 2 bar

I want to sort by the third colum, alphabetically. In case of ties, I want to sort by the second column, numerically. In the man page for the unix sort program you find this:

> sort -k3 -k2n a

Which gives

a 2 bar
a 10 bar
b 10 foo

Which is correct.

Now, what I actually want, is to sort by the first column, alphabetically, and in case of ties, by the second column, numerically. Can anybody explain to me why

> sort -k1 -k2n a

Gives

a 10 bar
a 2 bar
b 10 foo

Which is plain wrong?

gaston

Posted 2012-11-09T17:06:08.737

Reputation: 231

1try -k1,1 -k2,2n but this is really more a superuser question since no programming is involed – frankc – 2012-11-09T17:19:15.793

Answers

2

The reason your example doesn't work as you expect is that sort considers all positions after the specified one as well. In your example, the dictionary sort of -k1 sorts the string a 10 bar before a 2 bar, and you don't need a tie break.

You need to explicitly specify the limits of the sort key, as described in the man page:

   -k, --key=POS1[,POS2]
          start a key at POS1, end it at POS2 (origin 1)

Example:

$ sort -k1,1 -k2n theFile
a 2 bar
a 10 bar
b 10 foo

Daniel Beck

Posted 2012-11-09T17:06:08.737

Reputation: 98 421

Thanks! I figured I would only need POS2 if I would want to sort using a range of columns and missed the default behaviour of sort using the remainder of the line from POS1 for sorting! Now it works like a charm. – gaston – 2012-11-12T08:37:16.353