Skip to content
Advertisement

grep AND Operations

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) containing string1
  • -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
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement