Alex Trevylan
Alex Trevylan

Reputation: 545

How can I compare different columns in two files, and print out in first file?

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

Answers (1)

Philip Kirkbride
Philip Kirkbride

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:

More comprehensive answer

Upvotes: 1

Related Questions