I want to read a random txt which contains some random integers in this form:
2:10
4:4
10:15
22:5
Then i want to find the sum of each column . Firstly , i thought of spliting each line like if every line is a string:
columnA="$(cut -d':' -f1 <<<$line)"
columnB="$(cut -d':' -f2 <<<$line)"
columnAcontains the elements of the first column and columnB the elements of the second one . Then i created a variable sumA=0 and i tried to take the sum of each column like that:
sumA=$((columnA+sumA))
I am getting the result i want but with this message as well
")syntax error: operand expected (error token is "
Same for the second column :
sumB=$((columnB+sumB))
The time i am getting this error and i dont get the result i want: ")syntax error: invalid arithmetic operator (error token is "
This is the code in general :
sumA=0
sumB=0
while IFS= read -r line
do
columnA="$(cut -d':' -f1 <<<$line)"
sumA=$((columnA+sumA))
columnB="$(cut -d':' -f2 <<<$line)"
sumB=$((columnB+sumB))
done < "random.txt"
echo $sumA
echo $sumB
Any thoughts?