Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


a 10 bar
a 2 bar
b 10 foo

Which is plain wrong?

share|improve this question

migrated from Nov 9 '12 at 18:04

This question came from our site for professional and enthusiast programmers.

try -k1,1 -k2,2n but this is really more a superuser question since no programming is involed – frankc Nov 9 '12 at 17:19
up vote 2 down vote accepted

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)


$ sort -k1,1 -k2n theFile
a 2 bar
a 10 bar
b 10 foo
share|improve this answer
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 Nov 12 '12 at 8:37

You must log in to answer this question.

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