summaryrefslogtreecommitdiff
path: root/rollingavg
blob: c825fc03eab7f85596e4ac7d10a3c3956a4059a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env bash
#
# calculates rolling average for numbers via stdin
#
# here's an example with hledger:
#
# hledger bal ^in cur:USD -MA --end=thismonth --output-format=csv \
#   | tail -n 1 \
#   | cut -d',' -f 2- \
#   | tr -d '" USD-' \
#   | tr ',' '\n' \
#   | rollingavg
n=0
total=0
declare -i n
while read i
do
    n+=1
    total=$(bc -l <<< "$total + $i")
    printf "%.2f\n"  $(bc -l <<< "$total / $n")
done