I have an input file like this:
bread,5
water,15
butter,5
I want to write a script which will get the numbers from each line after the comma and add them. So the example output must be: 25
Use awk,
$ awk -F, '{c+=$2}END{print c}' file
25
Here -F, we are setting the Field Separator as comma. {c+=$2} would add every num exists on the second column to a variable called c . c+=$2 is equal to c = c + $2. At the last c contains the total sum of all the numbers exists on the second column. Printing c in the END block will give the final value of c variable.