开发者

How can I read one line at a time with C shell in unix

开发者 https://www.devze.com 2023-01-22 00:58 出处:网络
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 in开发者_JAVA百

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 in开发者_JAVA百科to 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]


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\$


variable file is a space-delineated array of the lines in source file test.txt. It is a useful to extract a line at a time.

set text = 'awk -v ln=$j '{if (NR==ln) print $0}' test.txt'


in cshell correct method 1

foreach line (`awk '{print}' test_file`)
  echo $line
end

in cshell correct method 2

set n = `wc -l a.txt`
set i = 1
while($i <= $n)
    set line = "`awk '{if (NR == $i) print}' a.txt`"
    echo ${line}
    @i++
end


foreach line (`awk {print $0} test_file`)
  echo $line
end


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
0

精彩评论

暂无评论...
验证码 换一张
取 消