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

5 Answers 5


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
  • I find the solution for solaris its - grep -iwc snmpManager (only need to add w flag)
    – Eytan
    Feb 8, 2012 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"

  • 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, 2012 at 16:33
  • try replacing [[:space:]] with either \s or [ \t]
    – stew
    Feb 8, 2012 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, 2012 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, 2012 at 17:12
  • that [ \t] should have a space before the \t
    – stew
    Feb 8, 2012 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.)

  • I find the solution for solaris its - grep -iwc snmpManager (only need to add w flag)
    – Eytan
    Feb 8, 2012 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:

wget http://mirror.opencsw.org/opencsw/pkgutil.pkg
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

The manpage of grep in Solaris 11 pointed to /usr/xpg4/bin/grepif we want to use the -E I tried it and it worked in my case.

/usr/xpg4/bin/grep -E "DisplayConnect[[:space:]]*/etc/issue" /etc/proftpd.conf
DisplayConnect      /etc/issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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