0

let say i have one text file contains emails

abd3@dom
abd2@dom
sdklf2@lksd
sd@gm

i need small bash script for grep , find , to find contain the email in files and and print out the files they match it.

the expect it to be

**this email abd3@dom found in file8560.txt**
**this email abd2@dom found in file750.txt**
**this email sdklf2@lksd found in file970.txt**
**this email sd@gm found in file2690.txt**
1
  • @user* - It's always trivial to find strings you expect but much harder to not find strings you don't expect. We need to see some examples of how the email addresses you want to find appear in context so we can help you figure out how to find them without falsely matching on strings you don't want to find.
    – Ed Morton
    Commented Mar 30, 2020 at 16:34

2 Answers 2

1

If you know the list of files to search for email addresses in, you can

grep -F -H -w -o -f email_list_file list of files to search | awk -F: '{print "*** this email " $2 " found in " $1 "**}'

The ‘-w’ flag will reduce but not eliminate some of the false positives Ed points out in comments. The ‘-o’ flag is necessary to print only the email address, not the entire line containing it.

2
  • 1
    That would report that [email protected] exists in a file that contains [email protected]. You need anchors and to escape regexp metachars.
    – Ed Morton
    Commented Mar 30, 2020 at 16:31
  • 1
    @EdMorton - good point. I added flags ‘-F’ to address one of those issues.
    – Larry
    Commented Apr 9, 2020 at 0:37
0
grep -Fxf list_of_emails.txt files...

find ... -type f -exec grep -Fxf list_of_emails.txt /dev/null {} +

Replace the ... in the find command with a list of files and directories, and maybe other find predicates.

The /dev/null is to force grep to always prefix the result with the filename, which grep doesn't do when called with a single file. This emulates the -H option of GNU grep, which is not portable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .