我需要comapre两个文件f1.txt和f2.txt并获得匹配和不匹配,对于这种情况 我想检查f1.txt的第二个字段是否匹配f2.txt的第一个字段,如果是的话 然后打印f1.txt的整行并打印f2.txt的第一个字段和f2.txt的第二个字段的Sum。并且在f1.txt上找不到匹配状态“NotFound”。
f1.txt
aa,10,cc,Jan-13
bb,20,cc,Feb-13
dd,50,cc,Mar-13
f2.txt
10,1500,ss
20,500,gg
10,2000,kk
10,15000,yy
20,500,zz,
35,250,tt
Output.txt的
aa,10,cc,Jan-13,10,18500
bb,20,cc,Feb-13,20,1000
dd,50,cc,Mar-13,NotFound,NotFound
答案 0 :(得分:3)
此awk
应该
awk -F, 'FNR==NR {a[$1]+=$2;next} {if ($2 in a) print $0,$2,a[$2]; else print $0,"NotFound","NotFound"}' OFS=, f2.txt f1.txt
aa,10,cc,Jan-13,10,18500
bb,20,cc,Feb-13,20,1000
dd,50,cc,Mar-13,NotFound,NotFound
它是如何运作的:
awk -F, ' #Set Field separator to ,
FNR==NR {a[$1]+=$2;next} #Read data from file f2.txt using field #1 as index and sum field #2 in to array a
{if ($2 in a) #Test if field #2 in f1.txt is found in a
print $0,$2,a[$2] #If found, print line of f1.txt with sum and index from array
else print $0,"NotFound","NotFound" #If not found print line of f1.txt with NotFound
}
' OFS=, f2.txt f1.txt #Set Output field separator to , and read files
略短的版本:
awk -F, 'FNR==NR {a[$1]+=$2;next} {print $0 ","($2 in a?$2","a[$2]:"NotFound,NotFound")}' f2.txt f1.txt