Linux grep 排除多个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16212656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep exclude multiple strings
提问by Jeets
I am trying to see a log file using tail -f
and want to exclude all lines containing the following strings:
我正在尝试使用 tail 查看日志文件,-f
并希望排除包含以下字符串的所有行:
"Nopaging the limit is"` and `"keyword to remove is"
I am able to exclude one string like this:
我可以像这样排除一个字符串:
tail -f admin.log|grep -v "Nopaging the limit is"
But how do I exclude lines containing either of string1
or string2
.
但是如何排除包含string1
或 的行string2
。
回答by Stefan Podkowinski
egrep -v "Nopaging the limit is|keyword to remove is"
回答by hs.chandra
tail -f admin.log|grep -v -E '(Nopaging the limit is|keyword to remove is)'
回答by rezizter
Another option is to create a exclude list, this is particulary usefull when you have a long list of things to exclude.
另一种选择是创建一个排除列表,这在您要排除的内容列表很长时特别有用。
vi /root/scripts/exclude_list.txt
Now add what you would like to exclude
现在添加您要排除的内容
Nopaging the limit is
keyword to remove is
Now use grep to remove lines from your file log file and view information not excluded.
现在使用 grep 从文件日志文件中删除行并查看未排除的信息。
grep -v -f /root/scripts/exclude_list.txt /var/log/admin.log
回答by Eric Leschinski
Two examples of filtering out multiple lines with grep:
使用 grep 过滤掉多行的两个示例:
Put this in filename.txt
:
把这个放在filename.txt
:
abc
def
ghi
jkl
grep command using -E option with a pipe between tokens in a string:
grep 命令使用 -E 选项和字符串中标记之间的管道:
grep -Ev 'def|jkl' filename.txt
prints:
印刷:
abc
ghi
Command using -v option with pipe between tokens surrounded by parens:
使用 -v 选项和由括号包围的令牌之间的管道的命令:
egrep -v '(def|jkl)' filename.txt
prints:
印刷:
abc
ghi
回答by mikhail
You can use regular grep like this:
您可以像这样使用常规的 grep:
tail -f admin.log | grep -v "Nopaging the limit is\|keyword to remove is"
tail -f admin.log | grep -v "Nopaging the limit is\|keyword to remove is"
回答by Fidel
The greps can be chained. For example:
grep 可以链接起来。例如:
tail -f admin.log | grep -v "Nopaging the limit is" | grep -v "keyword to remove is"
回答by wisbucky
grep -Fv -e 'Nopaging the limit is' -e 'keyword to remove is'
-F
matches by literal strings (instead of regex)
-F
通过文字字符串匹配(而不是正则表达式)
-v
inverts the match
-v
反转比赛
-e
allows for multiple search patterns (all literal and inverted)
-e
允许多种搜索模式(所有文字和倒排)