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 a
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
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:
uniq
)Probably not the most efficient in the world, but at least each step makes some sense.