Try sg as follows:
awk 'NR==FNR{a[$1]=0;next}$1 in a{a[$1]+=$3}END{for(i in a) print i,a[i]}' Name_file table
Output:
Name1 71 Name2 149 Name3 0 Name4 12
In this case, you do not need the bash awk loop. First, it reads Names_table , then it processes all the table rows in one step. So it is much more effective.
ADDED
Or pure bash (> = 4.0):
printf -v tmp "[%s]=0 " $(<Name_file) declare -A htmp eval htmp=($tmp) while read abc; do [ -n "${htmp[$a]}" ] && ((htmp[$a] += $c)); done <table for i in ${!htmp[*]}; do echo $i ${htmp[$i]}; done
DISTRIBUTION
The extended question consisted of a grouping of $1 and $2 (and Name_file contains all the first keys from table , so this really does not need to be processed).
cat >table <<XXX Name1 xxxxx 34 Name1 xxxxx 37 Name2 aaaaa 59 Name2 xxxxx 90 Name4 Name3 12 XXX awk -v SUBSEP=, '{a[$1,$2]+=$3;++n[$1,$2]}END{for(i in a) print i,a[i],n[i]}' table
Output:
Name2,xxxxx 90 1 Name2,aaaaa 59 1 Name4,Name3 12 1 Name1,xxxxx 71 2
Truey source share