awk match two files and print output in new file based on condition -


i have 2 files alpha1.csv , spam1.csv

$ cat alpha1.csv   aktel_bangladesh,bangladesh,alphanumeric_a_msisdn_blocking,1095    aljawal_saudi_telecom_company,saudi_arabia,alphanumeric_a_msisdn_blocking,9592   b-mobile_brunei,brunei,alphanumeric_a_msisdn_blocking,3     $ cat spam1.csv   ain_ais_global_communications,thailand,spam_chain_sms_reject(spam_detection_and_blocking),1   aktel_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),16   aljawal_saudi_telecom_company,saudi_arabia,spam_chain_sms_reject(spam_detection_and_blocking),10593   at&t_wireless,united_states,spam_chain_sms_reject(spam_detection_and_blocking),218   banglalink_sheba_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),111   

expected output:

ain_ais_global_communications,thailand,spam_chain_sms_reject(spam_detection_and_blocking),1,**na,na**   aktel_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),16,alphanumeric_a_msisdn_blocking,1095   aljawal_saudi_telecom_company,saudi_arabia,spam_chain_sms_reject(spam_detection_and_blocking),10593,alphanumeric_a_msisdn_blocking,9592   at&t_wireless,united_states,spam_chain_sms_reject(spam_detection_and_blocking),218,**na,na**   banglalink_sheba_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),111,**na,na**   b-mobile_brunei,brunei,**na,na**,alphanumeric_a_msisdn_blocking,3   

my command printing matched cases of file 2 file 1 , not printing non matched cases:

$ awk 'begin{fs=ofs=","} fnr==nr {a[$1,$2]=$3 fs $4; next} {print $0, (i=a[$1,$2]?a[$1,$2]:"na,na")}' alpha1.csv spam1.csv   ain_ais_global_communications,thailand,spam_chain_sms_reject(spam_detection_and_blocking),1,na,na   aktel_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),16,alphanumeric_a_msisdn_blocking,1095   aljawal_saudi_telecom_company,saudi_arabia,spam_chain_sms_reject(spam_detection_and_blocking),10593,alphanumeric_a_msisdn_blocking,9592   at&t_wireless,united_states,spam_chain_sms_reject(spam_detection_and_blocking),218,na,na   banglalink_sheba_bangladesh,bangladesh,spam_chain_sms_reject(spam_detection_and_blocking),111,na,na   

re-using existing code:

awk ' begin { fs = ofs ="," }  fnr==nr {     a[$1 fs $2] = $3 fs $4;      next }  {     print $0, (($1 fs $2) in a?a[$1 fs $2]:"na,na")     delete a[$1 fs $2] } end {     for(left in a) print left,"na,na", a[left] }' alpha spam 

or

awk ' begin { fs = ofs ="," }  fnr==nr {     a[$1,$2] = $3 fs $4;      next }  {     print $0, (($1,$2) in a?a[$1,$2]:"na,na")     delete a[$1,$2] } end {     for(left in a) {         split(left, tmp, subsep)         print tmp[1], tmp[2], "na,na", a[left]     } }' alpha spam 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -