I am trying to add integers together from multiple rows while maintaining the data that comes before the number.
Here is my raw data:
"2020-06","28347","Afghanistan","791","anonymous","3128"
"2020-06","28347","Afghanistan","830","anonymous","402"
"2020-06","28347","Afghanistan","10019","anonymous","79"
"2020-06","28347","Afghanistan","10070","anonymous","829"
"2020-06","28347","Afghanistan","10604","anonymous","4319"
"2020-06","28347","Albania","266","anonymous","60"
"2020-06","28347","Albania","824","anonymous","23"
"2020-06","28347","Albania","10163","anonymous","166"
"2020-06","28347","Algeria","267","anonymous","11047"
And here is what I expect my output to be:
28347,Afghanistan,8757
28347,Albania,249
28347,Algeria,11047
So far what I have done is I'm extracting the second and third columns out of the data, then trying to loop through each one with a grep and add together the values. Unfortunately I get a total combined value instead of the values for each country.
COUNTRIES=$(awk -F\, '{OFS=",";}{print $2,$3}' file.dat | sort | uniq)
for COUNTRY in "${COUNTRIES[@]}"
do
NUMBER=$(grep $COUNTRY file.dat | awk -F\, '{print $6}' | sed 's/\"//g' | awk '{s+=$1} END {print s}')
echo "$COUNTRY,$NUMBER" | sed 's/\"//g'
done
Which gives me
28347,Afghanistan
28347,Albania
28347,Algeria,20053
I'm not quite sure why it is giving me the whole total instead of the total per country. Any ideas?