ack + grep + not really match the string

1

1

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 127.0.0.1 /var/ntp/ntpstats/file

   55305 57262.736 10.106.190.191 9624 0.000460 0.00127 0.00168

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

but ack return the line:

   55305 57262.736 10.106.190.191 9624 0.000460 0.00127 0.00168

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

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

thanks

jennifer

Posted 2010-10-14T09:25:40.313

Reputation: 897

ack is not stronger, it's weaker! :-) – Jonathan Hartley – 2012-02-27T14:25:47.320

Answers

3

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 127.0.0.1 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.

Andy Lester

Posted 2010-10-14T09:25:40.313

Reputation: 1 121

It's not every day thay somone gets an answer from the author of the tool they're asking about! :) – daxelrod – 2011-11-08T03:30:25.990

It oughta be. I have a Google Alert for ack. Other authors can do the same. – Andy Lester – 2011-11-10T22:06:39.797

2

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

Ignacio Vazquez-Abrams

Posted 2010-10-14T09:25:40.313

Reputation: 100 516

0

Just to elaborate Ignacio's answer:

55305 57262.736 10.106.190.191 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.

JRT

Posted 2010-10-14T09:25:40.313

Reputation: 633