Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use the ack tool (like grep but more stronger) in order to match strings in files

Please take a look on the following ack example

  ./ack -a /var/ntp/ntpstats/file

   55305 57262.736 9624 0.000460 0.00127 0.00168

I want to search only the IP in /var/ntp/ntpstats/file

but ack return the line:

   55305 57262.736 9624 0.000460 0.00127 0.00168

So its seems that ack match the but actually the line not have the address

Can someone give me advice how to match exactly with the ack tool? (maybe some missing flags after ack?)


share|improve this question
ack is not stronger, it's weaker! :-) – Jonathan Hartley Feb 27 '12 at 14:25
up vote 3 down vote accepted

The . is a meta character in Perl. It means "match any character." You have three choices.

  • ack '127\.0\.0\.1' This quotes each period, making each be an actual period.
  • ack '\Q127.0.0.1' In Perl regexes, \Q means "quote all metacharacters after the \Q"
  • ack -Q Since the \Q is so common, ack has the -Q switch which means the same thing.

Also, I see you using the -a switch, presumably to make sure that you match files that don't have an extension. This is unnecessary if you specify a file to search through.

share|improve this answer
It's not every day thay somone gets an answer from the author of the tool they're asking about! :) – daxelrod Nov 8 '11 at 3:30
It oughta be. I have a Google Alert for ack. Other authors can do the same. – Andy Lester Nov 10 '11 at 22:06

A . in a regular expression matches any single character other than a newline. You'll need to escape it if you want to match a literal period.

./ack -a '127\.0\.0\.1' /var/ntp/ntpstats/file
share|improve this answer

Just to elaborate Ignacio's answer:

55305 57262.736 9624 0.000460 0.00127 0.00168

As you can see, with the wild character (.) the above part of the line matches your search criteria.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .