在bash中,如何在一个文件中找到一个模式,而与另一个文件的任何行不匹配?
我如何在一个文件中找到一个模式,该模式与我知道GREP具有-f选项的另一个文件的任何行匹配
,因此,我可以将其馈送为模式的文件,而不是给GREP添加模式。
(aa是我的主文件)
user@system:~/test# cat a.a
Were Alexander-ZBn1gozZoEM.mp4
Will Ate-vP-2ahd8pHY.mp4
(pp是我的模式文件)
user@system:~/test# cat p.p
ZBn1gozZoEM
0maL4cQ8zuU
vP-2ahd8pHY
,因此命令可能是
somekindofgrep pp aa
,但它应该给出0mal4cq8zuu
,它是该模式的模式模式文件,PP,与文件AA中的任何内容都不匹配,
我不确定要做什么命令。
$grep -f p.p a.a<ENTER>
Were Alexander-ZBn1gozZoEM.mp4
Will Ate-vP-2ahd8pHY.mp4
$
我知道,如果AA中有一条其他行与PP中的任何模式不匹配,则grep -f pp aa
不会显示它。如果我做grep -v -v -f pp aa
,那么它只会显示AA行,而在PP中不匹配
,但我有兴趣在(我的模式文件)中找到什么模式PP不匹配AA!
我看着让Grep Print缺少查询,但他想要两个文件中的所有内容。而且,那里的一个答案之一提到-v,但我看不到适用于我的案件的答案,因为-v显示了不匹配任何模式的文件的行。因此,拥有或没有-v不会帮助我,因为我正在寻找与文件的任何行不匹配的模式。
How can I find a pattern in one file that doesn't match any line of another file
I'm aware that grep has a -f option, so instead of feeding grep a pattern, I can feed it a file of patterns.
(a.a is my main file)
user@system:~/test# cat a.a
Were Alexander-ZBn1gozZoEM.mp4
Will Ate-vP-2ahd8pHY.mp4
(p.p is my file of patterns)
user@system:~/test# cat p.p
ZBn1gozZoEM
0maL4cQ8zuU
vP-2ahd8pHY
So the command might be something like
somekindofgrep p.p a.a
but it should give 0maL4cQ8zuU
which is the pattern in the file of patterns, p.p, that doesn't match anything in the file a.a
I am not sure what command to do.
$grep -f p.p a.a<ENTER>
Were Alexander-ZBn1gozZoEM.mp4
Will Ate-vP-2ahd8pHY.mp4
$
I know that if there was an additional line in a.a not matched by any pattern in p.p, then grep -f p.p a.a
won't show it. And if I do grep -v -f p.p a.a
then it'd only show that line of a.a, not matched in p.p
But i'm interested in finding what pattern in (my file of patterns) p.p doesn't match a.a!
I looked at Make grep print missing queries but he wants everything from both files. And also, one of the answers there mentions -v but I can't quite see that applying to my case because -v shows the lines of a file that don't match any pattern. So having or not having -v won't help me there, because i'm looking for a pattern that doesn't match any line of a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
建议
awk
扫描aa
一次:script.awk
运行:
script.awk
testing:
pp pp
<代码> AA
测试:
Suggesting
awk
script that scansa.a
once:script.awk
running:
script.awk
Testing:
p.p
a.a
test:
自制脚本:
就像建议的User1934428一样,此脚本在文件
pp
中的模式上循环,并打印出文件aa
中未找到的任何模式。Home made script:
Like user1934428 suggested, this script loops on the patterns in file
p.p
and prints out any pattern that is not found in filea.a
.这是基于您要做的事情的一种可能解释的可能解决方案(
pp
与第一个- 以及
aa
的行中的最后一个。
):以上将使用每个Unix框中的任何Shell中的任何尴尬来稳健,便便且有效地工作。它将运行的数量级比当前的壳循环答案快,比现有的尴尬答案或XARGS答案快,并且无论任一个文件中存在哪个字符,包括REGEXP Metachars中的哪个字符,以及是否来自其中包括REGEXP Metachars,以及是否来自
pp
作为子字符串或其他上下文中存在于aa
中。无论输入文件中有什么问题,它也将具有零安全性问题。Here's a possible solution based on one possible interpretation of what it is you're trying to do (a full-string match on the lines in
p.p
against the substrings between the first-
and the last.
in the lines ina.a
):The above will work robustly, portably, and efficiently using any awk in any shell on every Unix box. It'll run orders of magnitude faster than the current shell loop answer, faster than the existing awk answer or the xargs answer, and will work no matter which characters exist in either file, regexp metachars included, and whether or not the search strings from
p.p
exist as substrings or in other contexts ina.a
. It also will have zero security concerns no matter what is in the input files.