Page 1 of 1

Shell script not working -> change contents of OFX file

PostPosted: Sat Jan 23, 2016 9:21 pm
by rpedro
I'm trying to change the contents of a OFX file (bank statement) using one of the following shell scripts (both achieve the same result):

Option 1:
Code: Select all
sed -i.bak '/<TRNAMT">/s//&-/; s/--/-/' $1


Option 2:
Code: Select all
perl -pe 's/^(<TRNAMT>)(-?\d+\.\d+)$/sprintf "%s%.2f", $1, -$2/e' $1


I ran both on Terminal replacing $1 with the file name and they work, so I'm doing something wrong in Hazel.
I really have no experience at all with Unix commands, I got help on a Unix forum to get those scripts.

Do I have to add a path? Or something before or after the commands?

The Hazel rule is:
IF: file extension is ofx
DO THE FOLLOWING: Run Shell Script

In case it helps, the scripts reverse the sign of numbers in specific lines, so that a positive number becomes negative and vice versa. The lines always starts with <TRNAMT> followed by the amount, example below, in red:

<STMTTRN>
<TRNTYPE>DEBIT
<DTPOSTED>20151127000001[-3:GMT]
<TRNAMT>-8.77
<FITID>667800007
<CHECKNUM>667800007
<MEMO>PAYPAL
</STMTTRN>

Re: Shell script not working -> change contents of OFX file

PostPosted: Mon Jan 25, 2016 11:31 am
by Mr_Noodle
You should enclose the $1 in double-quotes, in case the path has characters like spaces in them. See if that fixes things.

Re: Shell script not working -> change contents of OFX file

PostPosted: Tue Jan 26, 2016 2:59 pm
by rpedro
Yes Mr Noodle! Quotes did the trick, that's awesome!
I'll test with some more files but I think its working OK.

Thanks a lot, so simple.