LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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,012

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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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,697
Blog Entries: 5

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



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 7 12-19-2015 01:03 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 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