Skip to content

Maintaining CSV rules

Raphael edited this page Sep 5, 2024 · 11 revisions

Associated directory: 06-maintaining-lots-of-csv-rules

Lots and lots of rules

Eventually you will reach a point where your rulefile is so big that every time you add a new rule, you are faced with a choice: either search for an existing rule to add a new pattern to, or create a new rule, writing the boilerplate of:

if
PATTERN
  account2 ...

, which is rather wasteful and quite a lot to type.

My solution to this was to write rules in a simple pipe-separated file, and make use of the "if table" syntax that I've added to hledger 1.19.

The file would have the following format:

if|account2|comment
PATTERN1|account-that-you-want-to-use|optional-comment
PATTERN2|account-that-you-want-to-use|optional-comment
...

, where the comment could be empty (but you still need to provide tailing separator).

For example:

$ cat import/lloyds/rules.psv

if|account2|comment
INTEREST|income:interest|
EMPLOYER INC|income:employer|
TRANSFER|assets:Lloyds:transfers|
WAITROSE|expenses:groceries|
OASIS COFFEE|expenses:coffee|
AVIVA|assets:pension:aviva|

An account could also be left blank, in which case generated rule will just add a comment. This could be used to augment more general rule, for example, you can annotate a transaction on a particular date:

if|account2|comment
...
2017-10-11.*EMPLOYER INC||per diem for the trip to New York

File in this format that could be easily edited or manipulated in Excel/OpenOffice.

To add these rules to your main rules file, simply add a line saying include rules.psv:

$ cat import/lloyds/lloyds.rules

fields date,code,sortcode,account1,description,amount1-out,amount1-in,balance1
skip 1

date-format %d/%m/%Y
currency1 £
account2  expenses:unknown

include rules.psv

Lastly, add "rules.psv" to the extraDeps of llodys in export.hs.

extraDeps file
  | "//lloyds//*.journal" ?== file   = ["lloyds.rules", "rules.psv"]
  | otherwise = []

See the changes in 06-maintaining-lots-of-csv-rules or diffs/05-to-06.

Next steps

Now that importing basic statements should not be an issue anymore, we can tackle more complex subjects, such as tracking investments.