sum up value from textfile - bash

Posted by user3493435 on Stack Overflow See other posts from Stack Overflow or by user3493435
Published on 2013-10-30T03:47:58Z Indexed on 2013/10/30 3:53 UTC
Read the original article Hit count: 85

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell