Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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      loghost   Master SyslogSer vip Tcc NtpServer1 NtpServer2 snmpManager snmpManagerPA1 snmpManagerPA2
share|improve this question
up vote 3 down vote accepted

I don't think the standard Solaris (e)grep understands the [[:space:]] syntax so you would have to use something like

egrep -i (^| |<-TAB->)snmpmanager($| |<-TAB->)

where <-TAB-> is Ctrl-VTab

If you use /usr/xpg4/bin/egrep then it works as expected.

/usr/xpg4/bin/egrep -i '([[:space:]])snmpmanager($|[[:space:]])'  /etc/hosts
share|improve this answer
I find the solution for solaris its - grep -iwc snmpManager (only need to add w flag) – Eytan Feb 8 '12 at 17:54

use egrep -ic instead of grep -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"

share|improve this answer
I try this on my solaris --> egrep -ic '(^|[[:space:]])snmpManager($|[[:space:]])' /etc/hosts 0 (why this not match the snmpManager string that already defined in host file – Eytan Feb 8 '12 at 16:33
try replacing [[:space:]] with either \s or [ \t] – stew Feb 8 '12 at 16:37
Also check the options: do a man egrep on Solaris and check to see what they do. Make sure that your invocation is using the right options. – Mei Feb 8 '12 at 16:43
egrep -i '(^|[\t])snmpmanager($|\t])' /etc/hosts or egrep -i '(^|[\s])snmpmanager($|\s])' /etc/hosts not give any output also -:( – Eytan Feb 8 '12 at 17:12
that [ \t] should have a space before the \t – stew Feb 8 '12 at 17:15

Traditionally (such as exists in Solaris) there were three alternate versions of grep: there was also egrep, fgrep, and rgrep.

According to POSIX, these three variants are now included as options to grep:

  • egrep is equivalent to grep -E
  • fgrep is equivalent to grep -F
  • rgrep is equivalent to grep -r

Thus, with your invocation, you want this:

egrep -ic '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts

(This assumes there aren't other errors.)

share|improve this answer
I find the solution for solaris its - grep -iwc snmpManager (only need to add w flag) – Eytan Feb 8 '12 at 17:54

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:

pkgadd -d pkgutil.pkg
pkgutil -U

Now you can install GNU grep:

pkgutil -y -i ggrep

You can add /opt/csw/gnu to your PATH, which will allow you to use the grep command the same way as on Linux.

If you want to use it your script, you can write:

/opt/csw/bin/ggrep -icE '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.