Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!dsac.dla.mil!dsacg3.dsac.dla.mil!nts0302 From: nts0302@dsacg3.dsac.dla.mil (Bob Fisher) Newsgroups: comp.unix.questions Subject: Re: How read a line of a file from C-shell? Message-ID: <2641@dsacg3.dsac.dla.mil> Date: 22 Oct 90 12:17:34 GMT References: <8900@ncar.ucar.edu> Organization: Defense Logistics Agency Systems Automation Center, Columbus Lines: 35 From article <8900@ncar.ucar.edu>, by tparker@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@dsac.dla.mil osu-cis!dsacg1!bfisher