0

I have the following being sent to my command line:

find /base/dir1 /base/dir2 -type f -exec /x/y/z/grep -e lolol -e wow {} +

This returns each file containing one or both of the supplied strings (lolol and wow). What I would like to do is to only return the files that contain both strings (AND not OR). If possible how could I accomplish this?

EDIT: The searched strings would need to be present in each file returned but not necessarily on the same line.

EDIT 2: I do not have the -r or -R options

  • @StephenKitt I am fairly new to this so I will have to research the solution found in your link some. If this does in fact offer a viable solution to search for a string1 AND a string2 it should suffice but again I will need to see if all mentioned is available on my OS – FamousAv8er Jun 09 '20 at 14:59
  • 2
    find . -type f -exec grep -q FIND {} \; -exec grep -l ME {} \; from the accepted answer to the question I linked above will work with any POSIX-compliant implementation of find and grep. – Stephen Kitt Jun 09 '20 at 15:30
  • @StephenKitt I have tested that and it does seem to answer my question, thank you for the response. I can delete this post or accept as a duplicate. – FamousAv8er Jun 09 '20 at 15:54

2 Answers2

1

Perhaps use GNU awk instead of grep:

find /base/dir1 /base/dir2 -type f -exec gawk '
    FNR == 1 {p1 = p2 = 0}
    /lolol/  {p1 = 1} 
    /wow/    {p2 = 1}
    p1 && p2 {print FILENAME; nextfile}
' {} +
glenn jackman
  • 85,964
-1

I couldn't find a way of doing that using the same find, so I tried comparing the results of both greps, here's what I came up to:

comm -12 <(find . -type f -exec grep -l lolol {} + | sort) <(find . -type f -exec grep -l wow {} + | sort)

Hope it helps!

Stephen Kitt
  • 434,908