0

I would like to store in file all path files with a specific extract of file content where file contains AAA but NOT BBB.

I tried so many things but this try is close to my goal :

find /data/my_project -type f -name "*.php" -exec grep -q "AAA" {} \; -exec grep -L "BBB" {} \; -exec awk '/AAA/{print $NF}'> /tmp/tables_names.txt {} \;  -print > /tmp/class_list.txt

But... Result contains file with sometimes AAA only, AAA AND BBB..

EDIT :

This is pretty same question as Find files that contain a string and do not contain another but where there's no answer validated it was really difficult to read. 75 % of my answer was in fact there sorry... But, I would like to return not only matched files paths but an extract (piece of text) of this matched file !

Delphine
  • 103
  • I'm not sure I understand. You want to find all files that contains AAA but no files that contain BBB (even if they contain AAA). What would you want to do with them? I'm confused by your awk command. – Kusalananda Mar 07 '18 at 14:33

4 Answers4

4

Finding all regular files with filenames matching *.php in or below /data/my_project that contain AAA but not BBB and storing their pathnames in /tmp/class_list.txt:

find /data/my_project -type f -name '*.php' \
    -exec grep -qF 'AAA' {} ';' \
    ! -exec grep -qF 'BBB' {} ';' \
    -print >/tmp/class_list.txt
Kusalananda
  • 333,661
  • @kusalanada : I would like to extract some word just after AAA in fact because I have something like AAA = xxx; and I want to provide a list with xxx and file path to know for wich AAA BBB part is missing ! – Delphine Mar 07 '18 at 15:10
  • 1
    Finally I used this find /data/my_project -type f -name '*.php' -exec grep -qF 'const TABLE_NAME' {} ';' ! -exec grep -qF 'BBB' {} ';' -exec awk '/const TABLE_NAME/{print $NF}' {} ';' -print >/tmp/class_list.txt where AAA is in reality const TABLE_NAME :) – Delphine Mar 07 '18 at 15:18
2

find + awk solution:

find /data/my_project -type f -name "*.php" -exec \
awk '/AAA/{ a=1 }/BBB/{ b=1 }END{ exit (!a || (a && b)) }' {} \; -print > /tmp/class_list.txt
1

Try this :

find /data/my_project -type f -name '*.php' -exec \
bash -c 'grep -q AAA "$1" && ! grep -q BBB "$1" && echo "$1"' -- {} \; \
> /tmp/class_list.txt
0
find .... -name '*.php' \
   -exec awk -v RS="\0" '/AAA/ && !/BBB/{print FILENAME}' {} \; >  out
JJoao
  • 12,170
  • 1
  • 23
  • 45