【问题标题】:Ack & negative lookahead giving errors确认和否定前瞻给出错误
【发布时间】:2011-12-05 12:03:33
【问题描述】:

我在使用 ack-grep 时遇到了问题。

我正在运行这个命令:

ack-grep "paypal_responded(?!_at)"

但我收到了错误:

bash: !_at: event not found

我尝试在不同的地方添加反斜杠,但我对使用 ack 和 linux 也很陌生,所以请把我当作新手,有任何说明。

提前致谢。

【问题讨论】:

    标签: linux command-line ack


    【解决方案1】:

    试试ack-grep 'paypal_responded(?!_at)'

    您需要单引号以避免 bash 将 ! 解释为历史扩展命令。

    【讨论】:

    • 好东西。这样就整理好了。谢谢 :o)
    • @TobyG 你介意接受这个作为答案,这样这个问题就不会再没有答案了吗?谢谢!
    • 对不起。这可以追溯到我在这里的早期并没有意识到我没有接受答案。现在完成。
    • 这也适用于 zsh。这比在正则表达式中转义所有( )! 更实用
    【解决方案2】:

    shell 将您输入中的 ! 解释为命令替换:

    $ ack-grep root /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    $ !ac
    ack-grep root /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    $ 
    

    你需要告诉shell!没有特殊含义;有两种方法可以做到这一点:

    罢工>

    ack-grep "paypal_responded(?\!_at)"
    

    ack-grep "paypal_responded\(?\!_at\)"
    

    ack-grep 'paypal_responded(?!_at)'
    

    单引号字符串应用的转换较少:

    $ ack-grep "s\!" /etc/passwd
    $ ack-grep 's!' /etc/passwd
    $ 
    

    【讨论】:

    • 我试过... ack-grep "paypal_responded(?\!_at)" .. 但得到了错误... "ack-grep: Invalid regex 'paypal_responded(?\!_at)' : 正则表达式中无法识别序列 (?\...);在 m/paypal_responded(?\ 中用
    • Sheesh,显然发生的事情比我预期的要多——我的简单示例完全按照我的意愿工作,但是带有(?!_at) 的较长示例以可怕的方式失败了,很容易看出你为什么要开车你自己疯了,试图让它发挥作用。 ack-grep "paypal_responded\(?\!_at\)" 工作正常....但我无法解释。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签