我的目标是在solaris和linux上匹配来自主机文件的stringsnmpmanager
以下命令在Linux(红帽子5.1)上工作,但不适用于SunOS,请咨询如何使语法适合solaris?
来自solaris OS的例子
grep -icE '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts grep: illegal option -- E
我固定之后
egrep -i '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts or egrep -i '(^|[\s])snmpmanager($|\s])' /etc/hosts or egrep -i '(^|[\t])snmpmanager($|\t])' /etc/hosts
但我没有得到任何匹配输出(但已经在主机文件中定义了snmpmanager)?
我的主机文件
10.170.10.5 loghost 10.170.10.61 Master SyslogSer vip Tcc NtpServer1 NtpServer2 snmpManager snmpManagerPA1 snmpManagerPA2
我不认为标准的Solaris(e)grep理解[[:space:]]
语法,所以你将不得不使用类似
egrep -i (^| |<-TAB->)snmpmanager($| |<-TAB->)
其中<-TAB->
是Ctrl-V Tab
如果您使用/ usr / xpg4 / bin / egrep,则按预期工作。
/usr/xpg4/bin/egrep -i '([[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
使用egrep -ic
而不是grep -icE
。 请注意,-i正在使匹配大小写不敏感,根据“Exactly”的定义,这可能是也可能不是您想要的,
传统上(如Solaris中存在)有三个替代版本的grep
: egrep
, fgrep
和rgrep
。
根据POSIX,这三个变体现在包含在grep
:
egrep
相当于grep -E
fgrep
相当于grep -F
rgrep
相当于grep -r
因此,在您的调用中,您需要这样做:
egrep -ic '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
(这个假设没有其他错误。)
Solaris grep不是来自coreutils的GNU grep,所以在更复杂的情况下,行为可能会有所不同。
一个想法是安装GNU grep。 如果你从OpenCSW安装ggrep包,它将把/opt/csw/bin/ggrep
GNU grep安装为/opt/csw/bin/ggrep
并在/opt/csw/gnu/grep
附加一个符号链接。
首先你bootstrap pkgutil:
wget http://mirror.opencsw.org/opencsw/pkgutil.pkg pkgadd -d pkgutil.pkg pkgutil -U
现在你可以安装GNU grep:
pkgutil -y -i ggrep
您可以将/opt/csw/gnu
添加到您的PATH
,这将允许您以与Linux上相同的方式使用grep
命令。
如果你想使用它的脚本,你可以写:
/opt/csw/bin/ggrep -icE '(^|[[:space:]])snmpmanager($|[[:space:]])' /etc/hosts
如果我们想要使用-E
,Solaris 11中的grep的联机帮助页指向/usr/xpg4/bin/grep
我试过了,它在我的情况下工作。
/usr/xpg4/bin/grep -E "DisplayConnect[[:space:]]*/etc/issue" /etc/proftpd.conf DisplayConnect /etc/issue