Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 7th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Combining and summing data from multiple files
  2. aaa 5
  3. bbb 2
  4. ccc 9
  5. ddd 46
  6. eee 89
  7. fff 56
  8.        
  9. aaa 54
  10. bbb 8
  11. ccc 16
  12. ddd 4
  13. eee 66
  14. fff 9
  15.        
  16. declare -A sums
  17. while read name val1 val2; do
  18.   sums[$name]=$(( val1 + val2 ))
  19. done < <(join -j 1 file1 file2)
  20.  
  21. echo "Sum of aaa: ${sums[aaa]}"
  22. echo "Sums of ccc and ddd: $(( ${sums[ccc]} + ${sums[ddd]} ))"
  23. echo "Sums of bbb, eee, and fff: $(( ${sums[bbb]} + ${sums[eee]} + ${sums[fff]} ))"
  24.        
  25. kent$  awk 'NR==FNR{a[$1]=$2;next;}{a[$1]+=$2}END{print "sum of aaa:",a["aaa"]}' file1 file2
  26. sum of aaa: 59
  27.        
  28. awk '{a[$1]+= $2;}END{for(i in a){print i,a[i];}}' file
  29.        
  30. cat file1 file2 | awk '...