Skip to main content
2 of 2
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/

When you iterate over the same file twice you can use line numbers as convenient indices; it can make for cleaner logic.

awk 'NR == FNR {if ($2 in z) { y[z[$2]]; y[FNR] } z[$2]=FNR; next} (FNR in y)' file file

I used a similar trick in my answer to this question:


The basis of this trick is that Awk will create a variable simply by referencing it, and the index in arrayname construct returns true or false depending on whether an array element has been created with the specified index.

Wildcard
  • 37.5k
  • 30
  • 149
  • 284