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

 mobiledu2502910233 发布于 2023-02-09 12:36

我有一个文件,例如"queries.txt",它有硬回包分隔的字符串.我想使用此列表在第二个文件"biglist.txt"中查找匹配项.

"biglist.txt"可能在"queries.txt"中为每个字符串添加多个匹配项.我想只返回每个查询的第一个匹配并将其写入另一个文件.

grep -m 1 -wf queries.txt biglist.txt>输出

只给我输出一行.我应该输出与queries.txt相同的行数.

有什么建议吗?非常感谢!我搜索了过去的问题,但在几分钟的阅读后没有找到一个完全相同的案例.

1 个回答
  • 如果你想在每个文件后"重置计数器",你可以这样做

    cat queries.txt | xargs -I{} grep -m 1 -w {} biglist.txt > output
    

    这用于xargsgrep输入中的每一行调用一次......应该为你做的伎俩.

    说明:

    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
    

    2023-02-09 12:38 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有