Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort and keep a unique duplicate which has the highest value

Tags:

unix

sorting

uniq

I have a file like the one shown below, I want to keep the combinations between the first and second field which has the highest value on the third field(the ones with the arrows, arrows are not included in the actual file) .

1   1   10
1   1   12        <- 
1   2   6         <-
1   3   4         <- 
2   4   32
2   4   37
2   4   39
2   4   40        <- 
2   45  12
2   45  15        <- 
3   3   12
3   3   15
3   3   17
3   3   19        <- 
3   15  4
3   15  9         <- 
4   17  25
4   17  28
4   17  32
4   17  36        <- 
4   18  4         <- 

in order to have and output like this:

1   1   12
1   2   6
1   3   4
2   4   40
2   45  15
3   3   19
3   15  9
4   17  36
4   18  4

And I thought maybe I just play with the sort and uniq command, but I made a mess.

Any ideas?

Very important note: the entries are not neatly sorted from the beginning, I just used sort -k1,1 -k2,2 -k3,3

Thanks in advance guys

like image 484
Tamalero Avatar asked Apr 02 '14 20:04

Tamalero


2 Answers

This is a bit funny, but:

sort -nr myfile.txt | rev | uniq -f1 | rev | sort -n

Output:

1   1   12
1   2   6 
1   3   4 
2   4   40
2   45  15
3   15  9 
3   3   19
4   17  36
4   18  4 

How it works:

  • Sort reverse numerically, putting the highest values at the top (so they are saved)
  • Reverse each line, so the last field is first (needed for uniq)
  • Save only the first uniq line, but ignoring the first field (was the last field)
  • Reverse the line back to original order
  • Sort the lines from low to high again

Probably not the most efficient in the world, but at least each step makes some sense.

like image 51
beroe Avatar answered Oct 18 '22 10:10

beroe


Two passes of sort should do it, for example in bash shell

sort -k1,1n -k2,2n -k3,3nr -t$'\t'  file  | sort -k1,1n -k2,2n -t$'\t' -u -s
1       1       12
1       2       6
1       3       4
2       4       40
2       45      15
3       3       19
3       15      9
4       17      36
4       18      4
like image 38
iruvar Avatar answered Oct 18 '22 11:10

iruvar