Sort one column ascending and another column descending

sort

And by strange, I probably mean that it's intended behavior, but I don't understand why, nor how to fix it.

The task is to sort based on column 5, and then column 4.

grep -w RockDoveHPG_Transcript_21  xaa.blast| sort  -gk5,5 -gk4,4

which gives me

RockDoveHPG_Transcript_21   XM_004941221.2  70.588  17  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  82.353  17  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  68.182  22  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  80.000  25  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  57.692  26  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  70.588  34  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  100.000 77  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  77.778  18  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  89.474  19  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  70.000  20  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  71.429  21  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  72.000  25  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  72.727  33  2.46e-80
RockDoveHPG_Transcript_21   XM_004941221.2  94.737  76  2.46e-80
...
RockDoveHPG_Transcript_21   XM_004941221.2  78.947  19  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  71.429  21  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  83.544  79  1.56e-43

Col 5 sorts just fine, but ideally I'd like Col 4 sorted highest to lowest. Right now it's low to high. Ah, that sounds like I need the -r flag..

grep -w RockDoveHPG_Transcript_21  xaa.blast| sort  -gk5,5 -r -gk4,4

Now Col 5 is sorted in the opposite direction, but Col 4 is good.

RockDoveHPG_Transcript_21   XM_004941221.2  83.544  79  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  71.429  21  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  78.947  19  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  68.750  16  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  86.667  15  1.56e-43
RockDoveHPG_Transcript_21   XM_004941221.2  85.526  76  6.22e-44
...
RockDoveHPG_Transcript_21   XM_004941221.2  100.000 77  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  70.588  34  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  57.692  26  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  80.000  25  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  68.182  22  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  82.353  17  4.17e-86
RockDoveHPG_Transcript_21   XM_004941221.2  70.588  17  4.17e-86

So basically it seems like the -r flag is not applied column-wise.. Reversing order of 1 sorted column reverses the order of the other..

What I'd like, is to have

RockDoveHPG_Transcript_21   XM_004941221.2  100.000 77  4.17e-86

returned at the top, which I think should be accomplished by sorting Col5 lowest to highest and then Col 4 highest to lowest.

Best Answer

By adding -r the way you did, it is being treated as a global option. From info sort:

A position in a sort field specified with ‘-k’ may have any of the option letters ‘MbdfghinRrV’ appended to it, in which case no global ordering options are inherited by that particular field.

Note the phrase "appended to it" i.e. to reverse only field 4, use -gk4,4r

Related Question