Query regarding for loop.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query regarding for loop.
# 1  
Old 07-21-2010
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

# 2  
Old 07-21-2010
Set your input field separator

Code:
$  IFS=$'\n'
$  for i in `cat file_name` 
>do 
>echo $i; 
>done

This User Gave Thanks to bankai For This Post:
# 3  
Old 07-21-2010
Quote:
Originally Posted by bankai
Set your input field separator

Code:
$  IFS=$'\n'
$  for i in `cat file_name` 
>do 
>echo $i; 
>done

Its working for me . Please use $() instead traditional `` .
Code:
IFS=$'\n'
for i in $(cat Filename.txt)
do
echo $i;
done

# 4  
Old 07-21-2010
Quote:
Its working for me . Please use $() instead traditional `` .
Scripts should be written with portability in mind, hence the use of bourne syntax is highly advised
This User Gave Thanks to bankai For This Post:
# 5  
Old 07-21-2010
Quote:
Originally Posted by bankai
Scripts should be written with portability in mind, hence the use of bourne syntax is highly advised
Thanks for the advise but few months earlier i was advised to use $() to make the script look better. However I also agree on ur point if $() is useless on other shell.


I have 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

It should have been without any space in line.
# 6  
Old 07-21-2010
Code:
for i in $(cat Filename.txt)
do
echo "$IFS $i"
done

Look at the example, why are you printing the IFS?

You need to set the IFS to a new line character -- this is not the default action.
# 7  
Old 07-21-2010
Quote:
Originally Posted by bankai
Code:
for i in $(cat Filename.txt)
do
echo "$IFS $i"
done

Look at the example, why are you printing the IFS?
that is the point. I just want to know what is the value of IFS without assigning it a new line character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If loop query

Hi, its getting aborted with below IF loop, can plz guide me what im missing here if || ; then echo "print $APPEND" fi (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

For loop query...

Hi all... I am using a for loop for another part of AudioScope... Consider this code:- for n in {0..100} do if }" = "another_string" ] then break fi done This works perfectly except I am not sure if breaking out of a 'for' loop might cause... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

4. Shell Programming and Scripting

awk loop - substr and sub query

Hi there Looking for help with formating text output. I have following file 1ABC1234567890123Y 31-Jul-2012 1DEF1234567890124NMEDIUM 31-Jul-2012 193939312345889 YSMALL 31-Jul-2012 10093939312345889YSMALL 31-Jul-2012 1 YSMALL 31-Jul-2012 ... (14 Replies)
Discussion started by: viallos
14 Replies

5. Shell Programming and Scripting

Query regarding the if loop

Hi friends, One of my shell program(test.ksh) contains the following if loop.Can anyone please explain me how this loop will work The shell script will be executed as test.ksh -d all The value of variables are user=all opt=-d if && then && _del_all_w=1 || _group="${1}"... (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

6. Shell Programming and Scripting

Need help to run query in loop for all acct nbr in a file..

I have a txt file with contents of acct nbr's like: 22222222222 33333333333 33445566778 I need to write a script which takes each acct nbr in the file and run the query like: select seq_nbr from event where acct_nbr='22222222222' and the query's output should be passed to a... (2 Replies)
Discussion started by: Rajesh Putnala
2 Replies

7. Shell Programming and Scripting

Need help to run query in loop for all acct nbr in a file..

I have a txt file with contents of acct nbr's like: 22222222222 33333333333 33445566778 I need to write a script which takes each acct nbr in the file and run the query like: select seq_nbr from event where acct_nbr='22222222222' and the query's output should be passed to a... (0 Replies)
Discussion started by: Rajesh Putnala
0 Replies

8. Shell Programming and Scripting

SQL query in a loop with single sqlplus connection

Hi, I'm trying to build a shell script that reads a set of accounts from a file. For each account I need to perform a set of sql queries. So I have a loop with a set of sqlplus connections to retrieved my data. Is it possible to have a single sqlplus connection before entering the loop and... (4 Replies)
Discussion started by: lsantacana
4 Replies

9. Shell Programming and Scripting

Dynamic update loop query on Sybase database

Hello Guys, I'm new to Shell scripting, and i need someone to help me with this issue: I'm trying to do a dynamic update query on Sysbase database table using shell script. Lets say, the Update query is "update Table set id=X" , where X is dynamic value for the loop index. If the loop is... (10 Replies)
Discussion started by: Alaeddin
10 Replies

10. UNIX for Dummies Questions & Answers

Simple loop query

Hi All Just started with shell scripts and am stumped by, what is to most of you no doubt, a simple issue. All I'm trying to do is prompt a user for input and writing to a log file. If the user types the word 'stop', then the program should halt. If the word typed is 'clear', then the log file... (2 Replies)
Discussion started by: kutz13
2 Replies
Login or Register to Ask a Question