my target is to match Exactly the string snmpmanager from hosts file on solaris & linux
the following command work on Linux (red-hat 5.1) but not for SunOS , please advice how to fit the syntax to solaris?
example from solaris OS
grep -icE '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
grep: illegal option -- E
after I fixed it to
egrep -i '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
or egrep -i '(^|[\s])snmpmanager($|\s])' /etc/hosts
or egrep -i '(^|[\t])snmpmanager($|\t])' /etc/hosts
but I don’t get any match output (but snmpmanager already defined in host file) ??
my host file
10.170.10.5 loghost
10.170.10.61 Master SyslogSer vip Tcc NtpServer1 NtpServer2 snmpManager snmpManagerPA1 snmpManagerPA2
I don't think the standard Solaris (e)grep understands the
[[:space:]]
syntax so you would have to use something likewhere
<-TAB->
is Ctrl-VTabIf you use /usr/xpg4/bin/egrep then it works as expected.
use
egrep -ic
instead ofgrep -icE
. Note that -i is making the match case insensitive, which may or may not be what you want depending on your definition of "Exactly"Traditionally (such as exists in Solaris) there were three alternate versions of
grep
: there was alsoegrep
,fgrep
, andrgrep
.According to POSIX, these three variants are now included as options to
grep
:egrep
is equivalent togrep -E
fgrep
is equivalent togrep -F
rgrep
is equivalent togrep -r
Thus, with your invocation, you want this:
(This assumes there aren't other errors.)
Solaris grep is not the GNU grep from coreutils, so the behavior in more complex cases is likely to be different.
One idea would be installing GNU grep. If you install the ggrep package from OpenCSW, it will install the GNU grep as
/opt/csw/bin/ggrep
with an additional symlink in/opt/csw/gnu/grep
.First you bootstrap pkgutil:
Now you can install GNU grep:
You can add
/opt/csw/gnu
to yourPATH
, which will allow you to use thegrep
command the same way as on Linux.If you want to use it your script, you can write:
The manpage of grep in Solaris 11 pointed to
/usr/xpg4/bin/grep
if we want to use the-E
I tried it and it worked in my case.