Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何反转grep表达式_Regex_Linux_Grep - Fatal编程技术网

Regex 如何反转grep表达式

Regex 如何反转grep表达式,regex,linux,grep,Regex,Linux,Grep,以下grep表达式成功列出了当前目录和子目录中的所有.exe和.html文件 ls -R |grep -E .*[\.exe]$\|.*[\.html]$ 如何反转此结果以列出那些不是.html或.exe的结果。(即,!=)使用命令行选项-v或--inverse match ls -R |grep -v -E .*[\.exe]$\|.*[\.html]$ 或 您也可以使用查找执行相同的操作: find . -type f \( -iname "*" ! -iname ".exe" !

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

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

如何反转此结果以列出那些不是.html或.exe的结果。(即,
!=

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

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

您也可以使用
查找
执行相同的操作:

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

更多信息。

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

如多次所述,反转是通过
-v
选项实现的。让我补充一点(希望是有趣的),您可以通过grep
grep
帮助文本自己解决这个问题:

 grep "subscription" | grep -v "spec"  
grep --help | grep invert
-v、 --反转匹配选择不匹配的线


find
命令是解决此问题的最具语义的解决方案。为了达到这个目的,将
ls
grep
组合在一起,充其量也只是一种黑客行为。这应该是公认的答案。(+1)@Eric不管OP的要求如何,反转grep表达式的用处远远不止于查找文件。我怀疑这是大多数人来这里的原因。应该注意的是,
-v
/
--inverse match
不一定会翻转
grep
的返回码是否表示成功执行,而是会匹配不匹配的行。那些希望反转返回代码的人(即,如果所有行与模式不匹配,而不是至少一行,则成功)应该使用
!grep
。这在条件表达式中可用,例如:
if!ls | grep-qE“(\.exe)$”;然后在$(pwd)中回显No.exe文件;fi
。我无法实现上述功能,但这对我很有效:
ls-R | grep-v-E.*(\.exe)$\.*(\.vue)$”
 grep "subscription" | grep -v "spec"  
grep --help | grep invert