I do my banking online, and my bank provides QIF files (Quicken files) for download. The QIF files are plain text files with a simple format. Here's a sample:
D08/10/2006
T-5.81
C*
N
PPurchase
M# XXXXXXXXX GIANT FOOD INC #XXX CITY,STATE 08/09/06
^
Each entry ends with a "^". This particular transaction was a withdrawal of $5.81. Now software such as gnucash and grisby can read qif files, but they do not quite work for me. The balance they come up with does not match the balance reported by my bank. Needless to say, when it comes to balancing my checkbook, the last thing I want to deal with is buggy software.
So I decided to write a shell script called qifadder to read a QIF file and add all of the entries to an opening balance. The script expects a file OpenningBalance to exist in the same directory. This file should simply contain a number giving the opening balance of the account.
I run the script like this (the purpose of using tac, cat in reverse, is to have the script use the same transaction order as it appears on my bank's website):
$ ./qifadder fib_01-01-06_to_8-19-06.qif > summary.out; tac summary.out > summary.tac
Here is a listing of qifadder (Updated to remove duplicates):
#!/bin/sh
# Usage: qifadder file.qif
# Simple script to add up transactions in a qif file.
tac $1 > $1.tac
read openbal < OpenningBalance
bal=$(echo "scale=2; $openbal" | bc)
count=0
while read line
do
firstChar=${line:0:1}
case "$firstChar" in
^ ) lineCarrot["$count"]="$line";;
M ) lineM[count]="$line";;
P ) lineP[count]="$line";;
N ) lineN[count]="$line";;
C ) lineC[count]="$line";;
T ) lineT[count]="$line";;
D ) lineD[count]="$line"
# Now all lines have been read in for the transaction.
# Search through previous transactions to check for duplicates.
# If the date has changed, reset count to 0 so that the
# search list does not keep building up.
prevcount=0
foundDup=0
while [ "$prevcount" -lt "$count" ]
do
if [ "${lineD[count]}" = "${lineD[0]}" ]
then
if [ "${lineT[count]}" = "${lineT[prevcount]}" ] &&
[ "${lineC[count]}" = "${lineC[prevcount]}" ] &&
[ "${lineN[count]}" = "${lineN[prevcount]}" ] &&
[ "${lineP[count]}" = "${lineP[prevcount]}" ] &&
[ "${lineM[count]}" = "${lineM[prevcount]}" ] &&
[ "${lineCarrot[count]}" = "${lineCarrot[prevcount]}" ]
then # found duplicate
echo "found duplicate"
foundDup=1
fi
else # restart the list to search for duplicates
lineD[0]="${lineD[count]}"
lineT[0]="${lineT[count]}"
lineC[0]="${lineC[count]}"
lineN[0]="${lineN[count]}"
lineP[0]="${lineP[count]}"
lineM[0]="${lineM[count]}"
lineCarrot[0]="${lineCarrot[count]}"
count=0
fi
let "prevcount = $prevcount + 1"
done
if [ "$foundDup" = 0 ]
then
# line*[count] is not a duplicate, process the line
tmp="${lineT[count]}"
trans="${tmp:1}" # keep the number after "T"
trans=`echo $trans | sed -e 's/,//'` # get rid of commas
bal=$(echo "scale=2; $bal+$trans" | bc)
echo "change: $trans balance: $bal"
fi
let "count = $count + 1"
;;
esac
done <"$1.tac"
echo ""
echo "balance is $bal"
Bookmark/Search this post with:
Posting a shell script???
How are you supposed to do this? It took me an hour to get this posted in any form whatsoever. If I enclose the whole thing in a code block, or use no formatting at all, only the first few lines post. I had to break it up into several code blocks. It still removed all of the indentation.
Help!!!
Re: Posting a shell script???
Ok, I figured out a slightly more optimal solution. You have to replace all of the greaterthan and lessthan signs, as mentioned in the html composition tips on this site. Then you have to make sure that there are no line breaks, and enclose the whole code block with <code> and </code>. The bbcode
[code]and[/code] preserves spaces and identation, but it also shows a bunch of html markups.Another option is to use the debian package code2html. Is there a way to post standalone html and have it appear like it would in its own page (i.e. $ firefox standalone.html)?
does is add up?
When you add it up yourself, does it balance? If so, nag the GnuCash people. (Well, really only 1 person the last time I looked in 2002...)
Yes, it adds up.
It adds up, but I have to look for duplicate entries of transactions. Apparently my bank's qif files contain duplicate entries, which the bank sorts out.
If I delete the duplicate transactions, qifadder works just fine. I'll have to try GnuCash again on the qif file without duplicates, but I think GnuCash still might have another issue (I remember seeing a message about removing duplicates when GnuCash was importing the file).