Linux 如何反转grep表达式

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

How to invert a grep expression

regexlinuxgrep

提问by sMaN

The following grep expression successfully lists all the .exe and .html files in the current directory and sub directories.

以下 grep 表达式成功列出了当前目录和子目录中的所有 .exe 和 .html 文件。

ls -R |grep -E .*[\.exe]$\|.*[\.html]$  

How do I invert this result to list those that aren't a .html or .exe instead. (That is, !=.)

我如何反转这个结果以列出那些不是 .html 或 .exe 的结果。(也就是说,!=。)

采纳答案by Eric Fortis

Use command-line option -vor --invert-match,

使用命令行选项-v--invert-match

ls -R |grep -v -E .*[\.exe]$\|.*[\.html]$

回答by darioo

grep -v

or

或者

grep --invert-match

You can also do the same thing using find:

您也可以使用find以下方法做同样的事情:

find . -type f \( -iname "*" ! -iname ".exe" ! -iname ".html"\)

More info here.

更多信息在这里

回答by Rob Sobers

Add the -voption to your grepcommand to invert the results.

-v选项添加到您的grep命令以反转结果。

回答by Anja Ishmukhametova

 grep "subscription" | grep -v "spec"  

回答by jmd_dk

As stated multiple times, inversion is achieved by the -voption to grep. Let me add the (hopefully amusing) note that you could have figured this out yourself by grepping through the grephelp text:

正如多次所述,反转是通过-v选项实现的grep。让我添加(希望是有趣的)注释,您可以通过搜索grep帮助文本来自己解决这个问题:

grep --help | grep invert

-v, --invert-match select non-matching lines

-v, --invert-match 选择不匹配的行