Print only matching word, not entire line through grep

有些话、适合烂在心里 提交于 2019-12-01 02:25:46
damienfrancois

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

If you have perl :

echo "I am a boy" | perl -lne '/am/ && print $&'
am
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!