Ack & negative lookahead giving errors
Asked Answered
G

2

10

I have a problem with using ack-grep with a negative look ahead.

I am running this command:

ack-grep "paypal_responded(?!_at)"

but I am getting the error:

bash: !_at: event not found

I have tried adding backslashes in various places, but I'm also new to using ack & linux, so please treat me as a newbie with any instructions.

Thanks in advance.

Gutter answered 5/12, 2011 at 12:3 Comment(0)
H
18

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

You need single-quote to avoid bash interpret ! as history expand command.

Humes answered 5/12, 2011 at 12:6 Comment(4)
Great stuff. That's sorted it. Thanks :o)Gutter
@Gutter Would you mind to accept this as an answer so that this question no longer remains unanswered? Thanks!Ingridingrim
Sorry. This dates back to my early days on here didn't realise I'd not accepted an answer. Done now.Gutter
This also works with zsh. It is way more practical than escaping all ( ) and ! in regular expressionsSinistrality
T
4

The shell is interpreting the ! in your input as a command substitution:

$ 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
$ 

You need to tell the shell that ! has no special meaning; there are two ways to do that:

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

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

or

ack-grep 'paypal_responded(?!_at)'

Single-quoted strings have fewer transformations applied to them:

$ ack-grep "s\!" /etc/passwd
$ ack-grep 's!' /etc/passwd
$ 
Tomato answered 5/12, 2011 at 12:10 Comment(2)
I tried... ack-grep "paypal_responded(?\!_at)" .. but got the error... "ack-grep: Invalid regex 'paypal_responded(?\!_at)': Sequence (?\...) not recognized in regex; marked by <-- HERE in m/paypal_responded(?\ <-- HERE !_at)/"Gutter
Sheesh, there's obviously more going on than I expected -- my simple examples worked exactly as I wanted, but the longer one with (?!_at) fails in horrible ways and it's easy to see why you were driving yourself nuts trying to make it work. ack-grep "paypal_responded\(?\!_at\)" works fine.... but I can't explain it.Tomato

© 2022 - 2024 — McMap. All rights reserved.