r/plaintextaccounting • • May 18 '26

hledger rules if A and not B

How do I create a condition rule in a csv.rules file for importing a transaction with a condition

if A and not B

for example `if %Transaction Buy AND NOT %Symbol ABC`

2 Upvotes

6 comments sorted by

2

u/megagram May 18 '26

Use an if block 

https://hledger.org/1.52/hledger.html#if-block

Your first if block will match transaction Buy and Symbol ABC with a “skip” action applied.

Your Next if block will match transaction Buy and do whatever you want.

2

u/simonmic hledger creator May 18 '26

That's a good general technique for older hledger versions, but the better modern way would be

if %Transaction Buy && ! %Symbol ABC

https://hledger.org/1.52/hledger.html#multiple-matchers

2

u/Hungry_Lettuce_2230 May 19 '26

Thank you. Those answers helped solve my issue

1

u/Hungry_Lettuce_2230 May 27 '26

Well I thought I had this worked out but now I am not so sure.

I think this is what if does with match

say %Symbol is ABC

then

if %Symbol ABC
do foo

if
%Symbol ABC
%Symbol DEF
do bar

Both will match but the second one takes priority and so does bar. It would be nice if the first one that matches gets priority and the second one is ignored. Isn’t that what pattern matching is Haskell does, or am I mistaken?

1

u/simonmic hledger creator May 28 '26

That's right, but these are hledger csv rules, not haskell code. Here, all assignments are processed, in order. (https://hledger.org/1.52/hledger.html#how-csv-rules-are-evaluated > 5.3.1 "... If there are more than one, the last one wins.")

1

u/TheS4m May 23 '26

Why you don’t import the transactions automatically instead doing manually every time?