David Hasselhof the 2nd David Hasselhof the 2nd -4 years ago 153
Linux Question

Finding emails from one file in another

I want to find emails from one file listed as such:

john@blogs.com
joe@blogs.com
james@blogs.com


in another file listed as so:

john@blogs.com:+123456789
jack@blogs.com:+123456789
jeff@blogs.com:+123456789
james@blogs.com:+123456789


and output the lines from the second file that match with the first file also keeping in mind it needs to match the entire email from start to finish so it won't match robertjohn@blogs.com accidently.

Desired output:

john@blogs.com:+123456789
james@blogs.com:+123456789


Thanks!

Answer Source

With grep, get the newline separated patterns (strings here, -F) from an input file with -f, and match with the other file:

grep -Ff email.txt file.txt 

With awk, keeping an array with emails as indexes and checking if the first field of each record in the second file is an index:

awk -F: 'NR==FNR{a[$0]=1; next} a[$1]' email.txt file.txt

Example:

% cat email.txt 
john@blogs.com
joe@blogs.com
james@blogs.com

% cat file.txt
john@blogs.com:+123456789
jack@blogs.com:+123456789
jeff@blogs.com:+123456789
james@blogs.com:+123456789

% grep -Ff email.txt file.txt 
john@blogs.com:+123456789
james@blogs.com:+123456789


% cat email.txt                                                  
john@blogs.com
joe@blogs.com
james@blogs.com

% cat file.txt
john@blogs.com:+123456789
jack@blogs.com:+123456789
jeff@blogs.com:+123456789
james@blogs.com:+123456789

% awk -F: 'NR==FNR{a[$0]=1; next} a[$1]' email.txt file.txt
john@blogs.com:+123456789
james@blogs.com:+123456789
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download