Question about csv rules

Hi all,
I'm trying without luck to import a csv file with a lot of similar descriptions, John, John Doe, John Martin Doe....
When I write a block

if John
account1 Assets:Bank1

this matches John, John Doe, John Martin Doe...

Is there a way to match only the records which contain John and not John+something else?

My regards

Here’s two ways:

if ,john,
if %description ^john$
1 Like

Hi Michael,
i trusted your answer before testing it.

It doesn't work for me

sample.rules

account1 Assets:MyAccount
date %1
date-format %d/%m/%Y
description %3
amount %2
currency $

if groc martin less 
	account2 Revenues:martingrocless

if groc less
	account2 Revenues:grocless


if %description ^groc$
	account2 Revenues:groc

sample.csv

10/05/2024,200,Groc less,
10/06/2024,300,Groc martin less
10/07/2024,100,Groc

output of hledger import --dry-run --rules=sample.rules sample.csv


; would import 3 new transactions from sample.csv:

2024-05-10 Groc less
    Assets:MyAccount             $200
    Revenues:grocless           $-200

2024-06-10 Groc martin less
    Assets:MyAccount                   $300
    Revenues:martingrocless           $-300

2024-07-10 Groc
    Assets:MyAccount            $100
    income:unknown             $-100

hledger --version hledger 1.40, linux-x86_64

I forgot...
if I put

if groc (naked)

at the top of the rules file, it works as expected

What i'm doing wrong?

My kind regards

Hi @trebestie, thanks for testing and for the small example. I learned something new today! Using a field name to restrict an if condition works only if the field was named by a fields list - not if it was assigned individually.

To make it work I had to change the above to either

if %3 ^groc$

or

fields date,amount,description
if %description ^groc$

And I see the if ,groc, variant wouldn't work above, because there's no comma after Groc, but there is an extra comma in one of the other records, so I can't assume if ,groc$ would be ok.

When you put if groc at the top, here's what happened: that matches all of the records, then the more specific rules below override it for records 1 and 2.

Noted as csv: separately-assigned field name can't be used to restrict an if condition · Issue #2289 · simonmichael/hledger · GitHub

1 Like