我的命令每当时都会打印出两行FW_6.0.0找到了,下面是代码:
grep -oP 'FW_6.0.0, (.*)$' file
下面是输出,两者具有相同的值FW_6.0.0
FW_6.0.0, SUCCESS
FW_6.0.0, OK
我想匹配两个单词,那就是FW_6.0.0和成功可以在同一行找到它,以便打印出来:
FW_6.0.0, SUCCESS
并消除FW_6.0.0, OK
答案1
尝试使用双引号""
:
grep -oP "FW_6.0.0, SUCCESS" file
OR(因为它是固定字符串,而不是模式):
grep -oF "FW_6.0.0, SUCCESS" file
来自 grep 手册页:
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by
newlines, any of which is to be matched. (-F is specified by
POSIX.)
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression. This is highly
experimental and grep -P may warn of unimplemented features.
答案2
如果你想使用awk
:
awk '/FW_6\.0\.0/ && /SUCCESS/' file
答案3
尝试:
grep -o 'FW_6.0.0.*SUCCESS' file
-P
我们这里不需要选项。
答案4
通过sed,
$ sed -n '/FW_6\.0\.0.*SUCCESS/p' file
FW_6.0.0, SUCCESS