4

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?

  • 1
    Weird requirement. Why necessarily awk? – Kusalananda May 31 '15 at 18:17
5

You can try:

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

Perhaps like this:

join -1 3 -2 2 <(sort -k 3 file1.txt) <(sort -k 2 file2.txt) | awk '{ print $2, $3, $1 }'

This:

  • doesn't read any file entirely in memory
  • still uses awk(1) ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.