sum up value from textfile - bash
- by user3493435
I am trying to sum up the values in a textfile
try.txt
firstNumber,1
secondNumber,2
I tried with this script
#!/bin/bash
while IFS, read -r -a array; do
printf "%s %s\n" "${array[0]} ${array[1]}"
for n in "${array[1]}"; do
((total += n))
echo "total =" $total
done
done < try.txt
and I landed up with this output
firstNumber 1
total = 1
secondNumber 2
total = 3
expected output
firstNumber 1
secondNumber 2
total = 3
Thanks in advance