Grepping Multiple Strings on the Same Line 'Or'

I've got this command that I've been using to find strings on the same line, say I'm doing a search for name:

find . -name "*" | xargs grep -i "Doe" | grep -i "John" > output.txt

This gives me every line in a file that has John and Doe in it. I'm looking to add a OR operator for the second grep statement, so that I can grep for "John" OR "Jonathon" for example. In this example, I know that I could just enter "Jo*" but I don't want any other terms like Jose or Jonas.

I tried

find . -name "*" | xargs grep -i 'Doe\|John' > output.txt

but that didn't seem to work.

Thanks in advance.

you can use awk

awk '/Doe/&&(/John/||/Jonathon/){print}' * >output.txt

Awk scares me. It looks like C code. :slight_smile: But that looks good, I'll give it a try. Thanks!

Here are a couple of options:

find . -name ""| xargs grep -i Doe| grep -i -e John -e Jonathon >output.txt
find . -name "
"| xargs grep -i Doe| egrep -i "(John|Jonathon)" >output.txt

You can also combine the find and xargs commands:

find . -name "*" -exec grep -i Doe {} \; | grep -i -e John -e Jonathon >output.txt

[deleted as redundant]

Another approach. But don't have the script or the output file in the same tree as the find !

#!/bin/ksh
(
find . -type f -print | while read FILENAME
do
        echo "${FILENAME}"
        egrep -iw "Doe" "${FILENAME}"|egrep -iw "John|Jonathan"
done
) > /tmp/output