LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2010, 12:11 AM   #1
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Rep: Reputation: 36
Query regarding for loop.


Filename.txt
Code:
My First Line
My Second Line
::::While Loop:::
Program:
Code:
while read line
do
echo "$line"
done < Filename.txt
output:
Code:
My First Line
My Second Line
Is it possible to use for loop to get the same output.
I have tried executing below code but i get every word of my file being displayed one below above.Is there any way i can modify the for loop code to get the output returned by while loop?

:::For Loop:::
Program:
Code:
for line in $(cat Filename.txt)
do
echo $line
done
output:
Code:
My
First
Line
My
Second
Line

Last edited by pinga123; 07-21-2010 at 01:11 AM. Reason: wrong tagging done
 
Old 07-21-2010, 01:41 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
a real programmer is going to come up with something a bit more elegant.....

Code:
fil="Filename.txt"
for ((a=0; a<$(wc -l $fil|sed 's/ .*//') ; a++ )); do
    read line
    echo $line
done < $fil
 
Old 07-21-2010, 02:25 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well I am not sure about the "real programmer" part:
Code:
IFS=$'\n'

for i in $(cat file)
do
    echo "$i"
done
 
Old 07-22-2010, 12:16 AM   #4
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
Well I am not sure about the "real programmer" part:
Code:
IFS=$'\n'

for i in $(cat file)
do
    echo "$i"
done
This looks bit neat and simple.

However i have a little doubt in my mind.
In my earlier code ,if IFS was set to space(assuming from the output).

Code:
for i in $(cat Filename.txt)
do
echo "$IFS $i"
done
Why would output shows space in every line.
output:
Code:
 My

 First

 Line

 My

 Second

 Line
Output should have been without any space in line.
 
Old 07-22-2010, 12:37 AM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Here's my solution. Hope I understood the problem correctly.

Code:
for (( ;  ; )) ; do if read a ; then echo $a ; else break ; fi ; done < foo.txt
Quote:
Originally Posted by `The GNU Bash Reference Manual' Version 3.2, section 3.2.4.1 Looping Constructs
for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do COMMANDS ; done
First, the arithmetic expression EXPR1 is evaluated according to
the rules described below (*note Shell Arithmetic::). The
arithmetic expression EXPR2 is then evaluated repeatedly until it
evaluates to zero. Each time EXPR2 evaluates to a non-zero value,
COMMANDS are executed and the arithmetic expression EXPR3 is
evaluated. If any expression is omitted, it behaves as if it
evaluates to 1.
The return value is the exit status of the last
command in LIST that is executed, or false if any of the
expressions is invalid.


The `break' and `continue' builtins (*note Bourne Shell Builtins::)
may be used to control loop execution
.
 
Old 07-22-2010, 02:13 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
Originally Posted by pinga123
However i have a little doubt in my mind.
In my earlier code ,if IFS was set to space(assuming from the output).

Code:
for i in $(cat Filename.txt)
do
echo "$IFS $i"
done
Why would output shows space in every line.
output:
Code:
My

First

Line

My

Second

Line
Output should have been without any space in line.
You will need to explain further as I do not understand what you are trying to demonstrate here?
Using your example code, I agree with the output.
 
Old 07-23-2010, 01:41 AM   #7
pinga123
Member
 
Registered: Sep 2009
Posts: 684
Blog Entries: 2

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
You will need to explain further as I do not understand what you are trying to demonstrate here?
Using your example code, I agree with the output.
I think my question was not clear. Hope this will clear my doubt.

I have written little scriptlet to explain my doubt.
In below script value of IFS variable is same(Derived this conclusion from output ).So why the output is different for each cases.
What is the Default value of IFS variable?
I even checked environment variable but couldn't find anything.
Code:
env | grep IFS
content of file tempfile
Code:
my first line
my second line
script:
Code:
echo "Value of IFS is $IFS :"
echo "Start"
for i in $(cat tempfile)
do
    echo "$i $IFS"
done
echo "End"

IFS=$'\n'
echo "Start"
echo "Value of IFS is $IFS :"
for i in $(cat tempfile)
do
    echo "$i $IFS"
done
echo "End"
output:

Code:
Value of IFS is
 :
Start
my

first

line

my

second

line

End
Start
Value of IFS is
 :
my first line

my second line

End
I hope u understand my question this time.

Last edited by pinga123; 07-23-2010 at 01:43 AM.
 
Old 07-23-2010, 03:58 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Ok I am with you now. Your test for display IFS is inaccurate and so you are not understanding its full value.

The default for IFS:
Quote:
$IFS defaults to whitespace (space, tab, and newline)
So using this information, when it first looks at this piece of your code:
Code:
for i in $(cat tempfile)
do
    echo "$i $IFS"
done
This code now looks at the lines of tempfile and breaks them up using ALL 3 of the standard fields being used by IFS.
Also to note, as both echo and standard IFS have a newline in them or output by them, is why your statements are spaced so far apart vertically.

Now when you change IFS to only equal a newline it splits your file up accordingly.

Hope that helps.
 
Old 07-23-2010, 04:14 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Instead of messing with your IFS variable and in some cases forgetting to reset it back and causing future problems, use the while read loop.
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Loop or check SQlite3 database continuously, when query found run script for first 3 Techno Guy Linux - Newbie 3 09-27-2009 08:46 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 08:30 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM


All times are GMT -5. The time now is 09:52 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration