Skip to content
Permalink
448df2105c
Go to file
 
 
Cannot retrieve contributors at this time
executable file 56 lines (45 sloc) 1.81 KB
#!/bin/bash
set -e
fix_dates="$(which fix-dates-natwest || ./fix-dates-natwest)"
output=$1
statements="statements" # where statements live in an account dir
tmp="tmp.csv"
# make sure the tmp files exist
touch tmp{,-sorted}.csv
# loop inputs
for file in "${@:2}"; do
# remove header
# replace string dates for numbers
# fix date order (dd/mm/yyyy -> yyyy-mm-dd)
# remove blank lines
# sort and uniq
tail -n +4 "$file" | \
$fix_dates | \
sed -E 's/^(..)\/(..)\/(....)(.*)/\3-\2-\1\4/' | \
sed -E '/^$/d' | \
sort | uniq > "$tmp"
accounts=$(gawk '{sub(/\"\047/, "", $7); sub(/\"\r?$/, "", $7); print $7}' FPAT="([^,]*)|(\"[^\"]+\")" "$tmp" | sort | uniq)
# Extract accounts and loop
for account in $accounts; do
# find account dir with trailing characters
# Note: matches contain the _full_ path including contents of $output
match=$(find "$output" -depth 1 | grep -E "$account.*" | head -n 1)
[ "$match" ] && account_dir="$match" || account_dir="$output$account"
# make sure account statements directory exists
statements_dir="$account_dir/$statements"
mkdir -p "$statements_dir"
# Extract years from transaction in given account and loop
for year in $(grep "$account" "$tmp" | awk -F '-' '{print $1}' | sort | uniq); do
# extract transactions using year and account
egrep -i "$year.*$account" "$tmp" > tmp-sorted.csv
target="$statements_dir/$year.csv"
# make sure target file exists
touch "$target"
# combine existing target file with output to avoid duplicates/overwriting
cat "$target" >> tmp-sorted.csv
sort tmp-sorted.csv | uniq > "$target"
done
done
done
# tidy up
rm tmp{,-sorted}.csv
You can’t perform that action at this time.