0

I have a file containing the following content:

WP_021815421.1  OG5_132922  deth|YP_182312  3   -07 42.35   81.7
WP_021815427.1  OG5_128110  rbal|NP_868155  1   -55 50.72   53.2
WP_012456834.1  NO_GROUP    bsui|NP_700181  8   -53 48.18   62
WP_084606289.1  OG5_132922  aful|NP_069703  5   -28 57.00   92.6
WP_084606284.1  OG5_153487  bpse|YP_331974.1    6   -78 48.87   91.4

I want to remove the repitition line on the base of second coloumn (OG number). the output should not contain third row which contain the same OG number.

I am using sort -u and sort -k 2 -u but it doesn't work.

  • Can you please post an example of your expected output? – Panki May 14 at 13:33
  • 2
    Don't you mean that you want to omit the fourth row, where OG5_132922 matches the first row? – roaima May 14 at 13:38
  • Which row are you keeping when more rows share the same value for their second column? It looks like you want to keep the first row from the top of the file. Is it correct? – fra-san May 14 at 14:55
1

To limit the sort field to the second column (and not from the second column to the end of the line), you have to specify the end position:

$ sort -uk2,2  file
WP_012456834.1  NO_GROUP    bsui|NP_700181  8   -53 48.18   62
WP_021815427.1  OG5_128110  rbal|NP_868155  1   -55 50.72   53.2
WP_021815421.1  OG5_132922  deth|YP_182312  3   -07 42.35   81.7
WP_084606284.1  OG5_153487  bpse|YP_331974.1    6   -78 48.87   91.4
  • Be careful, this solution will also sort alphabetically by the second column. – Panki May 14 at 13:59
2
awk '!seen[$2]++' file

This will print only lines which are unique in the second column. Output:

WP_021815421.1  OG5_132922  deth|YP_182312  3   -07 42.35   81.7                                                                                                                                                                          
WP_021815427.1  OG5_128110  rbal|NP_868155  1   -55 50.72   53.2                                                                                                                                                                             
WP_012456834.1  NO_GROUP    bsui|NP_700181  8   -53 48.18   62                                                                                                                                                                               
WP_084606284.1  OG5_153487  bpse|YP_331974.1    6   -78 48.87   91.4

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.