How to read sequentially from a file in SH (not KSH)

David W. Tamkin dattier at vpnet.chi.il.us
Wed May 1 06:57:32 AEST 1991


mark at spider.co.uk (Mark Valentine) wrote in
<1991Apr29.105329.20148 at spider.co.uk>, quoting two other people:

| afc> while read VAR1 VAR2 < $FILENAME; do
| afc>   echo $VAR1 $VAR2
| afc> done
| afc>
| afc> but I keep reading the first line in the file over and over.  Nothing in
| afc> the manual page seems to be usable in this situation.  How is this done
| afc> under SH?  Is it possible?

| mike> cat $FILENAME | while read VAR1 VAR2
| mike> do echo $VAR1 $VAR2
| mike> done

Mike was onto it, but the extra fork for cat is unnecessary:

while read VAR1 VAR2
do echo $VAR1 $VAR2
done < $FILENAME

The trick is to redirect the entire loop and not just the read command.
Mark Valentine's suggested solution is not needed for this simple case.

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier at vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591



More information about the Comp.unix.shell mailing list