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
How to invert a grep expression
提问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 -v
or --invert-match
,
使用命令行选项-v
或--invert-match
,
ls -R |grep -v -E .*[\.exe]$\|.*[\.html]$
回答by darioo
回答by Rob Sobers
Add the -v
option to your grep
command 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 -v
option to grep
. Let me add the (hopefully amusing) note that you could have figured this out yourself by grepping through the grep
help text:
正如多次所述,反转是通过-v
选项实现的grep
。让我添加(希望是有趣的)注释,您可以通过搜索grep
帮助文本来自己解决这个问题:
grep --help | grep invert
-v, --invert-match select non-matching lines
-v, --invert-match 选择不匹配的行