Search code examples
bashgrepmatchdata-processing

Grep Match entire email multiple lines


I have two files:

I have this fist file:

./j/n:[email protected]:Test1
./n/o:[email protected]:Test2
./j/r:[email protected]:Test2
./j/d:[email protected]:Test3
./x/s:[email protected]:Test4
./r/s:[email protected]:Test5
./w/i:[email protected]@gmail.com:Test6

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

[email protected]
[email protected]
[email protected]
[email protected]

So I want the output to be:

./j/r:[email protected]:Test2
./j/d:[email protected]:Test3
./w/i:[email protected]@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 '[email protected]' file.txt
    

    Output:

    ./w/i:[email protected]@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"