string 没有字符串的grep

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

grep without string

stringgrep

提问by Mika H.

I want to find all the lines in my text file containing the string "abc", but not containing the string "def". Can I use the grepcommand to accomplish this task?

我想在我的文本文件中找到包含字符串"abc"但不包含字符串的所有行"def"。我可以使用grep命令来完成这个任务吗?

回答by sampson-chen

Either of the these will do:

这些中的任何一个都可以:

grep -v "def" input_file | grep "abc"

or

或者

grep "abc" input_file | grep -v "def"

The following will also preserve coloring if you only want to see the output on stdout:

如果您只想在 stdout 上查看输出,以下内容也将保留着色:

grep --color=always "abc" input_file | grep -v "def"

The -voption (stands for "invert match") tells grepto ignore the lines with the specified pattern - in this case def.

-v选项(代表“反转匹配”)告诉grep忽略具有指定模式的行 - 在这种情况下def

回答by alex

This might do it.

这可能会做到。

fgrep "abc" file | grep -v "def"