From dae2880c061d30e4f05759e2e1b2b2d4bab44c19 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 13 Feb 2026 11:32:37 -0800 Subject: [PATCH] container: hledger-flow: paypal: add uniform quotes - Allows undesirable characters in variable-text columns - Related refactoring --- .../accounts/paypal/paypal-shared.bash | 67 ++++++++++++------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/container/src/hledger-flow/accounts/paypal/paypal-shared.bash b/container/src/hledger-flow/accounts/paypal/paypal-shared.bash index 03a67d5..8d2dd14 100755 --- a/container/src/hledger-flow/accounts/paypal/paypal-shared.bash +++ b/container/src/hledger-flow/accounts/paypal/paypal-shared.bash @@ -2,7 +2,7 @@ # docker-finance | modern accounting for the power-user # -# Copyright (C) 2021-2025 Aaron Fiore (Founder, Evergreen Crypto LLC) +# Copyright (C) 2021-2026 Aaron Fiore (Founder, Evergreen Crypto LLC) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -47,10 +47,10 @@ function parse_report() # Paypal is known to allow commas within description ("Name") and amounts # NOTE: using custom timezone offset instead of provided timezone - # TODO: refactor xan/sed -> gawk (the sed line should remove non-csv commas and quotations) xan select "Date,Time,Name,Type,Status,Currency,Amount,Fees,Total,Exchange Rate,Receipt ID,Balance,Transaction ID,Item Title" "$global_in_path" \ - | sed -e 's:, : :g' -e 's:,",:,:g' -e 's:,"\([0-9]*\),:,\1:g' -e 's:,"-\([0-9]*\),:,-\1:g' -e 's:"::g' \ - | gawk -v global_year="$global_year" -v global_subaccount="$global_subaccount" \ + | gawk --csv \ + -v global_year="$global_year" \ + -v global_subaccount="$global_subaccount" \ '{ if (NR<2 || $1 !~ global_year) next @@ -71,32 +71,44 @@ function parse_report() # Print printf timestamp OFS # timestamp + # description (Name) sub(/%/, "%%", $3) - printf $3 OFS # description (Name) + gsub(/"/, "", $3) + printf "\"" $3 "\"" OFS - printf $4 OFS # type - printf $5 OFS # status - printf $6 OFS # currency + printf "\"" $4 "\"" OFS # type + printf "\"" $5 "\"" OFS # status + printf "\"" $6 "\"" OFS # currency + # amount direction=($7 ~ /^-/ ? "OUT" : "IN") sub(/^-/, "", $7) - printf $7 OFS # amount + gsub(/,/, "", $7) + printf "\"" $7 "\"" OFS + # fees sub(/^-/, "", $8) - printf $8 OFS # fees + gsub(/,/, "", $8) + printf "\"" $8 "\"" OFS + # total sub(/^-/, "", $9) - printf $9 OFS # total + gsub(/,/, "", $9) + printf "\"" $9 "\"" OFS - printf $10 OFS # exchange rate + # exchange rate + gsub(/,/, "", $10) + printf "\"" $10 "\"" OFS - printf $11 OFS # code (Receipt ID) - printf $12 OFS # balance + printf "\"" $11 "\"" OFS # code (Receipt ID) + printf "\"" $12 "\"" OFS # balance - printf $13 OFS # txid (Transaction ID) + printf $13 OFS # txid (Transaction ID) + # title (Item Title) sub(/%/, "%%", $14) - printf $14 OFS # title (Item Title) + gsub(/"/, "", $14) + printf "\"" $14 "\"" OFS printf direction OFS printf global_subaccount OFS @@ -110,7 +122,7 @@ function parse_report() # fiat_value printf "\n" - }' FS=, OFS=, >"$global_out_path" + }' OFS=, >"$global_out_path" } # NOTE: crypto transactions CSV, not crypto statement CSV @@ -122,7 +134,9 @@ function parse_crypto() # TODO: refactor into gawk after assert_header is fixed xan select "DateTime,Transaction Type,Asset In (Quantity),Asset In (Currency),Asset Out (Quantity),Asset Out (Currency),Transaction Fee (Quantity),Transaction Fee (Currency),Market Value (USD)" <(tail +4 "$global_in_path") \ - | gawk -v global_year="$global_year" -v global_subaccount="$global_subaccount" \ + | gawk --csv \ + -v global_year="$global_year" \ + -v global_subaccount="$global_subaccount" \ '{ if (NR<2 || $1 !~ global_year) next @@ -150,16 +164,19 @@ function parse_crypto() printf OFS # direction printf global_subaccount OFS - printf $3 OFS # in_value - printf $4 OFS # in_ticker - printf $5 OFS # out_value - printf $6 OFS # out_ticker - printf $7 OFS # tx_fee_value - printf $8 OFS # tx_fee_ticker + printf $3 OFS # in_value + printf "\"" $4 "\"" OFS # in_ticker + + printf $5 OFS # out_value + printf "\"" $6 "\"" OFS # out_ticker + + printf $7 OFS # tx_fee_value + printf "\"" $8 "\"" OFS # tx_fee_ticker + printf $9 # fiat_value printf "\n" - }' FS=, OFS=, >"$global_out_path" + }' OFS=, >"$global_out_path" } function main()