【问题标题】:ack: Exclude specific directories from search via regexack:通过正则表达式从搜索中排除特定目录
【发布时间】:2012-01-29 23:41:25
【问题描述】:

如何通过 RegEx 使用 ack 忽略特定目录?

我可以使用--ignore-dir 选项,但这不允许我指定正则表达式。我希望能够忽略名称中包含 testtests 或更复杂模式的任何目录。

我还尝试了消极的后视

ack -G '(?<!test)' pattern

但这不起作用。它不排除test 目录。

【问题讨论】:

    标签: regex ack


    【解决方案1】:

    在最新版本的 ack 中,您可以使用带有 --ignore-dir 的正则表达式。

    来自this thread

    是的,我们在 2.15_01 中添加了:

    • ack 现在支持 --ignore-dir=match:.... 谢谢 Ailin Nemui! (GitHub 票 #42)

    GitHub 链接:https://github.com/petdance/ack2/issues/42

    遗憾的是,我认为这仅支持匹配基本目录名称,而不是完整路径。

    例如,您不能将".*/docs/generated/.*" 之类的路径与它匹配。

    为此,请参阅GitHub issue 291

    2020 年更新:看起来现在已移至 ack3 (github issue)。

    【讨论】:

      【解决方案2】:

      用ack2,好像you can't use holygeek's solution.

      这是我使用-v-x 的方法:

      ack -v -g 'test' | ack -x pattern
      

      更一般地说,'test' 可以是要排除的目录的正则表达式

      【讨论】:

      • 确实对于ack2,这就是你想要的。谢谢!
      【解决方案3】:

      为了方便使用 1.96 之前版本的 ack 的人(比如我),您可以使用 regex 环顾四周来执行此操作。这是一个例子:

      ack --java 'text-pattern' -G '^((?!(test|target)).)*$'
      

      这将递归搜索所有Java 文件中的text-pattern(从.),这些文件的路径中没有单词testtarget

      【讨论】:

      • 这个结构 '^((?!(test|target)).)*$' 是什么意思?
      【解决方案4】:

      使用未记录的选项“--invert-file-match”(我系统上的确认版本:1.96):

      $ ack pattern -G 'test|tests' --invert-file-match
      

      嗯,这是有记录的:

      $ ack --help|grep invert
      -v, --invert-match    Invert match: select non-matching lines
      --invert-file-match   Print/search handle files that do not match -g/-G.
      

      它的 perldoc 中没有记录。

      【讨论】:

      • 太棒了——谢谢!在我正在查看的手册页中也没有任何关于此的内容,但它有效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多