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

5

6

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]
Fustic answered 25/10, 2010 at 9:14 Comment(5)
CSH PROGRAMMING CONSIDERED HARMFUL might be relevant to what you're doing if you have a choice with your shell. It's a bit of a rant but worth reading nonetheless.Cybil
@Noufal is this really needed in EVERY csh question?Punctuality
I don't deliberately hunt out csh questions to post that link but whenever I See it mentioned (I came across this question due to the unix tag), I bring it up since it is quite crippling. I've worked with it for 2 years or so and really wished that people had tossed it away before they started.Cybil
@Noufal, It's not really my choice whether to work with it or with another shell. Your comment is not helpful or constructing in any way, actually it's the exact opposite, alienating and annoying. Unless people ask whether or not they should use csh, I would refrain from that comment.Fustic
If you don't have a choice with the shell, I agree it's not helpful. I however didn't know that an if you did have a choice, it's useful to have some information which you can use to make a decision. That was my intent (and it's why I put it as a comment rather than an answer).Cybil
F
8

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\$
Fustic answered 27/10, 2010 at 13:37 Comment(0)
H
0

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'
Husky answered 20/4, 2021 at 4:0 Comment(1)
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.Husky
H
0
foreach line (`awk {print $0} test_file`)
  echo $line
end
Husky answered 20/4, 2021 at 5:52 Comment(0)
H
0

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
Husky answered 21/4, 2021 at 6:54 Comment(0)
G
-1

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
Gershom answered 27/10, 2010 at 13:48 Comment(4)
He specifically asked how to do this in cshell.Punctuality
he needed CSH solution, not AWK, though i must agree :-) using the AWK\SED tools is usually much better then playing with the shell.Slot
Indeed, but that's the wrong way to do it, unless there's some Unix I'm unaware of without awk :-)Gershom
So what's the UNIXish way of invoking a program on every line of a file using awk, then?Exploiter

© 2022 - 2024 — McMap. All rights reserved.