hledger icon indicating copy to clipboard operation
hledger copied to clipboard

[CSV rules] Custom field assignment under if statement

Open artkpv opened this issue 5 years ago • 7 comments

Hi, thanks for this brilliant tool!

Problem How to assign a value to a custom field (not hledger one)? So far it breaks with strange error.

Motivation I've tried to parse a complex csv file from a bank (broker). It has different fields for in and out amounts which can be both non empty (two transactions) and different currencies. Also in my hledger journals I use other symbols for currencies and placement: 1000 D (instead of $1000), i.e. after amount.

My csv file:

agreement	currency		Дата	Операция				Сумма зачисления	Сумма списания	В т.ч.НДС (руб.)	Остаток (+/-)	в т.ч. гарант. обеспечение	в т.ч. депозитная маржа	Площадка		Примечание				Промежуточный клиринг (FORTS)
myaggrementname от 06.04.2020	Рубль		11.06.20	"""Проценты по займам """"овернайт ЦБ"""""""				0.04						ММВБ										

My rules file:


separator TAB

skip 1

fields  aggrement, rc, , date, tname, , , , t_in, t_out

if %rc USD
 mycurrency D

if %rc Рубль
 mycurrency R

if %t_in .+
 amount-in %t_in %mycurrency

if %t_out .+
 amount-out %t_out %mycurrency

Steps to reproduce. Just run

[I] ➜ hledger -f test.csv print --rules-file csv.rules
hledger: user error (csv.rules:8:1:
  |
8 | if %rc USD
  | ^
unexpected 'i'
expecting end of input
)

Misc. hledger 1.17.1.1 (The same under 1.18)

artkpv avatar Jun 20 '20 12:06 artkpv

Hi @artkpv,

sorry but you can't assign to a custom field name, only to one of the standard hledger field names. https://hledger.org/csv.html#field-assignment

simonmichael avatar Jun 20 '20 15:06 simonmichael

It would be nice to improve the error message here.

simonmichael avatar Jun 20 '20 15:06 simonmichael

I would second this wish. The use case I have is related to #1061 — but the custom fields I want to match and then use somewhere would be mostly tags. For currencies as in the OP's example here I can't help but think there is already a better solution than custom variable to track a commodity name.

alerque avatar Jun 20 '20 17:06 alerque

It would be nice to improve the error message here.

After #1262 the error becomes much nicer:

hledger: user error (/tmp/test.rules:8:3:
  |
8 |   mycurrency D
  |   ^^^^^^^^^^^^
unexpected "mycurrency D"
expecting field assignment
)

adept avatar Jun 20 '20 20:06 adept

Hi @artkpv,

sorry but you can't assign to a custom field name, only to one of the standard hledger field names. https://hledger.org/csv.html#field-assignment

So should this rule work then?

if ! %from ""
    description %from | %to %note

if %from ""
    description %to | %name

I want to set description depending on if from is empty (for incoming funds).

Thaodan avatar Aug 22 '25 12:08 Thaodan

I'm not sure if "" does what you want there, I would usually use ^$ to detect an empty field. Or, perhaps just

if %from .
    description %from | %to %note

if ! %from .
    description %to | %name

simonmichael avatar Aug 22 '25 14:08 simonmichael

Thanks that worked much better, remaining errors are due to my bank not exporting all the data into CSV.

Thaodan avatar Aug 22 '25 16:08 Thaodan