I want to simulate AND operation in grep . List all the file files contains “Some Words” and contain “Some Other Words” anywhere in the file.
So far I tried #
egrep -lir '(Some Words)(Some Other Words)' *.myfiles
but it is not working.
Advertisement
Answer
Using GNU tools:
grep -lirZ string1 . | xargs -d '' grep -li string2
- The first grep lists files (
-l
) containingstring1
-i
: case insensitive,-r
: recursive,-Z
: file list delimited by ascii null (a safe delimiter for file names)- Pipe the list to xargs, with null delimiter, to pass it to the second grep
- The second grep filters the list for files containing
string2
.
refers to the current directory, which is searched recursively