14

I am sick of having to escape things when I want to search for part of an html tag.

How can I ack search for exactly what I type without having to escape stuff?

e.g.

ack-grep 'console.log(foo'

I get:

Unmatched ( in regex; marked by <-- HERE in m/console.log( <-- HERE par/

closed as off topic by Zoredache, Rilindo, voretaq7 Oct 25 '12 at 19:37

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Use a different shell? Put it in quotes? Place your search string in a file, I suspect ack-grep can get the string from a file. Perhaps you can even could even get the search via stdin. – Zoredache Mar 1 '12 at 22:08
  • @Zoredache I added a better example.. I don't want to have to escape (.. and what do you mean by better shell? – tester Mar 1 '12 at 22:10
  • I mean all the chracters you are complaining about are handled by your shell (bash/zsh/whatever). The problem has nothing to do with the tool. The problem is that the shell is not sending what you type. – Zoredache Mar 1 '12 at 22:12
  • Ah I think i found it.. -Q uses it literally.. – tester Mar 1 '12 at 22:14
17

You have to escape the regex.

ack 'console\.log\(foo'

(You should escape the . so that you don't match "consoleflog", because . matches any single character)

And if you would rather not do that, do this to quote every metacharacter automatically.

ack -Q 'console.log(foo'
  • 2
    \Q does not work on my server. ack '\Qconsole.log(foo' ack: Invalid regex '\Qconsole.log(foo': Unmatched ( in regex; marked by <-- HERE in m/\Qconsole.log( <-- HERE foo/ – Sébastien Dec 1 '14 at 12:37
  • I was mistaken about the \Q example and have removed it from the answer. Thanks, @Sébastien. – Andy Lester Jul 14 '17 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.