On Wednesday, January 2, 2013 11:27:28 AM UTC-8, Craig Earls wrote:

> My company has changed 401K service providers and I have a problem I can't 
> figure out. For a number of reasons that seemed reasonable up to now I have 
> enter many transaction by having ledger calculate the number of shares of 
> a commodities  like this:
>
> 2012/03/27 * (DD) Earnings
>     Assets:Investments:401K:Deferred    (198.82/106.30 VIFSX) @ $106.30
>     Assets:Investments:401K:Matching    (99.40/106.30 VIFSX) @ $106.30
>     Assets:Investments:401K:Profit Sharing  (6.38/106.30 VIFSX) @ $106.30
>     Income:Exempt:Dividends
>
> This was fine up until now.  I want to sell all shares out of an account, 
> but since I have used division there are MANY digits of precision that I 
> don't really want to deal with.  
>
... 

> Any ideas here?
>

 If I were faced with the problem I'd probably handle it by trying to 
massage the data into the new shape I wanted.  Given how ledger can hook 
into python, if you know that language it might be easy to fix it up.  For 
example, if I were doing it I'd probably reach for awk, starting with 
something like this:

$ cat sample.awk 
{
    if ($1 !~ /401K/ || $(NF-3) !~ /\//) {
print $0;
next;
    }

    stock=$(NF-2);
    sub(/\)/, "", stock);

    ratio=$(NF-3);
    sub(/\(/, "", ratio);

    split(ratio, arr, "/");
    units=arr[1];

    printf("    ");
    for (i = 1; i <= NF; i++) {
if (i == (NF-3)) {
    printf("    %s", units);
} else if (i == (NF-2)) {
    printf(" %s", stock);
} else if (i == (NF-1)) {
    printf(" @@");
} else {
    if (i > 1) {
printf(" ");
    }
    printf("%s", $i);
}
if (i == NF) {
    printf("\n");
}
    }
}
$ cat sample.dat 
2012/03/27 * (DD) Earnings
    Assets:Investments:401K:Deferred    (198.82/106.30 VIFSX) @ $106.30
    Assets:Investments:401K:Matching    (99.40/106.30 VIFSX) @ $106.30
    Assets:Investments:401K:Profit Sharing  (6.38/106.30 VIFSX) @ $106.30
    Income:Exempt:Dividends
$ awk -f sample.awk sample.dat 
2012/03/27 * (DD) Earnings
    Assets:Investments:401K:Deferred    198.82 VIFSX @@ $106.30
    Assets:Investments:401K:Matching    99.40 VIFSX @@ $106.30
    Assets:Investments:401K:Profit Sharing    6.38 VIFSX @@ $106.30
    Income:Exempt:Dividends


Reply via email to