CSV rule for default value if field value is missing/empty

I have just begun using hledger and am just beginning to wrap my head around the deep feature set of the tool. The documentation and examples have also been excellent and I feel like I’ve been able to get pretty far using them.

I have a CSV file where some field values at the end of a record may be missing or empty.

If the value is missing, I would like a rule to substitute in a default value.

In the rules file, I have a field defined as category using the fields directive.

I have tried matching an empty field with "", blank space, or the regex ^$.

How do you match an empty/missing field value?

Thank you.

if
MY_PAYEE
&& %category ""
    account2 Expenses:Default Category

or

if
MY_PAYEE
&& %category ^$
    account2 Expenses:Default Category

Welcome @dempsey. ^$ is the correct way, but are you sure the field is empty - could there be whitespace ?

Another approach is to first set a default value with a fixed assignment, followed by conditional rules which override that.

I’ve tried ^\s*$

This seems to match empty, blank, and whitespace only fields that I can ignore as missing data.

In a hledger CSV rule ? I would expect & to match & and \s* to match s or nothing, according to https://hledger.org/hledger.html#hledgers-regular-expressions ...

Oops - typo! (correcting original post)

^\s*$ matches start, 0 or more whitespace, end

BUT - apparently \s is not “whitespace” in this flavor of regex.

^ *$ would work for plain spaces (I think).

1 Like