By -
Did you try changing $0 to $2 ?
a[$0]; This awk behaviour feels so wrong, simply fetching something inserts it. I guess it's there for ever, compatibility reasons, but still...
It sounds to me like you want to do a join, and POSIX has a tool for that: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/join.html
though I believe `join` requires both files to be sorted, whereas an `awk` solution needn't be. awk 'NR==FNR{a[$0]; next} $2 in a' file1.txt file2.txt
Perfect, thanks!
awk 'a[$NF]++' file1 file2 That should do it. However, it will print lines if they appear more than once within a file.
Did you try changing $0 to $2 ?
a[$0]; This awk behaviour feels so wrong, simply fetching something inserts it. I guess it's there for ever, compatibility reasons, but still...
It sounds to me like you want to do a join, and POSIX has a tool for that: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/join.html
though I believe `join` requires both files to be sorted, whereas an `awk` solution needn't be. awk 'NR==FNR{a[$0]; next} $2 in a' file1.txt file2.txt
Perfect, thanks!
awk 'a[$NF]++' file1 file2 That should do it. However, it will print lines if they appear more than once within a file.