Solaris - grep with OR functionality

I want grep 2 patterns in a file on Solaris UNIX .

This file name is grep 'pattern1 OR pattern2'.

The following command does NOT work:

grep 'pattern1\|pattern2' filename

What is wrong with this team?

NOTE: I am on Solaris

+6
source share
6 answers

What operating system do you work on?

It will work with systems with GNU grep, but on BSD, Solaris, etc. \| not supported.

Try egrep or grep -E , for example

 egrep 'pattern1|pattern2' 
+15
source

If you want POSIX functionality (for example, behavior similar to Linux), you can put POSIX 2-compatible binaries at the beginning of your path:

 $ echo $PATH /usr/xpg4/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:[...] 

There is also / usr / xpg 6 compatible with POSIX 1.

 /usr/bin: SVID/XPG3 /usr/xpg4/bin: POSIX.2/POSIX.2a/SUS/SUSv2/XPG4 /usr/xpg6/bin: POSIX.1-2001/SUSv3 
+2
source

This command works great for me. Add more information such as your platform and the exact regular expression and contents of the file you are using (minimized to the smallest example that still reproduces the problem). (I would add a comment to your post, but did not have a sufficient reputation.)

0
source

That should be right. Make sure you do or not add the appropriate spaces, that is, "pattern1 \ | pattern2" and "pattern1 \ | pattern2".

Are you sure that you are not just having problems with business or something else? try the -i switch.

0
source

It completely depends on what pattern1 and pattern2 are. If these are just words, they should work, otherwise you will need:

 grep '\(pattern1\)\|\(pattern2\)' 
0
source

Secret method using fgrep (i.e. fixed strings) that works on Solaris 10 ...

Provide a list of templates, each of which is separated by NEWLINE, but is displayed in such a way as to interpret the shell as one word: -

 fgrep 'pattern1 pattern2' filename 

This method also works for grep , fgrep and egrep in /usr/xpg4/bin , although ERE in any of the channel-limited /usr/xpg4/bin is sometimes the least fussy.

You can insert arbitrary strings into a string if your shell allows you to edit history, for example: in bash the Cv Cj problem is in emacs mode or in vi mode.

0
source

Source: https://habr.com/ru/post/887494/


All Articles