Created
February 9, 2024 12:23
-
-
Save Tpaefawzen/672e2612c25a45a0693a118a7bf0c894 to your computer and use it in GitHub Desktop.
Ledgerにおいて、balance出力をSSV形式とかJSONPath-valueなのかXPath-value形式なのかよくわからないけどそれっぽい形式にする方法となるシェルスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # https://ledger-cli.org/doc/ledger3.html#Balance-Report | |
| # 貸借対照表を出力 | |
| ledger -f Ledger.txt balance | | |
| # | |
| # 1: 金額(通貨単位なし) 2: インデントで構造を表しながら勘定科目を表示 | |
| # インデントレベルを表示(2スペース単位の場合はこれ) | |
| sed ' | |
| s/\([0-9]\) /\1 A/ | |
| :L | |
| s/A /AA/ | |
| t L | |
| s/AA*/& /' | | |
| # | |
| # 1: 金額 2: Aの文字数でインデントレベルを示す 3: 勘定科目 | |
| # 最後2行は"------"と合計金額なんだけど、いらね。 | |
| sed '$d' | sed '$d' | | |
| # | |
| # 1: 金額 2: Aの文字数でインデントレベルを示す 3: 勘定科目 | |
| awk -v previous_level=0 '{ | |
| $2=length($2); | |
| print $2-previous_level, $2,$3,$1; | |
| previous_level=$2; | |
| }' | | |
| # | |
| # 1: 前行とのインデントレベルの差分 2: インデントレベル 3: 勘定科目 4: 金額 | |
| # | |
| awk '{ | |
| for(i=1;i<=levels+$1-1;i++) | |
| printf the_uppers[i] ":"; | |
| print $3, $4 | |
| } | |
| { | |
| the_uppers[$2]=$3 | |
| levels=$2 | |
| }' | |
| # | |
| # 1: 勘定科目ただしコロンで上位の勘定科目の内容も示してる 2: 金額 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment