I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:

2 3
2 2
1 2
2 1
1 1

Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:

1 1
1 2
2 1
2 2
2 3

However, I want to keep the 2nd column ordering, like this:

1 2
1 1
2 3
2 2
2 1

Is this possible with the sort command?

link|improve this question
feedback

migrated from stackoverflow.com Sep 1 '09 at 4:49

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

1 Answer

up vote 14 down vote accepted

Give this a try:

sort -s -n -k 1

The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.

The -k doesn't actually mean "this field and all of the following", as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line.

link|improve this answer
You are right. This is exactly what I needed. Thanks! – ssn Aug 31 '09 at 16:31
feedback

Your Answer

 
or
required, but never shown

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