Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Reading line by line



Hi,

Actually there are many ways to read the file line by
line. Three of the popluar ones are:

1.

for i in `cat filename`
do
echo $i
done

* This method works only if the file has only single
word in every line. Else it fails

2. Like Micah J. Cowan pointed out, 

while read i
do
echo $i
done < filename

3. The below example is taken from
http://open.itworld.com/5040/nls_unix_filedescriptor060608/page_1.html


$ read <&3 myline

The variable "$myline" will then contain the first
line from the file. Repeat the read command and we
will get the second line and so on. We might,
therefore, use a script like that shown below to read
and process each line in a file. Notice the comments
inserted into the spot where we would process each
line in some way in whatever way we want. 

if [ $# != 1 ]; then
    echo "Usage: $0 input-file"
    exit 1
else
    infile=$1
fi

# assign file descriptor 3 to input file
exec 3< $infile

until [ $done ]
do
    read <&3 myline
    if [ $? != 0 ]; then
        done=1
        continue
    fi
    # process file data line-by-line
    # in here ...
    echo $myline
done

echo "The End"


Regards,
sac.

--- meino.cramer@xxxxxx wrote:

> Hi,
> 
>  how can I read line by line from a file of text?
> 
>  I tried
> 
>  while theline in `cat results.txt`
>  do
>      echo ${theline}
>      echo "----------------"
>  done
> 
>  and got a new echo on every _blank_ seperated word
> and 
>  not a every new line seperated by a newline.
> 
>  Thank you very much for any help in advance!
>  Keep zshing!
>  mcc
> 
> 
> 
> -- 
> Please don't send me any Word- or
> Powerpoint-Attachments
> unless it's absolutely neccessary. - Send simply
> Text.
> See
>
http://www.gnu.org/philosophy/no-word-attachments.html
> In a world without fences and walls nobody needs
> gates and windows.
> 



       
____________________________________________________________________________________
Food fight? Enjoy some healthy debate 
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367



Messages sorted by: Reverse Date, Date, Thread, Author