Page 1 of 1

Script to sum expenses

PostPosted: Thu Aug 08, 2013 5:12 pm
by r.f.irish
Thanks in advance for any advice you have to share.

I have been trying to write a script into Hazel to sum the numbers in a text file. I don't have much experience and have tried a lot of different solutions in both bash and Applescript without any results.

Here is an example text file from which I would like to add the numbers preceded by ": $"

**15.44.04**
x
Art materials
: $9

Transport
: $4.50

**15.44.21**

Art materials
: $18

Art materials
: $25

Food
: $11.50

**20.03.53**

Miscellaneous
: $20

**20.04.02**

Food
: $10

I woiuld like to append the text "**Total: " with the sum of the values to the end of the file.

Is something like this possible using bash or Applescript?

Thanks again,

Robert

Re: Script to sum expenses

PostPosted: Fri Aug 09, 2013 5:15 pm
by a_freyer
Here's a quick and dirty shell scrip that should work:

Code: Select all
echo -e "\n**Total: $(perl -e "print $(cat "$1" | grep '\$' | sed 's/\$//g' | tr '\n' '+'  | sed 's/[A-Zaz:]//g;s/+$//g;s/+/ + /g')")" >>$1