Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic with array elements in bash

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[@]}"
like image 741
Vaderico Avatar asked Mar 29 '15 06:03

Vaderico


People also ask

How do you sum an element of an array in bash?

Explanation: "${arr[@]/%/ +}" will return 1 + 2 + 3 + By adding additional zero at the end we will get 1 + 2 + 3 + 0. By wrapping this string with BASH's math operation like this $(( "${arr[@]/%/ +} 0")) , it will return the sum instead.

Can we perform arithmetic operations in array?

Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character ( . ) distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs .

Can you do math in bash?

Bash offers a wide range of arithmetic operators for various calculations and evaluations. The operators work with the let , declare , and arithmetic expansion. Below is a quick reference table that describes Bash arithmetic operators and their functionality.


2 Answers

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
like image 31
user000001 Avatar answered Sep 28 '22 03:09

user000001


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
like image 187
SMA Avatar answered Sep 28 '22 02:09

SMA