Negating a string using PRINT

47 views
Skip to first unread message

jonath...@gmail.com

unread,
Feb 22, 2020, 1:56:17 PM2/22/20
to Ledger
Is there a way to negate a string when using the PRINT command?  I am trying to combine two files that have some overlap in a particular account, so in the first file I want to PRINT it and exclude that account.  So I tried using the command:

ledger print not "Account:Particular" > file.dat

But then file.dat still has all the entries from that account.

Is there a way to use PRINT and exclude transactions matching a string? 

John Wiegley

unread,
Feb 22, 2020, 2:15:12 PM2/22/20
to jonath...@gmail.com, Ledger
>>>>> "jp" == jonathan prin <jonath...@gmail.com> writes:

jp> Is there a way to use PRINT and exclude transactions matching a string? 

Please give this a try:

ledger print --limit '! (account =~ /Account:Particular/)'

John

Jonathan Prin

unread,
Feb 23, 2020, 8:30:28 PM2/23/20
to Jonathan Prin, Ledger
That didn't seem to work, unfortunately.  Just in case I'm making a mistake elsewhere in the command, here's a copy of what I tried, substituting the word "Bank" for my actual bank.

ledger print -f input_file.dat --limit '! (account =~ /Assets:Checking:Bank/)' > results.ledger
ledger print -f input_file.dat --limit '! (account =~ /Checking:Bank/)' > results.ledger
ledger print -f input_file.dat --limit '! (account =~ /Bank/)' > results.ledger

And in each case results.ledger was identical to input_file.dat.

I appreciate the response.  If you have any other suggestions, I'd be keen to hear them.

JP

John Wiegley

unread,
Feb 24, 2020, 12:52:51 PM2/24/20
to Jonathan Prin, Ledger
>>>>> "JP" == Jonathan Prin <jonath...@gmail.com> writes:

JP> That didn't seem to work, unfortunately.  Just in case I'm making a
JP> mistake elsewhere in the command, here's a copy of what I tried,
JP> substituting the word "Bank" for my actual bank.

Ah, I see, print prints the whole transaction, which does not allow you to
omit specific postings from it.

My advice at this point would be to use the `xml` report, for example, modify
it, read it back in (Ledger can read in that same data directly), and then
print that result.

Allowing this sort of XSLT-based transformation of reports is sort of why the
'xml' report exists.

John

Yuri Khan

unread,
Feb 24, 2020, 1:34:42 PM2/24/20
to Jonathan Prin, Ledger
On Tue, 25 Feb 2020 at 00:52, John Wiegley <jwie...@gmail.com> wrote:
>
> Ah, I see, print prints the whole transaction, which does not allow you to
> omit specific postings from it.

Omitting specific postings from transactions would not make sense
except in specific circumstances, as the transaction would not
balance.

As far as I understand, the OP is asking about omitting the whole
transaction if any of its postings matches a condition. This can be
accomplished with:

$ ledger print expr 'not any(account =~ /^Account:Particular$/)'

John Wiegley

unread,
Feb 24, 2020, 3:07:45 PM2/24/20
to Yuri Khan, Jonathan Prin, Ledger
>>>>> "YK" == Yuri Khan <yuri....@gmail.com> writes:

YK> Omitting specific postings from transactions would not make sense except
YK> in specific circumstances, as the transaction would not balance.

YK> As far as I understand, the OP is asking about omitting the whole
YK> transaction if any of its postings matches a condition. This can be
YK> accomplished with:

YK> $ ledger print expr 'not any(account =~ /^Account:Particular$/)'

Very good point, thank you Yuri!

John

Jonathan Prin

unread,
Feb 24, 2020, 11:00:05 PM2/24/20
to Yuri Khan, Ledger
Yuri - this seems to be exactly the answer I was looking for.  Thank you!

John - I apologize for causing confusion by being unclear.  As Yuri stated, I was trying to find a way to omit the entire transaction when any posting matched a condition. 

The context for this (which I probably should have posted originally) is that I have a file full of credit card transactions and a file full of checking transactions.  I would like to combine the two files, but because I use the checking account to pay off the credit card, if I combine them I will have double counted the payments from the checking account to the credit card.  To correct that I wanted to strip out the checking transactions from the credit card file, so that when I combined the files everything would end up with the correct ending balances.  Yuri's answer let me do that. 

Thank you to you both!


Reply all
Reply to author
Forward
0 new messages