'grep -v -l' 等价于 'grep -L' 吗?

don*_*sti 6 grep

每隔一段时间就会有一个答案(或评论)建议一起使用grep's-v-lswitch 而不是 the -L(尤其是当后者不可用时)。作者似乎相信它们是等价的。

那么,它们可以互换吗?IOW,是否grep -v -l产生与grep -L?相同的结果?

don*_*sti 10

不,它们不是等价的,因此结果几乎总是1不同。
让我们看看每个开关的作用:

‘-L’
‘--files-without-match’
     Suppress normal output; instead print the name of each input file
     from which no output would normally have been printed.
Run Code Online (Sandbox Code Playgroud)

这应该很简单:
grep -L 'pattern' ./*
打印不包含任何行匹配的每个文件的名称'pattern'

‘-v’
‘--invert-match’
     Invert the sense of matching, to select non-matching lines.
Run Code Online (Sandbox Code Playgroud)

这也应该非常简单:
grep -v 'pattern' ./*
打印'pattern'每个文件中不匹配的所有行。

‘-l’
‘--files-with-matches’
     Suppress normal output; instead print the name of each input file
     from which output would normally have been printed.
Run Code Online (Sandbox Code Playgroud)

再次,简单:
grep -l 'pattern' ./*
打印包含至少一行匹配的每个文件的名称'pattern'

现在,当组合-v和时会发生什么-l?它与 没有太大区别grep -l,除了它选择不匹配的行,因此
grep -vl 'pattern' ./*
打印包含至少一行不匹配的每个文件的名称'pattern'


所以,注意区别:

grep -L打印文件名唯一的,如果没有线匹配 'pattern'
grep -vl打印文件名只有当至少有一个行不匹配 'pattern'


1:
基于上述,很容易看出这两个命令何时会产生相同的输出: -
当文件不为空并且所有行都匹配模式时 - 在这种情况下将没有输出
- 或者当文件是不为空且没有与模式匹配的行 - 在这种情况下,它将被列出


故事寓意:永远不要grep -vl模仿grep -L
如果您grep不支持-L这是模仿它的正确方法