如何在unix中找到2个文件的匹配记录

时间:2014-03-10 07:34:33

标签: unix sed awk

我有两个包含email_ids的文件。 1. Test1.txt 2. Test2.txt

Test1.txt的内容是:

abc@gmail.com xyz@gmail.com mns@gmail.com

Test2.txt内容是:

jpg@gmail.com joy@yahoo.com abc@gmail.com pet@yahoo.com

这里abc@gmail.com是Test1.txt和Test2.txt之间的通用ID。我想从这两个文件中找出这样的Id并将它们插入到一个文件中。

请建议。我只需要在这两个文件之间常见的ID。

2 个答案:

答案 0 :(得分:3)

您也可以使用grep执行此操作:

grep -Fwf Test1.txt Test2.txt

$ head t*
==> t1 <==
abc@gmail.com
xyz@gmail.com
mns@gmail.com

==> t2 <==
jpg@gmail.com
joy@yahoo.com
abc@gmail.com
pet@yahoo.com

$ grep -Fwf t1 t2
abc@gmail.com

答案 1 :(得分:2)

尝试:

awk 'NR==FNR{A[$1]; next} $1 in A' file1 file2 > file.new

- 编辑:添加说明 -

awk '           
  NR==FNR {               # When the first file is being read (only then are FNR and NR equal)
    A[$1]                 # create an (associative) element in array A with the first field as the index 
    next                  # start reading the next record (line)
  }              
  $1 in A                 # while reading the second file, if field 1 is present in array A then print the record (line) 
' file1 file2 > file.new  # first read file1 as the first file and then file2 as the second file and write the output to a 3rd file.