-   Linux - Newbie (
-   -   Using "OR" type matching pattern with grep command (

riteshanand40 05-06-2009 02:51 AM

Using "OR" type matching pattern with grep command

I have a big text file and i want to grep only those lines who either have "word1" or "word2" or "word3".

How can i do it in a single command?
Currently i am doing this as 3 separate commands:
cat filename | grep word1
Cat filename | grep word2
Cat filename | grep word3

Ritesh Anand

syg00 05-06-2009 02:59 AM

You don't need the cat - try it like this

grep -E "word1|word2|word3" filename
or ...
grep -E "word[123]" filename

The latter requires very specific search fields.

jschiwal 05-06-2009 03:44 AM

You can also use "grep -e word1 -e word2 -e word3".

This is more useful in filtering out unwanted lines from a locate command:
grep -v -e /usr/src -e /mnt/ -e pattern1 -e pattern2

riteshanand40 05-06-2009 04:48 AM

Thank you very much syg00 and jschiwal.
Although i find the former more easy.

All times are GMT -5. The time now is 11:11 PM.