sed - How to find the matching records from 2 files in unix -
i have 2 files contains email_ids. 1. test1.txt 2. test2.txt
test1.txt contents are:
abc@gmail.com xyz@gmail.com mns@gmail.com
test2.txt contents are:
jpg@gmail.com joy@yahoo.com abc@gmail.com pet@yahoo.com
here abc@gmail.com common id between test1.txt , test2.txt. want find out such id's these 2 files , insert them 1 file.
please suggest. need id's common in between these 2 files.
try:
awk 'nr==fnr{a[$1]; next} $1 in a' file1 file2 > file.new
--edit: added explanation --
awk ' nr==fnr { # when first file being read (only fnr , nr equal) a[$1] # create (associative) element in array first field index next # start reading next record (line) } $1 in # while reading second file, if field 1 present in array print record (line) ' file1 file2 > file.new # first read file1 first file , file2 second file , write output 3rd file.
Comments
Post a Comment