Computing.Net > Forums > Unix > Reading a file line by line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Reading a file line by line

Reply to Message Icon

Name: Mona
Date: September 30, 2003 at 13:13:52 Pacific
OS: Sun OS
CPU/Ram: 240
Comment:

Hi,

I wrote the following code to read a file line by line
#!/bin/ksh
for i in `cat a.txt`
do
echo $i
done

This is not showing me the file contents line bye line. Instead I am seeing a word by word output. What is wrong ? DO I need to format the text file in some way that it reads the end of line char ?

Please advise.



Sponsored Link
Ads by Google

Response Number 1
Name: covina
Date: September 30, 2003 at 13:41:08 Pacific
Reply:

n=`wc -l < data.txt`
i=1
while [ "$i" -le "$n" ]
do
line=`cat data.txt | head -$i | tail -1`
echo $line
i=`expr $i + 1`

done

Hope this helps.


0

Response Number 2
Name: Nails
Date: September 30, 2003 at 20:40:15 Pacific
Reply:

Hi:

Try this:

#!/bin/ksh

lineno=0
while txtline=$(line); do
lineno=$((lineno+1))
printf "%s\n" "$txtline"

# printf "%8d %s\n" "$lineno" "$txtline"
done < a.txt

Regards,

Nails



0

Response Number 3
Name: WilliamRobertson
Date: October 1, 2003 at 00:49:49 Pacific
Reply:

Or this:

while read linevar
do
print $linevar
done < a.txt


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Reading a file line by line

read a file line by line in unix www.computing.net/answers/unix/read-a-file-line-by-line-in-unix/7334.html

print a file line-by-line www.computing.net/answers/unix/print-a-file-linebyline/4099.html

Shell script to read a file and pro www.computing.net/answers/unix/shell-script-to-read-a-file-and-pro/4304.html