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.
Asked
Active
Viewed 907 times
1

Zsolt Botykai
- 50,406
- 14
- 85
- 110

doon
- 2,311
- 7
- 31
- 52
-
2duplicate http://stackoverflow.com/questions/6480687/grep-for-2-words-existing-on-the-same-line – Srujan Kumar Gulla Jan 02 '13 at 19:08
3 Answers
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
- 50,406
- 14
- 85
- 110
2
It is a common problem and here is the common solution:
awk '/str1/&&/str2/' file

Ed Morton
- 188,023
- 17
- 78
- 185
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
- 91,102
- 13
- 100
- 152