5

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.

  • I believe something like: sort -k5,5gr -k4,4n is much more clear... – JJoao Jul 14 '17 at 17:13
6

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

4

use

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

Notice the r at the end.

  • That's weird.. but it works. Thanks a bunch! – Matt MacManes Jul 14 '17 at 13:28
  • 3
    @MattMacManes Actually, that's not weird. For each key to sort, you can have options which are placed after the key definition (see man page). Also your -g option could be placed behind the key, so you could do a different kind of sorting on the other key. So in your case better write sort -k5g -k4gr (why this 5,5 stuff anyhow?) – Philippos Jul 14 '17 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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