5

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
  • 1
    Weird requirement. Why necessarily awk? Commented May 31, 2015 at 18:17

2 Answers 2

7

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) ;)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.