Find total of a column of numbers with BASH

Find total of a column of numbers with BASH

Created:28 Nov 2016 20:53:05 , in  Host development

Suppose you have a file with a long list of items followed or perhaps preceded by whole numbers and you are asked to find the total of those numbers.

The file might look like this (case 1):


aaa:23
bb:2
ccc:98
.
.
.

or perhaps like this (case 2):


23,aaa
2,bb
98,ccc
.
.
.

or even like this (case 3):


aaa,23,aaa
ccc,2,bb
bb,98,ccc
.
.
.

Find column total - function

Here is a BASH function which finds you the total:


#!/usr/bin/env bash
 
function sum_it_up(){
  file="${1}"
  delimiter=${2:-":"}
  field=${3:-"2"}
  sum=0
  ints=($(cut -d ${delimiter} -f"${field}" "${file}"))
  for num in "${ints[@]}";do ((sum+=num)); done; echo $sum;
}

sum_up "${1}" "${2}" "${3}"

And below is how you would call the function to find totals for each of the three aforementioned cases (assuming the code above is in sum_it_up.sh and file with data is called to_sum_up.txt with both files being in the same directory):

case 1:


$ ./sum_it_up.sh to_sum_up.txt

case 2:


$ to_sum_up.txt "," 1

case 3:


$ ./sum_it_up.sh to_sum_up.txt "," 2

Parameters the function accepts are: file to process, [delimiter to split columns in the file at] (default is ":"), [column number to sum up] (default is 2).

Loop based quickie

Here is a more concise piece of code, based on IFS and while loop, that does exactly the same thing as the function above. It assumes data is kept in file called data and that contents of the file look like for case 3 described above.


# initial total
total=0
# column separator 
ifs=,
# column to find total of
col=col2
# data file
data_file=data

while IFS="$ifs" read -r col1 col2 col3 ; do
  (( total+=$"$col" ))
done < "$data_file"

echo $total

You can even use array. In this example the array is called cols and is referenced using ${cols[ $index - 1 ]}. Arrays in BASH, like in many other programming languages, are zero index.


# initial total
total=0
# column separator 
ifs=,
# column to find total of
index=2
# data file
data_file=data

while IFS="$ifs" read -r -a cols ; do
  (( total+=${cols[ $index - 1 ]} ))
done < "$data_file"

The examples above use some clever BASH features such as parameter expansions, read builtin or arrays. That's not even a tip of the iceberg in terms of what BASH has on offer. More articles to come soon ...

This post was updated on 27 Apr 2017 16:36:24

Tags:  BASH 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, is sWWW admin and owner.He enjoys doing Maths and studying algorithms, writing code in scripting and command languages, Thrash Metal music and playing electric guitar.

Copyrights

©Copyright, 2019 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "Find total of a column of numbers with BASH." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/find-total-of-a-column-of-numbers-with-bash