advanced grep question
Robert Locke
lists at ralii.com
Thu Feb 2 15:53:06 UTC 2006
On Thu, 2006-02-02 at 09:39 -0600, 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
>
With basic regular expression you need to quote the "|" so it would be
something like this:
grep 'string1\|string2\|string3' file
The alternative would be to get in to extended regular expression with
egrep or grep -E which would look like:
egrep 'string1|string2|string3' file
HTH,
--Rob
More information about the fedora-list
mailing list