我有一个文件,例如"queries.txt",它有硬回包分隔的字符串.我想使用此列表在第二个文件"biglist.txt"中查找匹配项.
"biglist.txt"可能在"queries.txt"中为每个字符串添加多个匹配项.我想只返回每个查询的第一个匹配并将其写入另一个文件.
grep -m 1 -wf queries.txt biglist.txt>输出
只给我输出一行.我应该输出与queries.txt相同的行数.
有什么建议吗?非常感谢!我搜索了过去的问题,但在几分钟的阅读后没有找到一个完全相同的案例.
如果你想在每个文件后"重置计数器",你可以这样做
cat queries.txt | xargs -I{} grep -m 1 -w {} biglist.txt > output
这用于xargs
为grep
输入中的每一行调用一次......应该为你做的伎俩.
说明:
cat queries.txt - produce one "search word" per line xargs -I{} - take the input one line at a time, and insert it at {} grep -m 1 -w - find only one match of a whole word {} - this is where xargs inserts the search term (once per call) biglist.txt - the file to be searched > output - the file where the result is to be written