简体   繁体   中英

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. And for no match found on f1.txt to state "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

This awk should do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM