Arithmetic with array elements in bash

10,760

Solution 1

Try using expr to add two expression something like:

sum=$(expr "$sum" + "${arr[i]}")

Or

sum=$((sum + arr[i]))


echo "11 13" >test.txt 
echo "12" >>test.txt

while read -a line; do ##read it as array
    sum=0
    for ((i=1; i < ${#line}; i++)); do ##for every number in line
       sum=$(expr "$sum" + "${line[i]}") ## add it to sum
    done
    echo $line[0] $sum ##print sum
done < test.txt
Output
36

After OP's edit:

echo "ABC 11 13" >test.txt echo "DEF 12" >>test.txt

while read -a line; do ##read it as array
sum=0
for ((i=1; i < $((${#line[@]})); i++)); do ##for every number in line
   sum=$(expr "$sum" + "${line[i]}") ## add it to sum
   if [[ $i -eq $((${#line[@]}-1)) ]]
   then
       echo "${line[0]} $sum" ##print sum
       sum=0
   fi
done
done < test.txt
Output:
ABC 24
DEF 12

Solution 2

If you want to sum the numbers in each lines of the file using a loop in bash you could do

#!/bin/bash
while read line; do
    array=($line);
    sum=0
    length=${#array[@]}
    for i in ${array[@]:0:$length}; do
       sum=$[$sum+$i]
    done
    echo $sum
done < "$1"

The difference with your code is that i is the element in the array, not the index.

However, possessing files in bash is rather slow. You would be probably better off to do the task in awk, like this for example:

awk '{s=0;for(i=1;i<=NF;i++) s+=$i;print s}' file
Share:
10,760
Vaderico
Author by

Vaderico

Updated on June 19, 2022

Comments

  • Vaderico
    Vaderico about 2 years

    I'm using bash and trying to add all elements of an array that was created from a file.

    while read line; do
        array=($line);
        sum=0
        length=${#array[@]}
        for i in ${array[@]:0:$length}; do
           sum=$[$sum+${array[i]}]   #<--- this doesn't work?
        done
        echo $sum
    done < $1
    

    edit: I should have been clearer why i want to use array splitting in for loop

    The input may be ------> david 34 28 9 12

    And I want to print ---> david 83

    So I would like to loop through all elements accept the first one. so i would use:

    length=$[${#array[@]} - 1]
    for i in${array[@]:1:$length}
    

    because of this i can't use:

    for i in "${array[@]}"
    
  • Vaderico
    Vaderico about 9 years
    sum=$(expr "$sum" + "${arr[i]}") : gives me an error. sum=$((sum + arr[i])) : doesnt work :/ and gives no error
  • SMA
    SMA about 9 years
    Ihave modified your script.
  • Vaderico
    Vaderico about 9 years
    I can't use that for loop, I explain why in the edited question.
  • Vaderico
    Vaderico about 9 years
    I can't use that for loop, I explain why in the edited question.
  • SMA
    SMA about 9 years
    how about for i in "${!line[@]}"; do or for ((i=0; i < ${#line}; i++)) or for ((i=0; i < ${length}; i++)) Also dont keep on editing the question with additional requirements.
  • user000001
    user000001 about 9 years
    @Vaderico Still i is the value, not the index. See edit. Also you have a typo in the question. It should be length=${#array[@]} and not length=${#arr[@]}
  • Vaderico
    Vaderico about 9 years
    It isn't the loop that is the problem, I am successfully itereating from index 1 to array length. It's just the arithmetic that is the problem. It isn't accepting the addition of an integer and a array string number.
  • Vaderico
    Vaderico about 9 years
    okay that works aswell, thanks for the help! much appreciated