fork download
  1. #!/usr/bin/env bash
  2.  
  3. declare -A totals=( )
  4. while IFS=, read -r category value; do
  5. (( totals[$category] += value ))
  6. done
  7.  
  8. for category in "${!totals[@]}"; do
  9. sum=${totals[$category]}
  10. echo "$category,$sum"
  11. done
Success #stdin #stdout 0s 19632KB
stdin
Blue,20
Blue,10
Black,13
Red,8
Black,10
Red,10
Blue,21
stdout
Black,23
Red,18
Blue,30