I want to find emails from one file listed as such:
JavaScript
x
john@blogs.com
joe@blogs.com
james@blogs.com
in another file listed as so:
JavaScript
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:
JavaScript
john@blogs.com:+123456789
james@blogs.com:+123456789
Thanks!
Advertisement
Answer
With grep
, get the newline separated patterns (strings here, -F
) from an input file with -f
, and match with the other file:
JavaScript
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:
JavaScript
awk -F: 'NR==FNR{a[$0]=1; next} a[$1]' email.txt file.txt
Example:
JavaScript
% 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