Thursday, September 19, 2013

grep regular expressions -OR-

To match word1 or word2 use this regexp:


word1|word2

for characters you have 2 options:
a|b
[ab]


In grep you either use:
grep "word1\|word2"
or
grep -E "word1|word2"


If you want to match 2 different word in grep:
grep -e "word1" -e "word2"


To match either word1 or word2 at the beginning of line and exclude word3 use this:
grep "\(^word1\)\|\(^word2\).*[^word3].*$"

which is (almost) the same as:
grep -e "^word1" -e "^word2" -v word3