Basically I have to process a text file which has been sorted this way:
John 12
John 13
John 10
John 900
Peter 20
Peter 30
Peter 32
The first column is a name, and the second an arbitrary value, both delimited by a space. How can I sum them up such that it would become:
John 935
Peter 82
Preferably, this should be done in awk. I have actually written a simple function in bash to do that but it was horribly inefficient:
while read rec; do
name=`echo $rec | cut -f1 -d" "`
value=`echo $rec | cut -f2 -d" "`
if [ "$name" = "$old_name" ]; then
let "total=total+value"
#for the first iteration, we have no old_name to print
elif [ "$old_name" = "" ]; then
total=$bytes
old_IP=$next_IP
else
echo "$old_IP $total"
total=$bytes
old_IP=$next_IP
fi
done < textfile
Thank you in advance.