【问题标题】:How to find files contains both keywords on different line by grep?如何通过grep查找包含不同行上的两个关键字的文件?
【发布时间】:2012-11-18 12:23:28
【问题描述】:

Linux内核源码config/*里面有很多文件我想找一个和我的板子差不多的。

所以我试试:

egrep -l -e key1 -e key2 config/*

Sample config/file1:
    This is key1
    This is key2
Sample config/file2:
    This is key1
Sample config/file3:
    This is key2

但结果是key1key2 文件列表。 我想要包含key1 key2的结果 还是有其他简单的 unix shell 可以生成结果?

在之前的示例中,我只想列出“file1”。

【问题讨论】:

标签: grep sh


【解决方案1】:

我的建议:

( grep -l key1 config/* ; grep -l key2 config/* ) |sort |uniq -c |grep '^ *2\>' |awk '{print $2}'

第一个grep 打印所有包含key1 的文件,第二个grep 打印所有包含key2 的文件, 然后是sortuniq 和第三个grep 过滤器,用于两次列出的文件,即包含两个键。

【讨论】:

    【解决方案2】:

    'grep -E 'pat1.*pat2|pat2.*pat1'' 是我常用的咒语。只有当这些模式以给定的顺序出现时,公理的解决方案才有效,“|”正则表达式运算符允许搜索以任何顺序查找模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2018-12-11
      • 1970-01-01
      • 2012-08-01
      • 2015-11-29
      相关资源
      最近更新 更多