LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-09-2009, 08:50 AM   #1
ralste
LQ Newbie
 
Registered: Jun 2007
Posts: 1

Rep: Reputation: 0
Elements in a "for loop" using bash not being separated by white spaces.


Hai da, together!!
Consider a file and a "for loop" over its elements.
$ cat file1
one
two
three four
five
$ for x in `cat file1`; do echo $x; done
one
two
three
four
five
How to get the "for loop" done to get the elements by line, means...
$ for x in `???`; do echo $x; done
one
two
three four
five
Thousands of thanks for any help...
 
Old 11-09-2009, 08:59 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,148

Rep: Reputation: 236Reputation: 236Reputation: 236
You can use while instead of for:

cat file1 | while read x ; do echo $x ; done
 
Old 11-09-2009, 01:17 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
This is better and eliminates that UUOC (useless use of cat). Also allows variables set inside the loop to stay that way outside the loop which is what is usually expected:
Code:
while read -r LINE ; do
   :
done < "$FILE"
 
Old 11-09-2009, 11:26 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Set the IFS var to newline only http://tldp.org/LDP/abs/html/internalvariables.html, then you can use a for loop.
 
Old 11-09-2009, 11:41 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by ralste View Post
Hai da, together!!
Consider a file and a "for loop" over its elements.
$ cat file1
one
two
three four
five
$ for x in `cat file1`; do echo $x; done
one
two
three
four
five
How to get the "for loop" done to get the elements by line, means...
$ for x in `???`; do echo $x; done
one
two
three four
five
Thousands of thanks for any help...
learn how to use awk to parse files. see my sig for gawk manual.
 
Old 11-10-2009, 01:55 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by chrism01 View Post
Set the IFS var to newline only http://tldp.org/LDP/abs/html/internalvariables.html, then you can use a for loop.
What are the advantages of that technique over tuxdev's suggestion in this case?
 
Old 11-10-2009, 02:26 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by catkin View Post
What are the advantages of that technique over tuxdev's suggestion in this case?
no advantage. One must take care to set IFS back to normal else it may cause problems elsewhere in the script. Using while read is still better in that respect.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 6 03-21-2013 10:35 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
"Hiding a process" - yes, a "white hat" angle sundialsvcs Programming 2 12-20-2007 01:32 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
bash script: how do I refer to a counter i in a "for i in" loop frankie_DJ Programming 5 07-24-2007 08:45 AM


All times are GMT -5. The time now is 08:09 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 Google+: linuxquestions
Open Source Consulting | Domain Registration