Ah, ok. I see what you mean. That was a tricky one. I had to go to the sort info page to figure it out.
Code:
$ cat test.dat | sort -k1,1 -k2,2n
aaaa 2
aaaa 4
aaaa 12
aaaa 21
aaaa 1002
bbbb 3
bbbb 20
bbbb 30
When sorting by multiple columns, using only
-k1 messes it up because that sorts the value from the beginning of the field to the end of the line. You have to tell it to focus only on the current field.
Note that in this example the full
-k2,2 is not really necessary, since there are no following fields after it that need to be ignored. Only the first field needs the full specification. But it would be needed if you wanted to further sort the data by a third field.