问题描述
我熟悉 bash 中的 shell 编程,但由于某种原因 egrep -o
仅打印匹配的单词不起作用并显示如下错误.
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.
环境是 putty 上的 ksh unix 控制台,而不是 linux 或 ubuntu 终端......感谢任何建议!
Environment is ksh unix console on putty and not linux or ubuntu terminal......any advice is appreciated!
终端输入&输出:
Terminal input & output :
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 : 类似的线程,但已经尝试过:grep 可以只显示匹配搜索模式的词吗?
PS : Similar thread but tried already : Can grep show only words that match search pattern?
推荐答案
我假设这是您要连接的 Solaris 设备.grep
的 Solaris 版本没有 -o
选项.所以你可以
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
- 在您的 Solaris 机器上安装 GNU grep(它可能已经安装在
/usr/sfw/bin
中,或者您可能会使用pkg install//solaris/text/gnu-grep
);或 - 改用 awk(参见这个问题)
- install the GNU grep on your Solaris box (it might already be installed in
/usr/sfw/bin
, or you might have luck withpkg install //solaris/text/gnu-grep
); or - use awk instead (see this SO question)
在我的盒子上看到:
$ 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
这篇关于通过 grep 仅打印匹配的单词,而不是整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!