I'm trying to egreg for a couple strings whcih are (0) and SYSTEM. The problem is the syntax for egreg is:
egreg "(string1|string2)"
With my basic knowledge of UNIX I don't know how to include '(0)' within "(string1|string2)" apart from trying to use single quotes which doesn't work, i.e. "('(0)')".
I reckon this must be eary to resolve but need some expert advice.
Whilst you're seeing what you think is the correct output that's because it's actually greping for 0 and SYSTEM
# cat tmp
hai it is the begining
this is test (0) for unix.com
also to test SYSTEM for the same
this is the end of the file
this is test 0 for unix.com
If I want to exclude lines including (0) using your syntax I get the following result:
# egrep -v '(0)|SYSTEM' tmp
hai it is the begining
this is the end of the file
Line 5 should not have been removed if the pattern is (0)
You have to escape the '(' and the ')'. Like this:
$ cat test
hai it is the begining
this is test (0) for unix.com
also to test SYSTEM for the same
this is the end of the file
this is test 0 for unix.com
$ egrep -v '\(0\)|SYSTEM' test
hai it is the begining
this is the end of the file
this is test 0 for unix.com
Here is some food for thought. I wanted to do this without the quotes, so I fiddled around and got this:
How are
egrep -v '\(0\)|SYSTEM' test
and
egrep -v \\\(0\\\)\|SYSTEM test
the same?
Ran the two on FreeBSD 4.6 under ksh. Check the output:
$ cat test
hai it is the begining
this is test (0) for unix.com
also to test SYSTEM for the same
this is the end of the file
this is test 0 for unix.com
$ egrep -v '\(0\)|SYSTEM' test
hai it is the begining
this is the end of the file
this is test 0 for unix.com
$ egrep -v \\\(0\\\)\|SYSTEM test
hai it is the begining
this is the end of the file
this is test 0 for unix.com
I dont need the explaination about having to escape the '|'. Its the multiple \s.