Search code examples
bashsortingnumerical

bash short by two columns


I've this dataset:

12363 111111
12363 222222
12363 3456
12364 2895
12364 257363
12364 246291
12364 243701
12364 243699

I would like to sort this by first column, numerical value, reverse and by second column, numerical value, reverse. Result would be:

12364 257363
12364 246291
12364 243701
12364 243699
12364 2895
12363 222222
12363 111111
12363 3456

I tried,

sort -rn
sort -rnk1,2
sort -rg
sort -rgk1,2

But somehow all of these gives back for the second column a wrong order (not numerical, but values):

12364 2895
12364 257363
12364 246291
12364 243701
12364 243699
12363 3456
12363 222222
12363 111111

Do you have any idea how to fix this?

Thanks!!


Solution

  • What about this?

    $ sort -rn -k1 -k2 file
    12364 257363
    12364 246291
    12364 243701
    12364 243699
    12364 2895
    12363 222222
    12363 111111
    12363 3456
    

    Note that -k1 -k2 is not the same as -k1,2.