Keeping unique rows based on information from 2 of three columns

There is a "famous" awk idiom for exactly this. You want to do:

awk '!seen[$1,$2]++' file

That creates an associative array "seen" with the 2 columns as the key. Use the post-increment operator so that, for the first time you encounter that key, the value is zero. The use the negation operator for a "true" result the first time you see the key.


If you don't mind that the output is sorted:

sort -u -k1,2 file
  • -u - unique
  • -k1,2 - use fields 1 and 2 together as the key

Tags:

Awk

Uniq