Awk File Comparison – Methods and Examples

awkjoin;text processing

I have a file1.txt

USA Joe 123.123.123
Russia Marry 458.786.892
Canada Greg 151.844.165
Latvia Grace 125.895.688

and file2.txt

1 123.123.123
2 151.844.165
3 465.879.515

and I want to create a new file result.txt where I print my only those lines that adresses (xxx.xxx.xxx) are both in file1 and file2 so my result should be

USA Joe 123.123.123
Canada Greg 151.844.165

I need to use awk, but how I need to use it for both files?

Best Answer

You can try:

awk 'FNR==NR{a[$2];next};$NF in a' file2.txt file1.txt > result.txt
Related Question