Sunday, March 12, 2006

Email Receipts to QIF

If you are like me, you love iTunes, and if you are very much like me, you use some sort of personal financial tool, to keep a handle on the ole' finances. I got sick of importing the digital email info into my financial program, so I found a way to import an email receipt into my financial program. I used the awk tool along with the amazingly expressive power of regular expressions to turn an mbox file (The file Thunderbird uses to save your email) into a QIF file that is ready for import. Peep This!
function getField(src, headerRE, valueRE) 
{
 if ( match( src, headerRE valueRE)) {
  headerStart = RSTART
  valueEnd = RSTART + RLENGTH
  match( src, headerRE )
  headerEnd = RSTART + RLENGTH
  valueStart = headerEnd  
  valueLength = valueEnd - headerEnd
  return substr(src, valueStart, valueLength)

 }
 else 
 {
  return " false"
 }
}

BEGIN {
 RS = "From - "
 apple = 0
 movielink = 0
 now = systime()
 getline
 start = match(FILENAME, "\\\\[^\\\\/]*$")
 output = "EmailReceipts" substr(FILENAME, RSTART + 1, RLENGTH) ".qif"
 print("using " FILENAME " as input.")
 print("outputting to " output)
 print("!Type:CCard") > output
}

/Apple Receipt/ { 
 print "D" getField($0, "Receipt Date: ", "[\\/$.0-9A-Za-z]*") > output
 print "T-" substr(getField($0, "Order Total: ", "[\\/$.0-9A-Za-z]*"), 2, 100) > output
 print "PiTunes" > output
 print "LLeisure:Tapes & CDs" > output
 print "MOrder #: " getField($0, "Order Number: ", "[\\/0-9A-Za-z]*") > output
 print "^" > output
 apple++ 
}
/Subject: Movielink Order Confirmation/ { 
 print "D" getField($0, "Date : ", "[\\/$.0-9A-Za-z]*") > output
 print "T" substr(getField($0, "Transaction Total:[ \t]*", "[\\/$.0-9A-Za-z]*"), 2, 100) > output
 print "PMovieLink" > output
 print "LLeisure:Movies & Video Rentals" > output
 print "M" getField($0, "Movie Title Player Format Store Until Price\n", "[\\\\\\/0-9A-Za-z$.() ]*") > output
 print "^" > output
 movielink++ 
  }

 END {
 print apple " apple reciepts."
 print movielink " movielink reciepts."
 
} 

0 Comments:

Post a Comment

<< Home