arithmetic in tcsh

Yes I know tcsh sucks for scripting and arithmetic but I have to write a script for multiple users and they all use tcsh.

I have this variable that I 'set' with but pulling numbers off of stings with

set STUFF = `grep string file | awk command`

Now I would like to add up the numbers that were pulled out. I tried (maybe rookie mistake)

@ SUM = ( `echo STUFF | awk '{print $1}'` + `echo STUFF | awk '{print $2}'` )

this returns: @: Expression Syntax. error

as a quick fix I use the the echo | awk commands to make several variables then add them up but this is ugly and not so clever.

conveniently the number of integers pulled from the file is known. Is there a nice way to do this for a known number of int? How about an unknown number?

Thanks!
gobi

Since you give no clue as to what your data looks like or what awk command you are using, a specific answer is not possible. But make your awk command smarter. awk can add numbers, so do all the arithemetic there.

I did not know awk could add. Sorry about that.

The file which name is stored in $LOG_FILE contains

bc misstep (hi)............34
bc misstep (hello)..2
bc misstep (bye)......0

This information is repeated many times but I only want to use the last set. I would like to add up these three numbers. The following gives me the three numbers. How can I add them up?

set LOG_BC = `tail -n 17 $LOG_FILE | grep bc | awk 'BEGIN{FS="."} {print $NF}'`

Like this...but first an easier way to do your awk script....

$ cat data
bc misstep (hi)............34
bc misstep (hello)..2
bc misstep (bye)......0
$ awk -F. '{print $NF}' data
34
2
0
$
$
$
$ awk -F. '{sum=sum+$NF}END{print sum}' data
36
$

Thanks! thats nice. I must take some time and learn the ways of awk