开发者

Solaris equivalent of -o option of grep on Linux

I run the following on Sun Solaris — it runs OK on Linux but not on Sun Solaris:

name="(WORD = (TCPIP = (PROTOCOL = TCP)(WORD = ALIAS_NAME)(PORT = 10234))"
echo $name | grep -o "(WORD = (TCPIP = (PROTOCOL = TCP)(WORD 开发者_开发问答= ALIAS_NAME)(PORT =  10234))"
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

My question is which option on Sun Solaris does the same task as the option grep -o (to match string capture) on Linux?


Solaris grep doesn't seem to have such an option. If you just need this to run on some Solaris boxes, perhaps they have GNU grep installed? (E.g. this one has it under /usr/local/gnu/bin/grep).

If you need this to run under any Solaris, you cannot use grep. Perhaps sed and awk can be used?


Sun's^W^WOracle's grep doesn't do that. You need to download the GNU grep version, preferably from sunfreeware.com.


then you can use nawk the "old school" way. Go over each word and check against your patterns

nawk '{
  for(i=1;i<=NF;i++){
      if($i == "your pattern") {
           print $i
      }
  }
}' file


Within Solaris, (OpenSolaris, OpenIndiana, etc.) you should use a command like this:

find . | xargs grep 'somestring'

That will get you what you are after.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