我熟悉bash中的shell编程,但出于某种原因egrep -o
仅打印匹配的单词不起作用,并显示如下错误。
环境是putty上的ksh unix控制台而不是linux或ubuntu终端......任何建议都表示赞赏!
终端输入&输出:
AB12 $ echo "i am a boy" | grep -w "am"
i am a boy
AB12 $ echo "i am a boy" | egrep -o "am"
egrep: illegal option -- o
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...
AB12 $ echo$
ksh: echo$: not found
AB12 $ echo $SHELL
/bin/ksh
AB12 $ echo "i am a boy" | grep -o "am"
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .
AB12 $
PS:类似的帖子但已尝试过: Can grep show only words that match search pattern?
答案 0 :(得分:12)
我假设这是您要连接的Solaris盒子。 Solaris的grep
版本没有-o
选项。所以你可以
/usr/sfw/bin
中,或者您可能运气好pkg install //solaris/text/gnu-grep
);或在我的方框中看到:
$ 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
答案 1 :(得分:5)
如果你有perl
:
echo "I am a boy" | perl -lne '/am/ && print $&'
am