How to use grep with two patterns?

2019-08-30 08:47发布

问题:

I have followed many links and reference documentation but still facing errors. I want to list all the words start or ending with 'sx'. I already found a solution which works fine:

awk '/^sx|sx$/' usr/dict/words

However I tried to use grep or egrep and neither worked. And then I made it simpler. Any word which contains 'ax' or 'sx', and again it did not work!

grep 'ax|sx' words
grep -E '^ax\|sx$' words
egrep '^ax\|sx$' words

Error:

grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .

Any suggestion appreciated.

回答1:

This should work for you:

egrep '(^ax|sx$)' usr/dict/words

NOTE:
I couldn't find any words ending in sx on the dictionary I've used.