# Integration: generate a formatted report from data with a here-doc # template, functions and printf. cat >sales.csv <<'CSV' region,item,qty,price north,widget,10,2.50 south,gadget,3,10.00 north,gadget,1,10.00 east,widget,7,2.50 south,widget,4,2.50 CSV cents() { # 2.50 -> 250 whole=${1%.*}; frac=${1#*.} echo $((whole * 100 + ${frac#0})) } money() { printf '%d.%02d' $(($1 / 100)) $(($1 % 100)); } total=0 regions= { read -r header while IFS=, read -r region item qty price; do c=$(cents "$price") line=$((qty * c)) total=$((total + line)) eval "r_$region=\$(( \${r_$region:-0} + line ))" case " $regions " in *" $region "*) ;; *) regions="$regions $region" ;; esac done } /dev/null cat <