How to grep two strings or words in file

I need to find two words or string in file. So i try

grep -w word1 file | grep -w word2
grep -w string1 file | grep -w string2

It is not working at all. Why don’t grep support searching two words? Is it even possible?

Hi,

You can use the below method:

grep -e "string1" -e "string2" file
1 Like

Another option is

egrep -w 'word1|word2' input_file
command | egerp -w 'string1|string2|string3'
egrep -w 'word1|word2' filename
egrep -wi 'foo|bar' data.txt

You should read my page - Search Multiple Words / String Pattern Using grep Command on Bash shell

2 Likes