Print only matching word, not entire line through grep

后端 未结 2 1941
失恋的感觉
失恋的感觉 2021-01-07 21:35

I am familiar with shell programming in bash, but for some reason egrep -o to print only matching words is not working and displays error as below.

Envi

2条回答
  •  情深已故
    2021-01-07 22:08

    I am assuming this is a Solaris box you are connecting to. Solaris' version of grep does not have the -o option. So you can either

    • install the GNU grep on your Solaris box (it might already be installed in /usr/sfw/bin, or you might have luck with pkg install //solaris/text/gnu-grep); or
    • use awk instead (see this SO question)

    See on my box:

    $ uname
    SunOS
    $  echo "i am a boy" | grep -o "am"
    grep: illegal option -- o
    Usage: grep -hblcnsviw pattern file . . .
    $  echo "i am a boy" | /usr/sfw/bin/ggrep -o "am"
    am
    

提交回复
热议问题