Path: utzoo!utgpu!attcan!uunet!ncrlnk!ncr-sd!crash!pnet01!marquez From: marquez@pnet01.cts.com (Dan Castro) Newsgroups: comp.unix.questions Subject: Re: Line at a time scripts in csh Message-ID: <3715@crash.cts.com> Date: 15 Jan 89 22:56:31 GMT Sender: news@crash.cts.com Organization: People-Net [pnet01], El Cajon CA Lines: 47 maart@cs.vu.nl (Maarten Litmaath) writes: >posert@bonnie.ics.uci.edu (Bob Posert) writes: >>I have a file with two words on each line, and want to >>process them in a shell script, but can't figure out >>how to get *the entire line* into a variable. >>I tried: >> foreach i (`cat file`) >> #misc processing >> end >>but i was set to each word, not line. > >foreach i ("`cat file`") # create 1 string > ..."$i"... # avoid * and friends getting expanded >end I had been trying to do the same sort of thing and came up with a different method. The script shown above didn't work on my system. (Microport V/AT 286 2.3) The following did work for both the Bourne and c shells... ---------------------cut here-------cut here-------------------- # expander # # expander processes the "datafile" a line at a time using the # "read" function. The resulting output is the original line # followed by the first two (white space separated) fields in # reverse order, on the same line. # expander() { while read line do one=`echo "$line" | awk ' { print $1 } '` two=`echo "$line" | awk ' { print $2 } '` echo "$line: $two $one" done } # main: shell execution starts here cat datafile | expander --------------------cut here---------cut here------------------- UUCP: {nosc ucsd hplabs!hp-sdd}!crash!pnet01!marquez ARPA: crash!pnet01!marquez@nosc.mil INET: marquez@pnet01.cts.com