LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack 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: 15
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
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139Reputation: 1139
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: 46
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: Nantes (France)
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 07:29 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration