从另一个文件中查找电子邮件

时间:2021-05-28 21:21:41

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.

并输出第二个文件中与第一个文件匹配的行,同时记住它需要从头到尾匹配整个电子邮件,因此它不会与robertjohn@blogs.com意外匹配。

Desired output:

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

Thanks!

1 个解决方案

#1


2  

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

使用grep,使用-f从输入文件中获取换行符分隔的模式(字符串,-F),并与另一个文件匹配:

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,使用电子邮件作为索引保持数组,并检查第二个文件中每个记录的第一个字段是否为索引:

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

#1


2  

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

使用grep,使用-f从输入文件中获取换行符分隔的模式(字符串,-F),并与另一个文件匹配:

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,使用电子邮件作为索引保持数组,并检查第二个文件中每个记录的第一个字段是否为索引:

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