david
david

Reputation: 761

compare 2 files and show only lines with partial matches

file #1 example:

one two three 
four five six 
seven eight nine
eleven 

file #2 example:

 two
 five
 nine.not
 eleven

I would like to find any lines on the file #1 that contain any word present on file #2, example output:

one two three
four five six 
eleven

I was trying to see if there's a way to do it in the linux command line, but have not been successful yet. any ideas?

Thanks

Upvotes: 2

Views: 2422

Answers (2)

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

you can try:

grep -f file2 file1

-f option obtain patterns from file2 (one per line)

edit

@Barmar comment

grep -F -w -f file2 file1

with -w option, lines like eleveneleven are not selected

Upvotes: 5

anubhava
anubhava

Reputation: 785266

You can use this awk command:

awk 'FNR==NR{a[$1]; next} {for (i in a) if (index($0, i)) print}' file2 file1
one two three
four five six
eleven

Upvotes: 1

Related Questions