1

I have two files like this:

File 1

apple fruit 10 4
strawberry fruit 22 11
berry fruit 33 33
ball toy 1 12
straw thing 2 33

File 2

strawberry straw berry
rawberry raw berry

I want to compare column 1 of file 2 with column 1 of file 1, column 2 of file 2 with column 1 of file 1, column 3 of file 2 with column 1 of file 1. If match is found, I want to display all three matching lines from file 1.

Here, the output should be:

strawberry fruit 22 11
straw thing 2 33
berry fruit 33 33

How can I do this?

1
$ awk 'NR==FNR{a[$1]=$0} $1 in a && $2 in a && $3 in a{print a[$1] ORS a[$2] ORS a[$3]}' file1 file2
strawberry fruit 22 11
straw thing 2 33
berry fruit 33 33
  • Save lines based on first column of file1 and then print matching lines if all three columns of file2 lines match
| improve this answer | |

Your Answer

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