Operating System - HP-UX
1832363 Members
2441 Online
110041 Solutions
New Discussion

Re: Grep for Multiple Strings

 
SOLVED
Go to solution
Global Server Operation
Frequent Advisor

Grep for Multiple Strings

How do use grep to search for multiple strings?

OS=11.i
All replies are appreciated.
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Grep for Multiple Strings

grep -E -e 'Mickey' -e 'Minnie' -e 'Pluto' < myfile

Man grep for details (of course, if you had done that you wouldn't have needed to ask).
If it ain't broke, I can fix that.
Jeff_Traigle
Honored Contributor

Re: Grep for Multiple Strings

You can use the -e option on grep.

grep -e this -e that filename

This would return lines containing either "this" or "that".
--
Jeff Traigle
IT_2007
Honored Contributor

Re: Grep for Multiple Strings

you can use egrep -i "string1|string2|string3"

...
Deepak Kulkarni
Regular Advisor

Re: Grep for Multiple Strings

Hi,



egrep -i 'text1|text2' File


Cheers
DK
spex
Honored Contributor

Re: Grep for Multiple Strings

# grep -E 's1|s2|s3'
Jaime Bolanos Rojas.
Honored Contributor

Re: Grep for Multiple Strings

VOM,

According to man pages:

"-e expression Same as a simple expression argument, but useful when the expression begins with a hyphen (-). Multiple -e options can be used to specify multiple patterns; an input line is selected if it matches any of the specified patterns."

Regards,

Jaime
Work hard when the need comes out.
A. Clay Stephenson
Acclaimed Contributor

Re: Grep for Multiple Strings

I should add that the grep -E (enhanced) may well be a better choice because unlike plain grep (without the -E) you can do much more powerful Regular Expression matches rather than simple strings.
If it ain't broke, I can fix that.
Global Server Operation
Frequent Advisor

Re: Grep for Multiple Strings

Thanks for all replies. I have found each reply useful.