【问题标题】:How to use `git grep --not`如何使用`git grep --not`
【发布时间】:2019-05-18 22:48:21
【问题描述】:

我想为 <pattern> 运行 git grep 命令,但排除超过 200 个字符的行。

我已经设法通过以下命令实现了这一点:

git grep <pattern> | grep -Ev '.{200}'

如果可能,我想将其作为一个命令运行(不使用管道)。 我以为我可以为此使用 git grep --not 标志,但遇到了困难。

这是我的尝试:

git grep -e <pattern> --not -e '.{200}'

我尝试了上述命令的不同组合,但没有得到想要的输出。

任何关于我下一步应该尝试的指示将不胜感激,在此先感谢您的帮助。


解决方案

(感谢@torek)

git grep -E -e one --and --not -e '.{200}'

【问题讨论】:

    标签: git grep


    【解决方案1】:

    description of the boolean operators for git grep 内容如下:

    --和
    --或
    --非
    ( …​ )
    指定如何使用布尔表达式组合多个模式。 --or 是默认运算符。 --and 的优先级高于 --or-e 必须用于所有模式。

    你用过:

    git grep -e &lt;pattern&gt; --not -e '.{200}'

    所以你确实为每个模式写了-e;但你没有选择--and--or 之一,所以你得到了默认值。哪一个是默认的? (阅读上面引用的文字。)如果您要求匹配某些模式的内容 not 至少 200 个字符,会选择 12 个字符长的内容吗?它至少不是 200,所以是的,它将被选中。 234 个字符 匹配的东西呢?好吧,它匹配,所以是的,它将被选中。唯一会被拒绝的是那些 匹配 并且 是 200 个字符或更长的内容。

    请注意,git grep 本身也默认为基本正则表达式,除非您使用命令行选项或将其配置为使用扩展或 Perl 正则表达式。所以你可能想在这里-E -e &lt;pattern&gt; --and --not -e '.{200}'。另见https://en.wikipedia.org/wiki/Regular_expression

    【讨论】:

    • 详尽解释的惊人答案,谢谢:)
    【解决方案2】:

    你也可以使用awk来解决这个问题。

    awk '/&lt;pattern&gt;/ &amp;&amp; length($2) &lt; 200'

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2019-04-16
      • 2011-12-12
      • 2013-08-16
      相关资源
      最近更新 更多