I'm wondering if there is a replacement for using the | operator for an egrep expression for example say I wanted to match this pattern "out" or "outside" how can that be done without doing egrep "(out|side)" file
You can use
grep -e 'foo' -e 'bar' file
---
out
and outside
may be tricky because out
will also find matches for outside
, so you would need to use something extra in the search to make a distinction..
grep -F -e 'out' -e 'outside' somefile
Fixed strings (-F) eliminates problems with characters like *
or [
that can be taken as part of a regular expression. -F turns off regex interpretations and searches so you find exactly what you type inside the single quotes.
PS - the pipe character performs alternation (sort of like an or) and only works in regex mode. So
grep '\b(out|outside)\b' somefile
would be how to correctly use alternation for those two whole words.
Note that GNU grep uses the glibc RE engine that has got some perl extensions (PRE). \b is such an addition.
GNU grep needs an (also nonstandard) \|
for OR while |
is standard for ERE (egrep or grep -E).
So you can write either
grep -E '\b(out|outside)\b' somefile
or
grep '\b\(out\|outside\)\b' somefile
Both need GNU grep and glibc.
A bit more spread (but also no standard) are \<
and \>
(left and right word boundary) for grep (not egrep).
Standard (portable) are
grep -w -e "out" -e "outside"
grep -w "out
outside"
(multi-line)