Script to sum expenses

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Script to sum expenses 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
r.f.irish
 
Posts: 2
Joined: Thu Aug 08, 2013 9:35 am

Re: Script to sum expenses 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
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Support