Skip to main content

Closing out the books in ledger(1)

I put this here mostly because I forget how to do it and have to make multiple starts to get it right.

ledger -f ledger.txt  print \
  -e 2022-1-1 > 2021.txt

ledger -f ledger.txt equity
  -e 2022-1-1 > tempfile.txt

 echo >> tempfile.txt

ledger -f ledger.txt print \
  -b 2022-1-1 >> tempfile.txt

mv tempfile.txt ledger.txt
Closing out the books for 2021

Note: I keep my ledger files in git so I have a fair bit of freedom to experiment. If I mess things up beyond repair, I can always git reset --hard to revert to the most recent snapshot. I recommend you make a backup as well.

The first step pulls all transactions ending (-e) before 2022-1-1 into 2021.txt as an archive.

Next, create a temporary file containing the Opening Balances for the period ending (-e) before 2022-1-1 using the equity command. Then echo adds a blank line before transactions start (note the >> to append to this file rather than overwrite it). Finally, print all the transactions beginning (-b) on 2022-1-1 into our temporary file.

At this point, we can move the tempfile.txt back atop the original transaction log.

You may need to do some cleanup of the Opening Balances to check for unexpected currency issues (I run in --pedantic mode).