Search Multiple Words with grep

If you are looking for searching at least 2 words in a file with grep, you can use the following:

grep 'word1\|word2\|word3' path_to_file

grep -E 'word1|word2|word3' path_to_file

and if you want to show the the lines where all the words are on the same line use -w like so:

grep -w 'word1\|word2\|word3' path_to_file

 

Note: I’ve seen -w missing some of the output when you only put one word (usually in scripts). So i only used -E switch and it seemed to work. It showed all the searched word on the same line.

Enjoy scripting! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *