Line at a time scripts in csh

Dan Castro marquez at pnet01.cts.com
Mon Jan 16 08:56:31 AEST 1989


maart at cs.vu.nl (Maarten Litmaath) writes:
>posert at 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 at nosc.mil
INET: marquez at pnet01.cts.com



More information about the Comp.unix.questions mailing list