38

I would like to find lines in files that include two words, word_1 and word_2 in order, such as in Line A below, but not as in Line B or Line C:

Line A: ... word_1 .... word_2 .... 
Line B: ... word_1 ....
Line C: ... word_2 ....

I have tried

$ack '*word_1*word_2'
$ack '(word_1)+*(word_2)+'

and the same commands with ^ appended at the beginning of the regex (in an attempt to follow the Perl regex syntax).

None of these commands return the files or the lines I am interested in.

What am I doing wrong?

Thanks!

Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564

1 Answers1

81

You want to find word_1, followed by anything, any number of times, followed by word_2. That should be

word_1.*word_2

You seem to be using * as it is often used in command line searches, but in regexes is it a quantifier for the preceding character, meaning match it at least 0 times. For example, the regex a* would match 0 or more as, whereas the regex a+ would match at least one a.

The regex metacharacter meaning "match anything" is ., so .* means "match anything, any number of times. See perlrequick for a brief introduction on the topic.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • Thanks @dsolimano! Would there be any difference between what you wrote and: `.*word_1.*word_2`. If not, why? – Amelio Vazquez-Reina Apr 10 '11 at 15:54
  • 1
    Sort of, but not for what you're looking at. That would additionally match everything before `word_1` on the line. If you were looking at what matched, as opposed to if it matched or not, you would see more stuff in the match. If you have some cash and time, I would recommend reading "Mastering Regular Expressions" by Friedl, it's excellent. – dsolimano Apr 11 '11 at 00:44