summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2022-11-15 09:53:20 -0500
committerBen Sima <ben@bsima.me>2022-11-15 09:53:20 -0500
commit4eae061536f4c83088bd8fb6c89e215da2c13c06 (patch)
tree6ae64bdf333328b538289f6f7ba262e644c84749
parent8723010e43c7eb10d047692f9ae6d613a0d629ef (diff)
add rolling averages script
rather handy for calculating stuff from hledger output
-rwxr-xr-xrollingavg21
1 files changed, 21 insertions, 0 deletions
diff --git a/rollingavg b/rollingavg
new file mode 100755
index 0000000..c825fc0
--- /dev/null
+++ b/rollingavg
@@ -0,0 +1,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