-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalance
More file actions
47 lines (41 loc) · 1.31 KB
/
Copy pathbalance
File metadata and controls
47 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env -S gawk -f
# Calculates balances of accounts from preprocessed postings.
# Usage:
# postings | rg "$pattern" | balance
#
# Environment:
# PTA_DEPTH collapse accounts to this depth (e.g. 2 turns
# expenses:food:groc → expenses:food)
BEGIN { OFS = "\t" }
# Hot path: just sum into full account name
{ balances[$3] += +$2 }
END {
depth = ENVIRON["PTA_DEPTH"]
# Collapse to requested depth (done once, not per-posting)
if (depth != "") {
for (acct in balances) {
n = split(acct, p, ":")
if (n > depth) {
collapsed = p[1]
for (i = 2; i <= depth; i++)
collapsed = collapsed ":" p[i]
balances[collapsed] += balances[acct]
delete balances[acct]
}
}
}
# Propagate amounts to parent accounts
for (account in balances) {
parent = account
while (sub(/:[^:]*$/, "", parent))
balances[parent] += balances[account]
}
# Print sorted; total = sum of top-level accounts only
PROCINFO["sorted_in"] = "@ind_str_asc"
for (account in balances) {
printf("%12.2f %s\n", balances[account], account)
if (account !~ /:/)
total += balances[account]
}
printf("%12.2f total\n", total)
}