for-while loop issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for-while loop issue
# 1  
Old 08-17-2010
for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here

Input file: space separated 2 columns

Quote:
ODS_INTRNL_GRADING ODS_LEGAL_ENTITY
ODS_LD_PLE ODS_PLE
ODS_SECURITY_RECOVERY_RATE ODS_SECURITY
ODS_UTILISATION_NONFM ODS_LEGAL_ENTITY
ODS_UTILISATION_NONFM ODS_PLE
ODS_PERSON_FCLT_RLTN ODS_FACILITY
ODS_PERSON_LGEN_RLTN ODS_LEGAL_ENTITY
ODS_PERSON_LGEN_RLTN ODS_PERSON
I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked with a while loop but not with a for loop.



Code with for loop:

Code:
for i in `cat tmp.dat `
          do
         c=`echo $i|awk -F " "  '{ print $1 }'`
        p=`echo $i|awk -F " " '{ print $2 }'`
         echo $c,$p
          echo -------
         done

Output ( wrong):

Quote:
ODS_INTRNL_GRADING
ODS_LD_PLE
ODS_SECURITY_RECOVERY_RATE
ODS_UTILISATION_NONFM
ODS_UTILISATION_NONFM
ODS_PERSON_FCLT_RLTN
ODS_PERSON_LGEN_RLTN
ODS_PERSON_LGEN_RLTN,ODS_LEGAL_ENTITY
ODS_PLE
ODS_SECURITY
ODS_LEGAL_ENTITY
ODS_PLE
ODS_FACILITY
ODS_LEGAL_ENTITY
ODS_PERSON
-------
While loop code:

Code:
while read i
        do
         c=`echo $i|awk -F " "  '{ print $1 }'`
        p=`echo $i|awk -F " " '{ print $2 }'`
        echo $c,$p
         echo -------
        done < tmp.dat

Output(correct):

Quote:
ODS_INTRNL_GRADING,ODS_LEGAL_ENTITY
-------
ODS_LD_PLE,ODS_PLE
-------
ODS_SECURITY_RECOVERY_RATE,ODS_SECURITY
-------
ODS_UTILISATION_NONFM,ODS_LEGAL_ENTITY
-------
ODS_UTILISATION_NONFM,ODS_PLE
-------
ODS_PERSON_FCLT_RLTN,ODS_FACILITY
-------
ODS_PERSON_LGEN_RLTN,ODS_LEGAL_ENTITY
-------
ODS_PERSON_LGEN_RLTN,ODS_PERSON
-------

shell /bin/ksh
Linux lonrs00845 2.6.9-42.0.8.ELsmp #1 SMP Tue Jan 23 12:49:51 EST 2007 x86_64 x86_64 x86_64 GNU/Linux


Was hoping someone could tell me why the for loop is printing all first columns first.. followed by the 2nd columns ..but the while loop correctly prints:
col1, col2
col1, col2..etc ( bascially the input file again )

i put in the IFS as "\n" as well..
thanks all ..much appreciated...is not a code as such..something i was trying out and cudnt figure out the cause.
# 2  
Old 08-17-2010
Hi.

The man page explains it:

Code:
for vname [ in word ... ] ;do list ;done
              Each  time  a for command is executed, vname is set to the next word taken from the in word list.

That's to say your $i variable would get one word at a time, not the line, as you expected.

The while loop is the better way, and can be shorter still:

Code:
while read c p; do
  echo $c,$p
  echo -------
done < tmp.dat

# 3  
Old 08-17-2010
First off, you get a Useless Use of Cat Award. You can make your program faster and less buggy by doing loops like
Code:
while read INPUT
do
done < file.txt

instead of
Code:
for INPUT in `cat file.txt`
do
done

since by politely reading the lines one at a time, you won't ever run into the limits of a shell size variable, and won't need to consume the entire size of the list in memory at once.

The read builtin also lets you separate both by IFS and by line, while the 'for' loop tries to jam it all in one big line.

Code:
while read A B
do
done < input.txt


Last edited by Corona688; 08-17-2010 at 01:12 PM..
# 4  
Old 08-17-2010
more info pls

thanks for the early reply guys..
@scottn -- if the for loop read it as 2 words, I should get the output as:

col1
col2
col1
col2
but i get it as:

col1
col1
col1
col1
col2
col2...etc
and i should still get multiple line separators instead of the single one i am getting at the output..


@corona..shouldnt the IFS take care of splitting the for loops into 2 variables as well?

and ya..ill take the award anyway..somethin better than nothin i guess :-P
# 5  
Old 08-17-2010
Quote:
Originally Posted by foal_newbie
@corona..shouldnt the IFS take care of splitting the for loops into 2 variables as well?
you're reading the entire file into memory as one ugly clump then splitting the whole clump instead of line by line. As such, it doesn't understand the concept of 'lines'. If you ever get a line with THREE things in it by accident, it will throw the entire rest of your processing into chaos and disarray.

Also, you're reading things one at a time, not two at a time.

