Print only matching word, not entire line through grep

后端 未结 2 1924
失恋的感觉
失恋的感觉 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
    
    0 讨论(0)
  • 2021-01-07 22:26

    If you have perl :

    echo "I am a boy" | perl -lne '/am/ && print $&'
    am
    
    0 讨论(0)
提交回复
热议问题