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.