Given this list of names and numbers:
Kirilienko:47
James:23
Bryant:24
Durant:35
Griffin:32
How would I find sum the numbers in the second field and print them in an awk script, in the sentence:
print "The total number of these five players is [161]."
I've done a bit of research and saw the general way to sum a column was with awk {sum += $3} END{print sum}
. I've tried to implement this in my script, only to return sum = 0.
Set the field separator as :
, and get the sum of the second field. In the END
block print the sum with desired text:
awk -F: '{sum+=$2} END{print "The total number of these five players is ["sum"]."}' file.txt
Example:
% cat file.txt
Kirilienko:47
James:23
Bryant:24
Durant:35
Griffin:32
% awk -F: '{sum+=$2} END{print "The total number of these five players is ["sum"]."}' file.txt
The total number of these five players is [161].