LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-17-2006, 06:40 AM   #1
bujecas
Member
 
Registered: Oct 2004
Location: Portugal
Distribution: Debian, Slackware
Posts: 78

Rep: Reputation: 20
for statement in bourne shell script


Hello,

I am parsing a file with entries like "word1 word2 word3". I need to parse each line, so I used a "for" statement in a Bourne shell script. Something like this:

Code:
for line in `cat $file`
do
  echo $line
done
In this simple example I want to print each line "word1 word2 word3" but instead it prints each word in a diferent line. The "for" statement seems not to distinguish between spaces and newlines...

Any idea to resolve this?
Thanks.
 
Old 07-17-2006, 06:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Is this what you want:

Code:
for line in "`cat $file`"
do
  echo "$line"
done
You need to double quote the cat statement and the $line variable.

Hope this helps.
 
Old 07-17-2006, 06:58 AM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by bujecas
Hello,

I am parsing a file with entries like "word1 word2 word3". I need to parse each line, so I used a "for" statement in a Bourne shell script. Something like this:

Code:
for line in `cat $file`
do
  echo $line
done
In this simple example I want to print each line "word1 word2 word3" but instead it prints each word in a diferent line. The "for" statement seems not to distinguish between spaces and newlines...

Any idea to resolve this?
Thanks.
Code:
cat $file | while read line
do
  echo $line
done
or
Code:
while read line
do
  echo $line
done < $file
 
Old 07-17-2006, 07:32 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
More generally, there are three main solutions when you want to read the output of something line by line:
Code:
something | while read line; do process "$line"; done
or
Code:
SaveIFS="$IFS"
IFS=$'\n'
for line in $(something); do process "$line"; done
IFS="$SaveIFS"
or
Code:
while read line; do process "$line"; done < <(something)
Yves.

Last edited by theYinYeti; 07-17-2006 at 07:34 AM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
file permissions in bourne shell script bujecas Linux - General 2 07-12-2006 11:46 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM
Help with basic bourne shell script rnj Programming 6 09-13-2005 08:41 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration