Search code examples
bashgrepmatchdata-processing

Grep Match entire email multiple lines


I have two files:

I have this fist file:

./j/n:jwillow@gmail.com:Test1
./n/o:nanofranky@list.ru:Test2
./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3
./x/s:xsebastianmenendez@hotmail.com:Test4
./r/s:rsebastianmenendez@hotmail.com:Test5
./w/i:willow@gmail.com@gmail.com:Test6

And I want it to match this second file, but the whole email, not just the appearance of the word:

willow@gmail.com
franky@list.ru
jdmq77@hotmail.com
sebastianmenendez@hotmail.com

So I want the output to be:

./j/r:franky@list.ru:Test2
./j/d:jdmq77@hotmail.com:Test3
./w/i:willow@gmail.com@gmail.com:Test6

I try with grep -f, but, since the words appear in the email, I get all the lines of the first file, not just the ones that matches with the whole email.


Solution

  • Use the -w argument.

    grep -w 'willow@gmail.com' file.txt
    

    Output:

    ./w/i:willow@gmail.com@gmail.com:Test6
    

    Bash script example to compare two files:

    #!/bin/bash
    
    strings_file="search_for.txt"
    input_file="in.txt"
    
    grep -w -F -f "$strings_file" "$input_file"