Print file line by line

I have a file where each line is a stream of text as follows,

table1, select * from table1
table2, select * from table2

How do i loop through the file line by line? I have tried doing the following

for line in `cat file.txt`
do
echo $line
done

and ...

cat file.txt|while read line
do
echo "$line"
done

but the output produced by the echo is

table1,
select
*
from table1
...etc etc...

Any help?

hm, the "for"-loop will not be working for the following reason: "for" will split up its argument at word boundaries and cycle through them:

a="1 2 3 4 5"
for value in $a ; do
print - $value
done

will yield:

1
2
3
4
5

The while-loop in conjunction with "read" is the correct way to do it:

cat file | while read line ; do
print - "$line"
done

should print out "file" line by line.

If not, try the following: load your script into vi and in command mode issue ":set list" to display all the nonprintable characters. Sometimes the shell reacts a bit picky when facing these. If this doesn't correct your problem do the same with your input file, maybe there are nonprintig characters hidden in it which prevent print/echo from working correctly.

bakunin

without the UUoC

while read line; do
  # stuff
done < filename

Cheers
ZB