commit bc1f67c6553fe9c2baf81320d14e2b4a7301d4d2
parent 2358e2b5090291dcb3b22e01b8dbd6e6bfb4c68f
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Sun, 22 Jun 2025 16:44:37 -0500
Simplify and improve to allow easy addition of grouping
You can now set the trim variable to limit account depth to it's
value, summing any sub-accounts into the parent. This also allows
the easy addition of a method of generating a total.
Diffstat:
| M | costs.awk | | | 55 | +++++++++++++++++++++++++++++++++++++++++-------------- | 
1 file changed, 41 insertions(+), 14 deletions(-)
diff --git a/costs.awk b/costs.awk
@@ -28,18 +28,26 @@ BEGIN {
 	t["quarterly"] = t["monthly"] * 3;
 	t["yearly"] = 365;
 
-	if (! ARGV[1]) {
-		output = "daily";
-	} else {
-		output = ARGV[1];
-		if (!(output in t))
-			die(output " not a valid period");
+	output = "daily";
+
+	if (ARGC > 1) {
+		if (ARGV[1] != "-") {
+			output = ARGV[1];
+			if (!(output in t))
+				die(output " not a valid period");
+		}
 		delete ARGV[1];
-		--ARGC;
+		if (ARGC > 1) {
+			prefix = ARGV[2];
+			delete ARGV[2];
+			if (ARGC > 3) {
+				die("usage: costs [period [prefix]]")
+			}
+		}
 	}
 }
 
-/^[^# ]/ && ! /^$/ {
+/^[^# ]/ && ! /^$/ && (prefix == "" || index($name, prefix) == 1) {
 	m = 1
 	n = split($period, ts, "/")
 	if (n > 2 || !(ts[1] in t) || (n > 2 && ts[2] == 0))
@@ -50,15 +58,34 @@ BEGIN {
 	x = t[ts[1]] * m
 
 	daily = $amount / x
-	if (maketotal)
-		total += daily * t[output];
-	else
-		print($name, daily * t[output]);
+
+	if (maketotal) {
+		group = "total"
+	} else if (!trim) {
+		group = $name
+	} else {
+		n = split($name, a, "/")
+		if (n <= 1) {
+			group = $name
+		} else {
+			group = a[1]
+			for (i = 2; i <= trim && i <= n; ++i) {
+				group = group "/" a[i]
+			}
+		}
+	}
+
+	groups[group] += daily
+	if (!(group in ordered)) {
+		ordered[group] = 1
+		order[++oi] = group
+	}
 }
 
 END {
 	if (status)
 		exit 1
-	if (maketotal)
-		print("total", total);
+	for (i = 1; i <= length(order); ++i) {
+		print(order[i], groups[order[i]] * t[output])
+	}
 }