On Mon, Apr 06, 1998 at 06:28:23PM -0400, kgibson wrote:
> > 
> > for line in `cat filename`
> > do
> >     do_whatever_with $line
> > done
> > 
> > 
> > And just to test $line
> > 
> > for line in `cat filename`
> > do
> >     echo $line
> > done
> > 
You could run into trouble with either of these.  When the shell evaluates
the line the `cat filename` will be expanded into the contents of the file.
the value assigned to the variable "line" will be a word from the values in
the expansion of `cat filename`.  The is a limit to the maximum command line
length (I don't know the Linux limit but on HPUX its about 2KB).  Try the 
following.  I use it on files with hundreds of lines.

while
        read  line
do
        do_whatever_with $line
done < filename

Depending on the structure of the data in the file several names
could be put in the read statement, e.g.
        read id name street city state
also IFS (internal field separator) could be set if the delimiter is
not white space, e.g.
        IFS=:
The default values of IFS are space, tab, and newline.
It may be helpful to save the old value of IFS before changing it so 
you can restore it easily.
This works good if read /etc/passwd or /etc/group

If the data is in fixed locations on the line I usually use the cut
command to pick out the pieces I want, e.g.
        name=`echo $line | cut -c5-10`
-- 
paul

--
           /-------------------\\   //---------------------------/
          / C h i p p e w a     \\ // alley  Technical  College /
         / 620 W. Clairemont Ave \//  Eau Claire, WI 54701 USA /
        /-----------------------------------------------------/
       /  Paul F. Almquist, Instructor - DBMS,OS,Networking  /
      /  Computer Information Systems Dept   HP-UX/Linux!!  /
     / [EMAIL PROTECTED]  Voice:715-833-6386 /
    /      http://cis.chippewa.tec.wi.us/~almquipf        /
   /-----------------------------------------------------/


-- 
  PLEASE read the Red Hat FAQ, Tips, Errata and the MAILING LIST ARCHIVES!
http://www.redhat.com/RedHat-FAQ /RedHat-Errata /RedHat-Tips /mailing-lists
         To unsubscribe: mail [EMAIL PROTECTED] with 
                       "unsubscribe" as the Subject.

Reply via email to