The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
print line whatever line i want in a file... there any way kittusri9 Shell Programming and Scripting 1 05-15-2008 09:37 AM
How to print 3rd to last line of file? NivekRaz Shell Programming and Scripting 2 09-10-2007 05:04 PM
how to print 35th line from a file ? gridview UNIX for Dummies Questions & Answers 9 05-15-2007 01:31 PM
how to print out line numbers of a text file? forevercalz Shell Programming and Scripting 4 12-12-2005 01:04 AM
Print one line of Existing File danhodges99 UNIX for Dummies Questions & Answers 2 02-25-2003 07:56 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-20-2005
Registered User
 

Join Date: Jul 2004
Posts: 31
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?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-20-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,032
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
Reply With Quote
  #3 (permalink)  
Old 10-20-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
without the UUoC
Code:
while read line; do
  # stuff
done < filename
Cheers
ZB
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0