How read a line of a file from C-shell?

Bob Fisher nts0302 at dsacg3.dsac.dla.mil
Mon Oct 22 22:17:34 AEST 1990


>From article <8900 at ncar.ucar.edu>, by tparker at bierstadt.scd.ucar.edu (Tom Parker):
> 
> I'm having trouble finding a way to read a line of a file in the C-shell.
> 
> For instance, I would like my script to read the first line of a file,
> process it, then read the second line, process it, etc.
> 
> I tried something like      foreach line(`cat file`)
> but this seems to process the entire file, a word at a time.

Before doing the "foreach", set the InterField Separator (variable name IFS)
to  a newline character.  The default for IFS is whitespace that includes
blanks.  The default would give you to get one "word" at a time.

When you do this, "line" may include blanks and can sometimes cause syntax
errors because the blanks can be interpreted as separators between command
line arguments.  Example (Bourne syntax):

	IFS='
'			# newline enclosed between single quotes
	for line in `cat file`
	do
	if test $line = ""; then ....

may cause a syntax error in the test command if $line contains whitespace.
However, the statement

	if test "$line" = ""; then ....

will not since the quotes delimit a single argument to the test command.
-- 
Bob Fisher
US Defense Logistics Agency Systems Automation Center
DSAC-TOL, Box 1605, Columbus, OH 43216-5002     614-238-9071 (AV 850-9071)
bfisher at dsac.dla.mil		osu-cis!dsacg1!bfisher



More information about the Comp.unix.questions mailing list