blob: 0b37204a0a70c39380fd52d4e3f979f6e8f712ff (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/bin/bash
tf="TransactionHistory.csv"
# Automatic cleanup
trap 'rm -f "$tmpfile"' EXIT
tmpfile=$(mktemp) || exit 1
trap 'rm -f "$ledgertmp"' EXIT
ledgertmp=$(mktemp) || exit
# Check for presence of transaction file
if [[ -a ~/Downloads/$tf ]]; then tpath=~/Downloads/$tf
elif [[ -a "$(pwd)/$tf" ]]; then tpath="$(pwd)/$tf"
else "Cannot find $tf. Expecting it either in ~/Downloads or in current folder."; exit 1
fi
# remove the BOM (https://stackoverflow.com/questions/45240387/how-can-i-remove-the-bom-from-a-utf-8-file)
sed -i $'1s/^\uFEFF//' $tpath
# read the file and process lines
while read line; do
line=${line//"-"/"£"}
echo -e "$line\n$(cat $tmpfile)" > $tmpfile
done < $tpath
# how to append to a text file in bash
echo -e "Date,Description,Amount\n$(cat $tmpfile)" > $tmpfile
# use ledger to convert to ledger format
$(ledger convert --input-date-format "%d/%m/%Y" $tmpfile > $ledgertmp)
# Remove the erroneous Equity lines
sed -i '/Equity:Unknown/d' $ledgertmp
# dump to sdout for further redirection
cat $ledgertmp
|