I wrote something along the lines of this for income statements. YMMV
--- I'm definitely not an accountant or any sort of financial
professional.
```
#!/bin/bash
# This script generates an income statement for the given year.
# This is designed to be run by apenwarr's redo.
# To use it standalone, make it executable and give it
# three arguments:
#
# - $1: (doesn't matter)
# - $2: the name of the file minus the extension (should be a year with a corresponding .journal file)
# - $3: the name of a temp file which will hold the output
#
# You'll have to make changes to suit your own needs.
name="Your name here"
year=$(basename $2)
redo-ifchange $year.journal
# Change these commands to suit your needs ########################
cmd="ledger -f $year.journal -HX $ -p $year bal"
income=$($cmd ^Income --account "tag('country')" --invert)
expenses=$($cmd ^Expenses)
total=$($cmd ^Income ^Expenses --invert | tail -n 2 | sed -e '1s/-/=/g' -e '2s/$/ Net Income/')
# Go to the end of the year if year is past
if test "$(date +%Y)" -le "$year"; then
todate=$(date +%D)
else
todate=$(date -d $year-12-31 +%D)
fi
intro="This document is the income statement for $name for the calendar
year of $year to $todate. Amounts in CNY have been converted to USD at their
historical prices according to the time of transaction."
printf "Income statement for %d to %s\n" $year $todate >> $3
( printf "%39s\n" | tr " " = ) >> $3
printf "\n%s\n" "$intro" "$income" "$expenses" "$total" >> $3
unix2dos $3
mv $3 $(dirname $3)/$(basename $3 .tmp)
```
For example, with the following journal file:
```
; 2022.journal
2022-01-01 Employer
Assets:Bank $2,000.00
Income:Employer $-2,000.00
2022-01-02 Paycheck Party!
Expenses:Beer $200.00
Expenses:Food $100.00
Assets:Bank $-300.00
2022-01-03 Savings
Assets:Savings $1,000.00
Assets:Bank $-1,000.00
```
Running `./incomestmt.sh '' 2022 2022.income.txt.tmp`, I get the following:
```
Income statement for 2022 to 02/20/22
=======================================
This document is the income statement for Your name here for the calendar
year of 2022 to 02/20/22. Amounts in CNY have been converted to USD at their
historical prices according to the time of transaction.
$2,000.00 Income:Employer
$300.00 Expenses
$200.00 Beer
$100.00 Food
--------------------
$300.00
====================
$1,700.00 Net Income
```
Hope this helps someone!