tags:

views:

73

answers:

3

I try to make a small script, using c shell, that will take a file made of several lines, each containing a name and a number and sum all numbers that a have certain name. How can I put into a variable the next line each time?

the summig part I do by: (after I'll be able to get a full line to $line)

set line =($line)
@ sum = $sum + $line[2]
+1  A: 

I found this discussion that might answer your question: http://www.linuxquestions.org/questions/programming-9/csh-while-read-738708/

David Mårtensson
+3  A: 

I have managed to solve it using the next piece of code:

foreach line ("`grep $1 bank`")
    echo $line
    set line_break = ($line)
   @ sum = $sum +$line_break[2]
end
echo $1\'s balance id: $sum\$
Ilya Melamed
A: 

Awk can be called from any shell:

% cat >test.dat
a 1
a 3
b 2
a 7
b 4
% awk '($1 == "a") { SUM += $2 } END { print SUM }' < test.dat
11
Gaius
He specifically asked how to do this in cshell.
Amir Rachum
he needed CSH solution, not AWK, though i must agree :-) using the AWK\SED tools is usually much better then playing with the shell.
codeScriber
Indeed, but that's the wrong way to do it, unless there's some Unix I'm unaware of without awk :-)
Gaius