[英]awk Lookup 2 files, print match and Sum of Sencond Field:
I need to comapre two files of f1.txt and f2.txt and obtain matches, and non-matches, for this case I am looking to check Second field of f1.txt is matching with First field of f2.txt,if yes then print the entire line of f1.txt and print first field of f2.txt and Sum of second field of f2.txt. 我需要comapre两个文件f1.txt和f2.txt并获得匹配和不匹配,对于这种情况我要检查f1.txt的第二个字段是否匹配f2.txt的第一个字段,如果是的话打印f1.txt的整行并打印f2.txt的第一个字段和f2.txt的第二个字段的Sum。 And for no match found on f1.txt to state "NotFound".
并且在f1.txt上找不到匹配状态“NotFound”。
f1.txt f1.txt
aa,10,cc,Jan-13
bb,20,cc,Feb-13
dd,50,cc,Mar-13
f2.txt f2.txt
10,1500,ss
20,500,gg
10,2000,kk
10,15000,yy
20,500,zz,
35,250,tt
Output.txt Output.txt的
aa,10,cc,Jan-13,10,18500
bb,20,cc,Feb-13,20,1000
dd,50,cc,Mar-13,NotFound,NotFound
This awk
should do 这个
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
How does it work: 它是如何工作的:
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
A slightly shorter version: 略短的版本:
awk -F, 'FNR==NR {a[$1]+=$2;next} {print $0 ","($2 in a?$2","a[$2]:"NotFound,NotFound")}' f2.txt f1.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.