Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

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.

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.

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.

Source Link
Wildcard
  • 37.5k
  • 30
  • 149
  • 284

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.