Sorting lots

131 views
Skip to first unread message

Kumar Appaiah

unread,
Jan 4, 2017, 6:17:49 AM1/4/17
to Ledger
Hello.

When I display the lots of commodities in my account, I get something like this:

# ledger bal --lots "Account" -f myfile.dat -S date -y "%Y-%m-%d"


3.254 EQMF119581 {300.2 INR} [2014-12-01]
2.596 EQMF119581 {308.0 INR} [2016-02-17]
9.480 EQMF119581 {311.6 INR} [2016-03-08]
94.791 EQMF119581 {313.4 INR} [2016-01-21]
9.460 EQMF119581 {317.3 INR} [2015-05-07]
3.141 EQMF119581 {318.4 INR} [2015-01-01]
9.271 EQMF119581 {323.6 INR} [2016-02-08]
9.193 EQMF119581 {326.3 INR} [2015-06-08]

This is sorted in price. However, it would be much more useful if I could sort it in terms of date for further processing in a script, since I need FIFO lot details. Is there a way to get date sorted lot outputs directly from ledger?

Thanks.

Kumar

John Wiegley

unread,
Jan 4, 2017, 3:34:35 PM1/4/17
to Kumar Appaiah, Ledger
>>>>> "KA" == Kumar Appaiah <a.k...@alumni.iitm.ac.in> writes:

KA> When I display the lots of commodities in my account, I get something like
AK> this:

AK> # ledger bal --lots "Account" -f myfile.dat -S date -y "%Y-%m-%d"

AK> This is sorted in price. However, it would be much more useful if I could
AK> sort it in terms of date for further processing in a script, since I need
AK> FIFO lot details. Is there a way to get date sorted lot outputs directly
AK> from ledger?

Hi Kumar,

I've just pushed a fix to the 'next' branch which allows you to say:

ledger bal --lots "Account" -f myfile.dat \
-S 'lot_date(total) || date' -y "%Y-%m-%d"

It was an oversight that sort expressions did not have access to the current
reporting scope. Now you can sort on the lot date of the total, or the date if
no lot date exists.

--
John Wiegley GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com 60E1 46C4 BD1A 7AC1 4BA2

Kumar Appaiah

unread,
Jan 4, 2017, 8:30:47 PM1/4/17
to Ledger, a.k...@alumni.iitm.ac.in, jo...@newartisans.com
On Thursday, January 5, 2017 at 2:04:35 AM UTC+5:30, John Wiegley wrote:
>>>>> "KA" == Kumar Appaiah <a.k...@alumni.iitm.ac.in> writes:

KA> When I display the lots of commodities in my account, I get something like
AK> this:

AK> # ledger bal --lots "Account" -f myfile.dat -S date -y "%Y-%m-%d"

AK> This is sorted in price. However, it would be much more useful if I could
AK> sort it in terms of date for further processing in a script, since I need
AK> FIFO lot details. Is there a way to get date sorted lot outputs directly
AK> from ledger?

Hi Kumar,

I've just pushed a fix to the 'next' branch which allows you to say:

  ledger bal --lots "Account" -f myfile.dat \
      -S 'lot_date(total) || date' -y "%Y-%m-%d"

It was an oversight that sort expressions did not have access to the current
reporting scope. Now you can sort on the lot date of the total, or the date if
no lot date exists.


Dear John,

Thanks for adding this feature.

Kumar

Manuel Amador (Rudd-O)

unread,
Jan 5, 2017, 3:40:49 PM1/5/17
to ledge...@googlegroups.com
On 01/05/2017 01:30 AM, Kumar Appaiah wrote:
> On Thursday, January 5, 2017 at 2:04:35 AM UTC+5:30, John Wiegley wrote:
>
> >>>>> "KA" == Kumar Appaiah <a.k...@alumni.iitm.ac.in
> <javascript:>> writes:
>
> KA> When I display the lots of commodities in my account, I get
> something like
> AK> this:
>
> AK> # ledger bal --lots "Account" -f myfile.dat -S date -y "%Y-%m-%d"
>
> AK> This is sorted in price. However, it would be much more useful
> if I could
> AK> sort it in terms of date for further processing in a script,
> since I need
> AK> FIFO lot details. Is there a way to get date sorted lot
> outputs directly
> AK> from ledger?
>
> Hi Kumar,
>
> I've just pushed a fix to the 'next' branch which allows you to say:
>
> ledger bal --lots "Account" -f myfile.dat \
> -S 'lot_date(total) || date' -y "%Y-%m-%d"
>
> It was an oversight that sort expressions did not have access to
> the current
> reporting scope. Now you can sort on the lot date of the total, or
> the date if
> no lot date exists.
>
>
> Dear John,
>
> Thanks for adding this feature.

Was documentation added for this feature?

