bash 如何使用 grep 对多个文本字符串进行反向搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20591383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:00:26  来源:igfitidea点击:

How do I Invert search using grep for multiple strings of text

bashshellgrep

提问by user2476714

I am trying filter out couple of blocks of text that keep repeating over and over in my log file. For eg;

我正在尝试过滤掉在我的日志文件中不断重复的几个文本块。例如;

grep -v ("string one that I don't want" \| "string two that I don't want") file.log

I tried several variations of this and tried tweaking the white spaces . Sometimes it will filter out the first string sometimes neither. What will be the correct format to filter out more than one block of text using grep ?

我尝试了几种变体并尝试调整空格。有时它会过滤掉第一个字符串,有时两者都不会。使用 grep 过滤掉多个文本块的正确格式是什么?

回答by anubhava

You can use -eoption multiple times in grepto skip multiple search items:

您可以-e多次使用选项grep来跳过多个搜索项:

grep -v -e "string one that I don't want" -e "string two that I don't want" file.log

OR else use regexusing egrep

或其他使用regex使用egrep

egrep -v 'string one|string two' file.log