0

我正在尝试使用grep捕获包含两个句子之一的文件。

为了捕捉我使用的一句话

grep -L "Could not place marker for right window edge" *log

对于两个句子,看看它们中的任何一个是否存在于我尝试过的文件中

grep -L "Could not place marker for right window edge \| Could not place marker for left window edge" *log

但这不起作用。

对此有何建议?

4

3 回答 3

1

我怀疑您引入的起始和尾随空格导致了问题。尝试:

$ egrep -L 'this is sentence|another different sentence' *log

或者使用fgrep你只是在寻找固定字符串而不是正则表达式:

$ fgrep -Le 'this is sentence' -e 'another different sentence' *log

如果你的句子实际上是指线条,那么你可能也对这个-x论点感兴趣。

-x, --line-正则表达式

仅选择与整行完全匹配的匹配项。(-x 由 POSIX 指定。)

您正在使用-Lwhich 显示不匹配的文件这是您真正想要的还是您的意思是-l只显示匹配的文件名?

于 2013-09-21T19:39:49.897 回答
1

试试这 3 个变体:

grep -l 'this is sentence\|another different sentence' *log
grep -lE 'this is sentence|another different sentence' *log
grep -lE '(this is sentence|another different sentence)' *log

如果要查找匹配的文件,-L是不是正确的开关;而是使用-l, from man grep

-L, --files-without-match
Suppress normal output; instead print the name of each input file
from which no output would normally have been printed.  The scanning will
stop on the first match.

-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which
output would normally have been printed.  The scanning will stop on the first
match.  (-l is specified  by POSIX.)
于 2013-09-21T20:04:38.360 回答
0

使用awk

awk '/Could not place marker for right window edge|Could not place marker for left window edge/' *.log

或者这可以像这样完成

awk '/Could not place marker for (right|left) window edge/' *.log
于 2013-09-21T21:14:02.777 回答