简体   繁体   English

从文本文件中查找内容与某行匹配的文件

[英]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 ,还xabcdabcdy 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.

相关问题 从部分名称在文本文件中的文件夹中选择wav文件 - Select wav files from a folder whose partial names are in a text file 如何遍历两个文件并逐行查找file1中匹配file2的所有匹配项,然后替换为file3中的内容 - How to iterate over two files and find all occurrences in file1 matching file2, line by line, then replace with content from file3 如何查找和串联字符不同但根目录在文本文件中列出的文件? - How can I find and concatenate files that have varying characters but whose root is listed in a text file? 匹配两个文件中的字符串,并在第一个文件中向第二个文件的行尾添加匹配字符串 - Match strings from two files and append line with matching string from first file to end of line of second file 查找名称包含“ AC”的文件以及名称以“ RES”开头的文件列表 - Find files whose name contains “AC” and the list of file whose name starts with “RES” 在linux命令行中使用find和grep来搜索具有特定用户和文本内容的文件? - Using find and grep in linux command line to search for files with a specific user and text content? 列出所有内容可以与多个字符串中的任何一个匹配的文件名的最快方法 - Fastest way to list all the file names whose content can match with any of multiple strings 在文件中查找文本并获取所需的内容 - Find text in files and get the needed content Linux,大文本文件,将内容从A行删除到B行 - Linux, big text file, strip out content from line A to line B 如何在多行中查找包含内容的文件? - How to find file with content in multi line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM