I want to grep 2 patterns in a file on Solaris UNIX.
That is grep 'pattern1 OR pattern2' filename.
The following command does NOT work:
grep 'pattern1\|pattern2' filename
What is wrong with this command?
NOTE: I am on Solaris
I want to grep 2 patterns in a file on Solaris UNIX.
That is grep 'pattern1 OR pattern2' filename.
The following command does NOT work:
grep 'pattern1\|pattern2' filename
What is wrong with this command?
NOTE: I am on Solaris
What operating system are you on?
It will work with on systems with GNU grep, but on BSD, Solaris, etc.,
\|
is not supported.Try
egrep
orgrep -E
, e.g.That command works fine for me. Please add additional information such as your platform and the exact regular expression and file contents you're using (minimized to the smallest example that still reproduces the issue). (I would add a comment to your post but don't have enough reputation.)
That depends entirely on what pattern1 and pattern2 are. If they're just words, it should work, otherwise you'll need:
If you want POSIX functionality (i.e. Linux-like behavior) you can put the POSIX 2-compatible binaries at the beginning of your path:
There is also /usr/xpg6 which is POSIX 1 compatible.
That should be correct. Make sure that you do or don't add the appropriate spaces i.e. "pattern1\|pattern2" vs "pattern1\| pattern2".
Are you sure you aren't just having problems with cases or something? try the -i switch.
An arcane method using
fgrep
(ie: fixed strings) that works on Solaris 10...Provide a pattern-list, with each pattern separated by a NEWLINE, yet quoted so as to be interpreted by the shell as one word:-
This method also works for
grep
,fgrep
andegrep
in/usr/xpg4/bin
, although the pipe-delimited ERE in anyegrep
is sometimes the least fussy.You can insert arbitrary newlines in a string if your shell allows history-editing, eg: in
bash
issueC-v C-j
in either emacs mode or vi-command mode.