Have you tried my solution? It works.
# 6  
Old 08-17-2010
Quote:
Originally Posted by foal_newbie
thanks for the early reply guys..
@scottn -- if the for loop read it as 2 words, I should get the output as:

col1
col2
col1
col2
but i get it as:

col1
col1
col1
col1
col2
col2...etc
and i should still get multiple line separators instead of the single one i am getting at the output..


@corona..shouldnt the IFS take care of splitting the for loops into 2 variables as well?

and ya..ill take the award anyway..somethin better than nothin i guess :-P
Hi.

I can't possibly see how you got that output with the code you posted.

Did your script do anything else with the output before you posted it?
# 7  
Old 08-18-2010
more info pls

hi,



@scott...i jus pasted the code from unix into the forum.. i never used shells on linux ...been using ksh on AIX since long so i figured prob somethin i dont noe here..
shells cant be diff on 2 systems ( at least as much as the output)..
I didnt write a script as such...was jus running the commands from the shell..trying things out..


will try it again tomo on the office system whr i got the issue and update post again..else lemme noe if you guys can provide some insight..

coronas soln wud work im sure..

---------- Post updated at 10:03 PM ---------- Previous update was at 11:19 AM ----------

Hi,

Update on the issue:

Opened new shell and executed for loop: get multi line-separators.

Quote:
ODS_INTRNL_GRADING,
-------
ODS_LEGAL_ENTITY,
-------
ODS_LD_PLE,
-------
ODS_PLE,
-------
ODS_SECURITY_RECOVERY_RATE,
-------
ODS_SECURITY,
-------
ODS_UTILISATION_NONFM,
-------
ODS_LEGAL_ENTITY,
-------
ODS_UTILISATION_NONFM,
-------
ODS_PLE,
-------
ODS_PERSON_FCLT_RLTN,
-------
ODS_FACILITY,
-------
ODS_PERSON_LGEN_RLTN,
-------
ODS_LEGAL_ENTITY,
-------
ODS_PERSON_LGEN_RLTN,
-------
ODS_PERSON,
-------
Ran again after setting export IFS="\n"
Quote:
ODS_INTRNL_GRADING
ODS_LD_PLE
ODS_SECURITY_RECOVERY_RATE
ODS_UTILISATION_NONFM
ODS_UTILISATION_NONFM
ODS_PERSON_FCLT_RLTN
ODS_PERSON_LGEN_RLTN
ODS_PERSON_LGEN_RLTN,ODS_LEGAL_ENTITY
ODS_PLE
ODS_SECURITY
ODS_LEGAL_ENTITY
ODS_PLE
ODS_FACILITY
ODS_LEGAL_ENTITY
ODS_PERSON
-------
Wondering how IFS messes the output like this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue when doing a loop

Hi, I just have started learning shell scripting (sh). Why do i only get the date? while read dt do echo "Date : ${dt} sed -n '/${dt}/,/${dt}/p' file1.log | grep -w ERROR done < date1.dat INPUT - date1.dat 2019-04-05 04:58:25 2019-04-05 04:58:26 2019-04-05 05:00:56... (3 Replies)
Discussion started by: margel
3 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

Issue with while loop?

Hi, I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a... (11 Replies)
Discussion started by: svajhala
11 Replies

4. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

5. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

6. Shell Programming and Scripting

until loop issue.

Hi, my script is waiting for 3 files to come to a folder for 30 min but even when all the files arrive in the folder it's still waiting for these three files. Files can come with in 2 min and I want it to start processing them immediately after all the files arrive in the folder. until ; do... (3 Replies)
Discussion started by: gurpartap
3 Replies

7. Shell Programming and Scripting

loop issue

function ext { echo "THANKS & WELCOME BACK" } function upc { echo "TO EXPORT UPROC GIVE UPROC NAME PER LINE IN THE input.txt and PRESS Y" echo "TO GO BACK PRESS 99" read parm0 if ; then start elif ; then for i in `cat input.txt` ; do echo $i $UXEXE/uxext upr upr=$i... (0 Replies)
Discussion started by: kojo
0 Replies

8. Shell Programming and Scripting

loop issue

I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below: host www184.domain.com { hardware ethernet 00:13:72:3B:B4:3A; fixed-address 192.168.0.184; }... (4 Replies)
Discussion started by: kkkk
4 Replies

9. Shell Programming and Scripting

while loop issue

Hi, Following is my code and the file FILE_LIST_EXCESS.txt has 40 file names in it while read LineIn do echo ${LineIn} `ftp -vin << END_INPUT >> ${PID}_DS_GET_Log.log 2>&1 open servername user userid password cd FileDir get ${LineIn} END_INPUT`... (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Help With A For Loop Issue

I was wondering how I can modify this for loop, so it only loops through the filenames that do not have an ".old" extension. for filename in $(ls "$1") do echo $filename | grep '\.old$' > /dev/null if then mv $1/$filename $1/$filename.old fi done (5 Replies)
Discussion started by: ralts01
5 Replies
Login or Register to Ask a Question

Featured Tech Videos