advanced grep question

Joachim Backes [RHRK] joachim.backes at rhrk.uni-kl.de
Thu Feb 2 15:42:35 UTC 2006


Steven J Lamb wrote:
> what I am looking for is information on how to use grep in the following 
> way. I want to do essentially this
> 
> grep (string1|string2|string3) filename
> 
> I would use this to search filename for either string1, string2 or 
> string 3 instances
> 
> I realize I could do the following but would like to use it as one grep 
> statement as I believe it should be more efficient.
> 
>> tmp;
> 
> grep string1 filename >>tmp;
> grep string2 filename >>tmp;
> grep string3 filename >>tmp
> 
> as this would be defined in a normal regular expression. grep claims to 
> be able to use this functionality as best I can tell ... it is 
> documented in the man page and it says
> 
> Two  regular  expressions  may be joined by the infix operator |; the 
> resulting regular expression matches
>       any string matching either subexpression.
> 
> I have not been able to find an example of this anywhere and have tried 
> several forms of the syntax but no luck. my command interpreter keeps 
> trying to use the pipe instead of letting grep have it.
> 
> any one have any thoughts
> 
> thanks
> 
> 
> Steven Lamb

Use egrep:

egrep "(string1|string2|string3)" filename

does exactly this.

-- 
Joachim Backes <joachim.backes at rhrk.uni-kl.de>
Registered Linux user #407492
University of Kaiserslautern,Computer Center [RHRK],
Systems and Operations, High Performance Computing,
D-67653 Kaiserslautern, PO Box 3049, Germany
--------------------------------------------------
Phone: +49-631-205-2438, FAX: +49-631-205-3056
http://hlrwm.rhrk.uni-kl.de/home/staff/backes.html




More information about the fedora-list mailing list