DEV Community

Discussion on: Daily Challenge #4 - Checkbook Balancing

Collapse
v613 profile image
Ceban Dumitru • Edited on

BASH

#!/bin/bash
input='challenge.txt';
declare -a checkbook;
total=0;
for line in $(cat ${input}|tr -cd [' ','0-9','.','A-Z','a-z','\n']);do
    checkbook+=("${line}");
done;

echo "Original_Balance: "${checkbook[0]};
for (( i = 1; i < ${#checkbook[@]}; i+=3 )); do
    echo ${checkbook[@]:${i}:3};
    total=`echo "scale=2;${total} + ${checkbook[${i}+2]}" |bc`;
done;
average=`echo "scale=2;${total}/((${#checkbook[@]}-1)/3)"|bc`;

echo "Total expense: " ${total};
echo "Average expense: " ${average};

result