How to read line by line from a file?

How to read line by line from a file?

Post by Kevin » Tue, 28 Jun 1994 22:40:16



I have a data file "spectrum.tmp", which contains 15 lines and
each line is a name of file.
How can I read them line by  line?

The followin is what I'm doing now.  Is there any more elegant way?

count=1
awk 'NR=='"$count"'{print}' spectrum.tmp > numnrg.tmp
count=`expr $count + 1`
read tmpfil < specfil
cp $tmpfil weight
.
.
.
awk 'NR=='"$count"'{print}' spectrum.tmp > parameters.tmp
count=`expr $count + 1`
read tmpfil < parameters.tmp
echo "$tmpfil"
.
.
.

Thank you very much in advance.

Kevin Lu

--

Clinical Physics             FAX   : (416) 926 - 6566
Ontario Cancer Institute  Toronto, ON. Canada  M4X 1K9

 
 
 

How to read line by line from a file?

Post by Bob Li » Wed, 29 Jun 1994 06:46:08



>I have a data file "spectrum.tmp", which contains 15 lines and
>each line is a name of file.
>How can I read them line by  line?

1) Make more use of awk.  Have it process all the lines, generating
the command lines that you want, then pipe those commands into
another shell:

        awk '  
        NR==1   { print "cp", $1, "weight" }
        ...
        NR==7   { print "echo", $1 }
        ...
        NR==15  { print "whatever", $1 }
        ' < spectrum.tmp | /bin/sh

2) In ksh, you could read the file names into an array, and then
operate on the array:

        set -A tmpfile $(<spectrum.tmp)
        cp ${tmpfile[1]} weight
        ...
        echo ${tmpfile[7]}
        ...
----------


 
 
 

How to read line by line from a file?

Post by Fin » Wed, 29 Jun 1994 01:26:04


I'm not sure I understood what you are actually trying to do, but
here's my suggestion:

while read FILENAME
do
        while read LINE
        do
                echo What ever you want to do with this LINE
        done < $FILENAME
done < file_with_filenames

 
 
 

How to read line by line from a file?

Post by Ken V. Broeze » Thu, 30 Jun 1994 01:00:14


Try the following:

#! /bin/sh
# readline      Read one line at a time from a file
#
while read Line
do

    #< do something to $Line >
    #< if you want to exit when you find something -- use 'break' >
    echo $Line

done < $1

Usage: readline file_name

 
 
 

How to read line by line from a file?

Post by Roger Espel Lli » Thu, 30 Jun 1994 08:47:52




Quote:>Try the following:

>#! /bin/sh
># readline      Read one line at a time from a file
>#
>while read Line
>do

>    #< do something to $Line >
>    #< if you want to exit when you find something -- use 'break' >
>    echo $Line

>done < $1

>Usage: readline file_name

Be careful though, the implementation of this is broken on most /bin/sh's.
They will fork to do the redirection in the while, and as a result
variables set in the loop will be lost afterwards. An example (on
SunOS 4) :

zsh% sh
$ onx=1
$ while read line

Quote:> do
>   echo blah $line
>   onx=2
> done < $HOME/.forward


$ echo $onx
1
$

So, the variable's new setting was lost.

--
+----------------------------+---------------------------------------------+
|   Roger ESPEL LLIMA        |                                             |

|   75005 PARIS FRANCE       |                                             |
+----------------------------+---------------------------------------------+
| Ge mig evig insyn in i dr?mmarnas land - F?rst d? blir jag lycklig    |
| (?nglag?rd -  Kung Bore)                                               |
+--------------------------------------------------------------------------+

 
 
 

How to read line by line from a file?

Post by Pat Mart » Sun, 03 Jul 1994 01:28:46


|>
|> I have a data file "spectrum.tmp", which contains 15 lines and
|> each line is a name of file.
|> How can I read them line by  line?
|>
|> The followin is what I'm doing now.  Is there any more elegant way?
|>

I'm not sure I understand your question entirely, but here's how to
read a file containing a list of files to be read.  It also reads
each file to be read as it gets the corresponding filename.

#!/bin/sh

while read FILENAME
do
   while read LINE
   do
      echo "LINE: $LINE"
   done < $FILENAME
done < $1

It gets the master file (containing a list of files to read) from
the command line.

Hope that was what you were asking...

Pat

-----------------------------------------------------------------------
| Patrick Martin    | My opinions | World's Crummiest JAPH:           |

| Computer Engineer --------------| The First Attempt:                |
| Advance Geophysical Corporation | print 'Just another perl hacker`; |
-----------------------------------------------------------------------

 
 
 

How to read line by line from a file?

Post by Brian Hathaw » Fri, 08 Jul 1994 00:25:20



: >
: >I have a data file "spectrum.tmp", which contains 15 lines and
: >each line is a name of file.
: >How can I read them line by  line?

cat spectrum.tmp |while read FILENAME
do
        cat $FILENAME |while read LINE
        do
                echo $LINE
        done
done

Brian Hathaway

 
 
 

How to read line by line from a file?

Post by SIEV.. » Sun, 10 Jul 1994 10:09:20





>: >
>: >I have a data file "spectrum.tmp", which contains 15 lines and
>: >each line is a name of file.
>: >How can I read them line by  line?
>cat spectrum.tmp |while read FILENAME
>do
>    cat $FILENAME |while read LINE
>    do
>            echo $LINE
>    done
>done

#!/bin/sh

cat `cat spectrum.tmp` |
while
        read LINE
do
        # something
done

# another way of going about the same thing (and perhaps better!).
# uses only two "cats" & only requires a one stage loop. this example
# shows use of command substitution. the other example (above mine) uses
# "cat" once for each word in the file spectrum.tmp + 1 initial "cat" to
# get the file names. trouble with my example is that the loop won't know
# which file it is reading lines from (might be needed in user's program).

enjoy!  ...  jerry sievers

 
 
 

How to read line by line from a file?

Post by Peter Fa » Thu, 14 Jul 1994 03:02:59


You could also try:

while read -r LINE
do
    echo $LINE
done < spectrum.tmp

  -- or, to break each line up into tokens --

while read -r T1 T2 T3 T4 REST
    echo "Token #2 is" $T2
done < spectrum.tmp




>: >
>: >I have a data file "spectrum.tmp", which contains 15
>lines and
>: >each line is a name of file.
>: >How can I read them line by  line?

>cat spectrum.tmp |while read FILENAME
>do
>    cat $FILENAME |while read LINE
>    do
>            echo $LINE
>    done
>done

>Brian Hathaway



Toronto Dominion Bank
Phone: (416) 982-2888
Fax:(416) 982-6046
 
 
 

1. read from stdin while reading line by line a file

while read line ; do
    // I need to read input from the user
    read INPUT
done < file.txt

the problem is that it is not going through "read INPUT"

any workaround?

thanks for all your help. I really appreciate it
matias

2. Mapping network drive

3. line by line reading of the lines in list

4. Missing SCSI tape on bootup

5. line by line without using echo "$i" | while read line?

6. Bandwith usage

7. How to read line by line of a file

8. AP X25

9. HELP: How do I read a file line-by-line without getting spaces stripped?

10. read a file line by line

11. Read a file line after line

12. reading files line-be-line?

13. Line by line file read