[英]Find files whose content match a line from text file
I have a text file - accessions.txt
(below is a subset of this file):我有一个文本文件 -
accessions.txt
(下面是这个文件的一个子集):
KRO94967.1
KRO95967.1
KRO96427.1
KRO94221.1
KRO94121.1
KRO94145.1
WP_088442850.1
WP_088252850.1
WP_088643726.1
WP_088739685.1
WP_088283155.1
WP_088939404.1
And I have a directory with multiple files ( *.align
).我有一个包含多个文件的目录(
*.align
)。
I want to find the filenames ( *.align
) which content matches any line within my accessions.txt
text file.我想查找内容与我的
accessions.txt
文本文件中的任何行匹配的文件名 ( *.align
)。
I know that find. -exec grep -H 'STRING' {} +
我知道那个
find. -exec grep -H 'STRING' {} +
find. -exec grep -H 'STRING' {} +
works to find specific strings (eg replacing STRING with WP_088939404.1
returns every filename where the string WP_088939404.1
is present). find. -exec grep -H 'STRING' {} +
用于查找特定字符串(例如,将 STRING 替换为WP_088939404.1
返回存在字符串WP_088939404.1
的每个文件名)。
Is there a way to replace STRING
with "all strings inside my text file"?有没有办法用“我的文本文件中的所有字符串”替换
STRING
?
Or或者
Is there another (better) way to do this?还有另一种(更好的)方法可以做到这一点吗?
I was trying to avoid writing a loop that reads the content of all my files as there are too many of them.我试图避免编写一个循环来读取我所有文件的内容,因为它们太多了。
Many thanks!非常感谢!
grep
can take a list of patterns to match with -f
. grep
可以获取与-f
匹配的模式列表。
grep -lFf accessions.txt directory/*.align
-F
tells grep to interpret the lines as fixed strings, not regex patterns. -F
告诉 grep 将这些行解释为固定字符串,而不是正则表达式模式。
Sometimes, -w
is also needed to prevent matching inside words, eg有时,还需要
-w
来防止匹配内部单词,例如
abcd
might match not only abcd
, but also xabcd
or abcdy
.可能不仅匹配
abcd
,还xabcd
或abcdy
。 Sometimes, preprocessing the input list is needed to prevent unwanted matching if the rules are more complex.有时,如果规则更复杂,则需要预处理输入列表以防止不必要的匹配。
You're looking for grep's -f option.您正在寻找 grep 的 -f 选项。
find . -name '*.align' -exec grep -Fxqf accessions.txt {} \; -print
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.