2

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
Eytan
  • 611
  • 6
  • 13
  • 27

5 Answers5

4

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
user9517
  • 115,471
  • 20
  • 215
  • 297
1

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"

stew
  • 9,388
  • 1
  • 30
  • 43
  • 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 08 '12 at 16:33
  • try replacing `[[:space:]]` with either `\s` or `[ \t]` – stew Feb 08 '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 08 '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 08 '12 at 17:12
  • that `[ \t]` should have a space before the \t – stew Feb 08 '12 at 17:15
  • I find the solution for solaris its - grep -iwc snmpManager (only need to add w flag) – Eytan Feb 08 '12 at 18:10
1

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.)

Mei
  • 4,590
  • 8
  • 45
  • 53
1

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
automaciej
  • 426
  • 1
  • 6
  • 11
0

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
chicks
  • 3,793
  • 10
  • 27
  • 36
rav
  • 54
  • 5