
Untitled
By: a guest on
Aug 7th, 2012 | syntax:
None | size: 0.62 KB | hits: 12 | expires: Never
Combining and summing data from multiple files
aaa 5
bbb 2
ccc 9
ddd 46
eee 89
fff 56
aaa 54
bbb 8
ccc 16
ddd 4
eee 66
fff 9
declare -A sums
while read name val1 val2; do
sums[$name]=$(( val1 + val2 ))
done < <(join -j 1 file1 file2)
echo "Sum of aaa: ${sums[aaa]}"
echo "Sums of ccc and ddd: $(( ${sums[ccc]} + ${sums[ddd]} ))"
echo "Sums of bbb, eee, and fff: $(( ${sums[bbb]} + ${sums[eee]} + ${sums[fff]} ))"
kent$ awk 'NR==FNR{a[$1]=$2;next;}{a[$1]+=$2}END{print "sum of aaa:",a["aaa"]}' file1 file2
sum of aaa: 59
awk '{a[$1]+= $2;}END{for(i in a){print i,a[i];}}' file
cat file1 file2 | awk '...