gpt4 book ai didi

grep 使用列表查找文件中的匹配项,并仅打印列表中每个字符串的第一次出现

转载 作者:行者123 更新时间:2023-12-01 06:29:26 24 4
gpt4 key购买 nike

我有一个文件,例如“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

关于grep 使用列表查找文件中的匹配项,并仅打印列表中每个字符串的第一次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com