I have a text file and I'm trying to right-align / justify the amounts (123.00 EUR
) to a specified column number (say 53
) by stretching the spaces before them appropriately
# 2018
; A comment
* Transactions
2018-01-01 @Payee | Internet
expenses:communication:internet 123.00 EUR
assets:cash:eur
2018-01-01 @Landlady | Rent
expenses:housing:rent 321.00 EUR
expenses:fees 2.50 EUR ; Bank fee
assets:bank:eur
The output should be
# 2018
; A comment
* Transactions
2018-01-01 @Payee | Internet
expenses:communication:internet 123.00 EUR
assets:cash:eur
2018-01-01 @Landlord | Rent
expenses:housing:rent 321.00 EUR
expenses:fees 2.50 EUR ; Bank fee
assets:bank:eur
It could probably be done with awk
or printf
or something but I can't figure it out.
More about the issue in the narrow sense as applied to hledger at Add option to print with normalized output width · Issue #1045 · simonmichael/hledger
Using GNU awk for the 3rd arg to match:
$ cat tst.awk
match($0,/^([[:space:]]+[^[:space:]]+)[[:space:]]+([0-9.]+ [[:upper:]]+)(.*)/,a) {
$0 = sprintf("%-39s %13s%s", a[1], a[2], a[3])
}
{ print }
$ awk -f tst.awk file
# 2018
; A comment
* Transactions
2018-01-01 @Payee | Internet
expenses:communication:internet 123.00 EUR
assets:cash:eur
2018-01-01 @Landlady | Rent
expenses:housing:rent 321.00 EUR
expenses:fees 2.50 EUR ; Bank fee
assets:bank:eur
I'm using %-39s %13s
instead of %-40s%13s
above to ensure you get a space between the 2 fields even if the first field ended up being longer than 40 characters.