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.

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?

share|improve this question

migrated from stackoverflow.com Nov 9 '12 at 18:04

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

1  
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

1 Answer 1

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)

Example:

$ 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

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.