System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

how to grep more than one word

SOLVED
Go to solution
yogesh kumar_2
Frequent Advisor

how to grep more than one word

hi,
How to grep the word starting with M and ?.I want to display the line starting with M and ?.I have tried the following command

egrep '(^M|^?)

But i am not getting the answer.

can any one help me out in this issue.


6 REPLIES
Ivan Krastev
Honored Contributor
Solution

Re: how to grep more than one word

Use:
grep -e "^M" -e "^?" filename


regards,
ivan
yogesh kumar_2
Frequent Advisor

Re: how to grep more than one word

Thank you very much ivan.
Sandeep_Chaudhary
Trusted Contributor

Re: how to grep more than one word

grep -e "^M" -e "^?"

oR

egrep "^M|^?"
Fredrik.eriksson
Valued Contributor

Re: how to grep more than one word

Are you guys sure that "^?" will work? Usually "?" tells regexp that the next token is optional. (Maybe this isn't true if there is no proceeding token in the regexp?)

To be sure anyway I would escape (\) the questionmark to make sure it matches.

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

Re: how to grep more than one word

>Fredrik: Usually "?" tells regexp that the next token is optional.

If grep on Linux is like grep on HP-UX, it takes regular expressions. And "?" is only special for Extended Regular Expressions, where it says the previous ERE is optional.

So you're probably correct in the egrep examples. For HP-UX you get this nice error:
egrep: ?, *, or + not preceded by valid regular expression
Mike Stroyan
Honored Contributor

Re: how to grep more than one word

egrep on linux does use extended regular expressions. The ? does need quoting.
egrep "^M|^\?"
or
egrep "^[M?]"
will work.

egrep can have surprisingly different performance for different locales on linux.
Some implementations are quite a bit slower for the default utf8 locales. They will run faster after setting "export LANG=C". Other implementations are buggy and run horribly slow in utf8 locales.