Reputation: 545
I have two files that look this:
file1:
apples
pears
grapes
oranges
melon
file2:
gene1 apples
gene2 television
gene3 pears
gene4 homeless
desired output:
gene1 apples
gene3 pears
I want to use awk to compare column1 of file1 with column2 of file2 and print out the whole line of file2? How can I do this?
Upvotes: 1
Views: 67
Reputation: 22879
You can do this by using each line of file one as a grep value used on file2, like this:
cat file1.txt | xargs -I{} sh -c 'cat file2.txt | grep {}'
Or even better:
grep -f file1.txt file2.txt
Notes:
First example is vulnerable to injection attacks. That is someone could put a malicious bash command into the text files you process to have it executed. That isn't the case with the second example.
The main downside to the second example is that it won't match if you have extra spaces at the end of file1. That is "apple"
will be treated differently than "apple "
(notice space at the end of the second apple).
Resources:
Upvotes: 1