ack-grep: chars escaping
Asked Answered
C

4

22

My goal is to find all "<?=" occurrences with ack. How can I do that?

ack "<?="

Doesn't work. Please tell me how can I fix escaping here?

Calchas answered 19/6, 2010 at 2:48 Comment(0)
J
29

Since ack uses Perl regular expressions, your problem stems from the fact that in Perl RegEx language, ? is a special character meaning "last match is optional". So what you are grepping for is = preceded by an optional <

So you need to escape the ? if that's just meant to be a regular character.

To escape, there are two approaches - either <\?= or <[?]=; some people find the second form of escaping (putting a special character into a character class) more readable than backslash-escape.

UPDATE As Josh Kelley graciously added in the comment, a third form of escaping is to use the \Q operator which escapes all the following special characters till \E is encountered, as follows: \Q<?=\E

Japan answered 19/6, 2010 at 2:52 Comment(2)
Or use \Q<?=\E to escape ALL special characters.Pallaton
Or use the -Q option to escape all special characters, too, because really, who wants to mess with the \Q \E crap?Foumart
F
20

Rather than trying to remember which characters have to be escaped, you can use -Q to quote everything that needs to be quoted.

Foumart answered 19/6, 2010 at 4:51 Comment(0)
W
15
ack -Q "<?="

This is the best solution if you will want to find by simple text.

(if you need not find by regular expression.)

Whopping answered 30/1, 2015 at 2:52 Comment(0)
G
8
ack "<\?="

? is a regex operator, so it needs escaping

Garbanzo answered 19/6, 2010 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.