1

I have what looked to me as a simple operation but cannot find a solution. I need to search through files in a directory with lines containing str1 AND str2. There are several examples of str1|str2 e.g. grep -r "str1|str2" . But nothing for str1 AND str2. Can anyone provide a solution. This should be a common problem.

Zsolt Botykai
  • 48,485
  • 14
  • 85
  • 106
doon
  • 2,251
  • 6
  • 31
  • 47

3 Answers3

2

You can do something like:

egrep 'str1.*str2|str2.*str1' FILES

with egrep. Or with positive lookahead/behind regexes. Read the manual.

Zsolt Botykai
  • 48,485
  • 14
  • 85
  • 106
2

It is a common problem and here is the common solution:

awk '/str1/&&/str2/' file
Ed Morton
  • 172,331
  • 17
  • 70
  • 167
0

You didn't ask this, but if you want files that contain both str1 and str2 but not on the same line:

grep str2 $(grep -l str1)
Andy Lester
  • 86,927
  • 13
  • 98
  • 148