0

I want to find a list of files that have A but do not have B and C.

grep -r -L 'B\|C' finds the ones without B and C, but how do I add the condition of having A as well?

A.J
  • 1,140
  • 5
  • 23
  • 58

3 Answers3

2

You can use negative lookahead in grep using options -P or --perl-regexp

grep -r -P -L '^(?!.*A).*$|B|C'
Community
  • 1
  • 1
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
2

If I understand your question correctly:

grep -l "A" $(grep -r -E -L "B|C" *)

i.e. search for files containing "A" in the list of files that your original command generates.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

If I understood your question correctly, you can do it like this:

grep "A" file.txt | grep -v -e "B" -e "C"

The first grep finds lines containing A, the second greptakes the result and removes lines containing either "B" or "C". This works by the -v flag which inverses matches.

anaotha
  • 552
  • 6
  • 15
  • I ran `grep -r -l "lower\.html" | grep -v -e "DetVehicle" -e "customer"` and it return a list of files that have `lower` but they also have either `Vehicle` or `customer` – A.J Feb 03 '17 at 17:39