These are the sort of things that are sorely underdocumented about ledger.

John Wiegley

unread,
Jan 5, 2017, 3:46:28 PM1/5/17
to Manuel Amador (Rudd-O), ledge...@googlegroups.com
>>>>> "MA(" == Manuel Amador (Rudd-O) <rud...@rudd-o.com> writes:

MA(> Was documentation added for this feature?

It wasn't a new feature, I simply extended the value expression scope of the
sort expression to include the current report (and thus, access to the
lot_date function).

MA(> These are the sort of things that are sorely underdocumented about
MA(> ledger.

Yes, most things relating to value expressions are sorely underdocumented.

thierry

unread,
Jan 16, 2017, 1:57:36 PM1/16/17
to Ledger, a.k...@alumni.iitm.ac.in, jo...@newartisans.com
Hi,

I wanted to implement some kind of regress test file, not fully documenting the option, but at least keeping trace of the syntax of the command.

I tried on my own ledger file the command:


    ledger bal --lots "Account" -f myfile.dat -S 'lot_date(total) || date' -y "%Y-%m-%d"

But it is failing with :

    While evaluating value expression:
      (lot_date(total) | date)
       ^^^^^^^^^^^^^^^
    While calling function 'lot_date 0':
    Error: boost::bad_get: failed value get using boost::get

Can somebody (maybe original poster Kumar Appaiah) provide me with some anonymised test file?

Thanks
Regards,
Thierry

Kumar Appaiah

unread,
Jan 17, 2017, 8:24:52 AM1/17/17
to thierry, Ledger
On Mon, Jan 16, 2017 at 10:57:36AM -0800, thierry wrote:
> Hi,
>
> I wanted to implement some kind of regress test file, not fully
> documenting the option, but at least keeping trace of the syntax of the
> command.
>
> I tried on my own ledger file the command:
>
>     ledger bal --lots "Account" -f myfile.dat -S 'lot_date(total) || date'
> -y "%Y-%m-%d"
>
> But it is failing with :
>
>     While evaluating value expression:
>       (lot_date(total) | date)
>        ^^^^^^^^^^^^^^^
>     While calling function 'lot_date 0':
>     Error: boost::bad_get: failed value get using boost::get
>
> Can somebody (maybe original poster Kumar Appaiah) provide me with some
> anonymised test file?

This?

2012-01-31 * Buy Apples
Stocks 30 APPLES @ 12 USD
Unknown

2011-02-01 * Buy Apples
Stocks 10 APPLES @ 10 USD
Unknown

2011-02-02 * Buy Apples
Stocks 20 APPLES @ 11 USD
Unknown

Kumar
--
Kumar Appaiah

thierry

unread,
Jan 17, 2017, 1:49:59 PM1/17/17
to Ledger, th...@free.fr
Thanks Kumar, this is what was expected.
However I still have the same error, and I do not understand what is wrong... John, any idea?

$ cat /tmp/test.ledger

2012-01-31 * Buy Apples
    Stocks    30 APPLES @ 12 USD
    Unknown

2011-02-01 * Buy Apples
    Stocks    10 APPLES @ 10 USD
    Unknown

2011-02-02 * Buy Apples
    Stocks    20 APPLES @ 11 USD
    Unknown

$ ledger --args-only --file=/tmp/test.ledger bal --lots "Account" -S 'lot_date(total) || date' -y "%Y-%m-%d"

While evaluating value expression:
  (lot_date(total) | date)
   ^^^^^^^^^^^^^^^
While calling function 'lot_date 0':
Error: boost::bad_get: failed value get using boost::get



Thierry

John Wiegley

unread,
Jan 18, 2017, 7:27:16 PM1/18/17
to thierry, Ledger
>>>>> "t" == thierry <th...@free.fr> writes:

t> Thanks Kumar, this is what was expected. However I still have the same
t> error, and I do not understand what is wrong... John, any idea?

I'll take a look.

Kumar Appaiah

unread,
Apr 1, 2017, 1:27:24 AM4/1/17
to Ledger

On Thursday, January 19, 2017 at 5:57:16 AM UTC+5:30, John Wiegley wrote:
>>>>> "t" == thierry  <th...@free.fr> writes:

t> Thanks Kumar, this is what was expected. However I still have the same
t> error, and I do not understand what is wrong... John, any idea?

I'll take a look.


I get the same error. I'll try to see how to get this fixed.

Kumar

Otger Lara

unread,
Jun 29, 2022, 12:41:37 AM6/29/22
to Ledger
Hi Kumar, John and Thierry,

We were facing the same problem, and got a solution. If you're in a Linux or Mac machine, you could try

ledger -f file.dat bal account --lots | sort -t ' ' -k5

—Otger
Reply all
Reply to author
Forward
0 new messages