arithmetic in tcsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arithmetic in tcsh
# 1  
Old 05-27-2008
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
# 2  
Old 05-28-2008
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.
# 3  
Old 05-28-2008
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}'`
# 4  
Old 05-28-2008
Like this...but first an easier way to do your awk script....

Code:
$ 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
$

# 5  
Old 05-28-2008
Thanks! thats nice. I must take some time and learn the ways of awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with date arithmetic please

Hello fellow forum members, I wrote below piece of code to calculate the date after a given date - date=$DATE_FINAL declare -a max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31) eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100... (9 Replies)
Discussion started by: ektubbe
9 Replies

2. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

3. Shell Programming and Scripting

csh arithmetic ?

Hello, Could someone explain how this one is possible: # @ x = 10 - 11 + 3 # echo $x -4 I know that writing script using csh is bad idea, but I need to write few lines. thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

4. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

5. UNIX for Dummies Questions & Answers

String Arithmetic ?

Hello Experts, In my shell I need to perform some simple subtraction on a value returned as a result of the "wc" command. The code: scanFromLine="100" ## This is returned as string as a result of some operation totalLines=`wc -l "${latestLogFile}" | awk '{print $1}'` ## eg: 200 ... (2 Replies)
Discussion started by: hkansal
2 Replies

6. UNIX for Dummies Questions & Answers

Arithmetic Operators

Hello, I have a list of 'inputs' and i want to convert those on the second list named 'Desired Outputs', but i don't know how to do it? Inputs Desired Outputs 1 2 94 4 276 8 369 10 464 12 ... (0 Replies)
Discussion started by: filda
0 Replies

7. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

8. Shell Programming and Scripting

Can I use wc -l with arithmetic expression?

Folks, I am wondering that i can use something like this in one line. For example, $((cat filename > wc -l) / 2) It doesn't work; how to get it work using command substitution? Moreover, is there any option for wc -l not to return filename after the line counts? wc -l filename would... (3 Replies)
Discussion started by: lalelle
3 Replies

9. Shell Programming and Scripting

arithmetic in ksh

Helloo.. I am trying one very simple thing I could not find anything on google.. I have 2 integer variable..and I need to do division...in ksh where $catch and $num are integer variable.. I tryed with this: printf "%0.2f" $final=$catch/$num but it does not work.. any help is... (12 Replies)
Discussion started by: amon
12 Replies

10. UNIX for Dummies Questions & Answers

Arithmetic In UNIX

I am a beginner and I have been searching the web all morning for this answer but can't find it anywhere. I know that prtint 1*2 will return 1*2 what if i actually want the problem to be calculated so i want print 1*2 to return 2 How is this done? (3 Replies)
Discussion started by: tygun
3 Replies
Login or Register to Ask a Question