【问题标题】:how to exclude multiple pattern using grep如何使用 grep 排除多个模式
【发布时间】:2014-07-30 20:01:51
【问题描述】:

我想从 grep 中排除 "*log*"./tags

我做的是:

grep -rI "PatternToSearch" ./path --exclude="*log*" 

或者这个:

grep -rI "PatternToSearch" ./path --exclude="tags"

是否可以在一个 grep 中合并两种排除模式?

【问题讨论】:

  • 您也可以使用find 来解释它,例如find . -name ./tags -prune -o -not -name "*log*" -exec grep -HI Pattern {} +

标签: linux shell grep command


【解决方案1】:

还有一个--exclude <pattern>

grep -rI "PatternToSearch" --exclude="*log*" --exclude="tags" .

【讨论】:

  • 感谢@anubhava 它起作用了,但很想知道是否还有其他方法可以让我只写一次--exclude
  • 不幸的是--exclude 不允许正则表达式或扩展模式在单个选项中指定多个模式。
  • 您可以使用--exclude={*log*,tags} 忽略多个带有--exclude 的模式。
【解决方案2】:

试试下面:

 grep -rI "PatternToSearch" ./path --exclude={*log*,tags}

只需使用“,”来分隔模式。

似乎与how do I use the grep --include option for multiple file types? 重复

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2018-08-05
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多