Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Linux - Find occurrences of X OR Y in a file

Given a list of names in mates.txt

grep bob mates.txt

would find all strings that match bob

grep bob mates.txt | grep ro

would find all lines that have bob & ro, so would get robert, but NOT just bob on its own

grep -E '(bob|bill)' mates.txt

(grep -E = Interpret pattern as an extended regular expression)
would find all occurrences of bob OR bill

Sometimes you might have a list of search terms formed by another function. If you have a file of terms called search.txt, which contains Bob and Bill on new lines

. grep -f search.txt mates.txt

would find all occurrences of Bob OR Bill

Share this!