I want to lookup values from two different tables based on common columns and append. The trick is the column to be looked up is not fixed and varies , so it has to be detected from the header. How can I achieve this at once, for multiple data files, but lookup tables fixed.
The two lookup tables are ftable and ttable. For ftable I want cols 2 and 3 by matching 1.
For ttable, I want just col 2 based on col1.
ftable
fin,evalue,count2
abc,e1,7
xyz,e7,2
ttable
tin,count
ghi,11
fgr,22
in the input files , we must detect the column with header rid , that matches with ftable and the column header tid which matches with ttable
1st file from Loc B
mid,rid,tid,xid
1,abc,23,ghi,d
2,abc,,fgr,t
2nd File from expA
fid,mid,rid,tid,xid
001,1,abc,23,ghi,d
002,2,xyz,,ghi,t
003,3,xyz,1,fgr,34
Desired outputs
1st file from Loc B
mid,rid,tid,xid,evalue,count2,count
1,abc,23,ghi,d,e1,7,11
2,abc,,fgr,t,e1,7,22
2nd File from expA
fid,mid,rid,tid,xid,evalue,count2,count
001,1,abc,23,ghi,d,e1,7,11
002,2,xyz,,ghi,t,e7,2,11
003,3,xyz,1,fgr,34,e7,2,22
My try only for the 1st lookup
awk -F"," 'NR==FNR{f[$1]=$2","$3;next} NR==1 { (for i=1;i<=NF;i++) { if (i=="rid" ) { ftab=i } } print $0",evalue,count2" } NR!=1 && $ftab in f {print $0","f[$ftab]}' ftable "1st file from Loc B"