The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 12:37 PM
How to print 3rd to last line of file? NivekRaz Shell Programming and Scripting 2 09-10-2007 08:04 PM
how to print 35th line from a file ? gridview UNIX for Dummies Questions & Answers 9 05-15-2007 04:31 PM
how to print out line numbers of a text file? forevercalz Shell Programming and Scripting 4 12-12-2005 05:04 AM
Print one line of Existing File danhodges99 UNIX for Dummies Questions & Answers 2 02-25-2003 11:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-20-2005
handak9 handak9 is offline
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

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

Code:
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?

Last edited by Yogesh Sawant; 04-10-2009 at 06:19 AM.. Reason: added code tags
  #2 (permalink)  
Old 10-20-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #3 (permalink)  
Old 10-20-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0