- I could write another ant task to do the summing (this required more work that I was willing to invest, as ant isn't really suited for computational tasks)
- I could write a python script that took the contents of the specified file and returned the sum. I didn't really like this approach because it involved yet another file in my build process, invoked from the
ant task. I always sort of thought that if you were forced to use , you were performing a task beyond the scope of your tool. - I could skip the generation of the table and go straight to the sum. A few of the tables were created with XSLT, so this was a valid option. However, my XSLT programming ability is very much trial-and-error based, so I thought this might take some time. Also, some of the other tables, created with grep would not be affected.
- Write it in shell. I liked this idea. I really liked the feel of being able to just pipe something to 'sum' and have the sum returned. So this is what I chose.
#!/bin/bashIt did the trick quite well. I had suggestions from office mates for the following alternatives:
sum=0
while read line
do
sum=$(($sum + $line))
done
echo $sum
exit 0
Using python (courtesy Aran Donohue):
python -c "import sys;print sum(float(x) for x in sys.stdin.read().split())"
cat numbers | tr '\n' '+'|head --bytes='-1'|bc
(Note that this version doesn't quite work. bc throws a syntax error. not exactly sure why.)
Using my original design, I realized that if I abstracted out the operator, I could use this script to perform any 2-operand function I wished on the list, essentially creating a basic left fold:
#!/bin/bashI've done a little bit of error checking, to see if the parameter supplied is blank, and if so replace it with + by default.
op=""
if [ "$1" == "" ]; then
op="+"
else
op=$1
fi
sum=0
while read line
do
sum=$(($sum$op$line))
done
echo $sum
exit 0
I've only seen this work for '+' and '-'. If I use '*', it breaks because it replaces the wildcard/multiplication character with the listing of the current directory.
2 comments:
Two (less-than-awesome) solutions for the tr/bc solution:
echo `tr '\n' '+' | head --bytes='-1'` | bc
(tr '\n' '+' | head --bytes='-1' && echo) | bc
... just need to get that newline in at the end...
It's almost like someone should write a book on working with data ... :)
Post a Comment