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?
Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).
ack is easy to use: the main complication/feature relative to grep is that it uses perl regular expressions instead of POSIX ones. Another difference is that, while grep is an excellent general purpose tool, ack is specialized to serve the needs of programmers.
? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters. Use a double backslash ( \\ ) to denote an escaped string literal.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With