Reading file line by line in cshell

I want to read a file in cshell line by line. But it is reading the values by spaces....

For Ex, my file1 :

word1 word2 word3
word5 word6

By below script, variable taking the values separated by space

foreach v ( `cat file`)
echo $v
end

the output will be like,

word1 
word2 
word3
word5 
word6

I want these values to be read line by line instead of space by space..

Could you suggest on this pls?

Thanks again.

You want something like the equivalent of sh, ksh or bash:-

while read line
do
  ...whatever
done < file

Why write in csh? It has all sorts of issues, limitations and confusions - oh and it lets you use goto which creates a nightmare when you try to dismantle the logic later on.

Robin

You somewhat "jump through hoops" with csh. The following should work (tested in tcsh).

set v=`cat file`
set i=1
while ( $i < = $#v )
    echo $v[$i]
    @ i = $i + 1
end
1 Like