#!/bin/bash # Creates a database file with two fields - name and amount. This file is used # for demonstration purposes, don't include in YOUR shell script cat > input-file <<+ name1:2 name2:5 name1:6 name3:4 name3:3 name1:3 name2:8 + # Awk command to use an "array" to store amounts by first field "name", then # at the END, use a for loop to generate a total for each name. Then at END # display the name with corresponding total awk -F":" 'BEGIN {printf "Total Amounts by NAME\n=====================\n\n"} {amount[$1] += $2} END {for (i in amount) {printf "%-40s$%8.2f\n",i,amount[i];total+=amount[i]}} END {printf "\n%-40s$%8.2f\n","Totals:",total}' input-file