Nested while read line loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested while read line loop
# 1  
Old 06-22-2005
Question Nested while read line loop

Hi,

Can anyone please help me: i'm trying to read a file with directory-names , then go to that directory and read another (output) file to perform some tasks per line (second read line in the part of script below).
The problem is that after the nested while loop has finished, the first while finsihes too. So the first while loop only executes once Smilie . I'm guessing that I'm using read line the totally wrong way, but my knowledge of unix
is unfortunately not so good Smilie (just started scripting).


(part of script: I left out some irrelevant peaces to make it easier to read)


exec 0< dir_list_file.txt
while read line
do
cat input_file.txt | grep "^>>" | cut -c 4- > output_file.txt
exec 0< output_file.txt
while read line
do
empl_id=`echo $line | awk ' { print $1; } '`
empl_dir=`echo $line | awk ' { print $2; } '`
<... etc..>
done
done


Thanx in advance
# 2  
Old 06-22-2005
Code:
       while read line
       do
          empl_id=`echo $line | awk ' { print $1; } '`
          empl_dir=`echo $line | awk ' { print $2; } '`
        
       done  < output_file.txt

Take this block of code and put it in another separate file. Remove it from the old script and add a lien to the old script to call it from the old script.

The done < output_file.txt thing is another way to read a file.
# 3  
Old 06-23-2005
Thanx al lot Jim,

I will try your sollution, but is there any other way to fix something like this in one script?

Greetings
# 4  
Old 06-23-2005
It looks to me like the whole inner loop could be a single awk code block.
# 5  
Old 06-23-2005
What shell are you using? What does your input data look like? What os? This looks like it should be easy with ksh. But it's hard to tell with so little info.
# 6  
Old 06-24-2005
Sorry for the short info Smilie : im using the ksh on a sun/solaris and the input data is as following:

To describe the input data is quite complicated, but in a nutshell : I have a a flat-ascii file which contains directory-names. This file I want to read and with every red file, I'm reading the directories on an ftp-site. Then conditionally, after executing an Oracle-database package another text file is written (by this package). This is also a flat file with every line containing a path-name and jpg-filename. In the second read I have to create the path and copy the jpg-file. I hope its a little more clear now. The sollution of Jim is perfect, but I was just curious if I could make a nested while loop reading files...

Thanx for your replies and greetings,
Rakker
# 7  
Old 06-24-2005
I don't have the foggiest idea of what you're trying to do or what your data looks like. But maybe this will help.

Code:
while read directoryname ; do
      while read path jpg ; do
             #something goes here I guess
      done < $directoryname/secondfile
done < firstfile

This User Gave Thanks to Perderabo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

2. Shell Programming and Scripting

IF awk in a while read line-loop

Hi As a newbe in scripting, i struggle hard with my first script. What i want to do is, bringing data of two files together. file1: .... 05/14/12-04:00:00 41253 4259 5135 5604 5812 5372 05/14/12-04:10:00 53408 5501 6592 7402 7354 6639 05/14/12-04:20:00 58748 6037 7292 8223... (13 Replies)
Discussion started by: IMPe
13 Replies

3. Shell Programming and Scripting

Simple while read line loop question

#!/bin/bash count=1 while read line do if (($count > 4)); then awk -v var1="$count" '{printf "%3s%8s%11s%11s%11s\n",var1,$2,$3,$4,$5}' else echo $line fi count=$((count+1)) done < posre_sub.itp > test cat test INPUT: ; position restraints for... (3 Replies)
Discussion started by: origamisven
3 Replies

4. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

5. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

6. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies

7. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 Replies

8. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

9. UNIX for Dummies Questions & Answers

Alternative for a while read line loop

HELLO all :), I have been trying to use a simple while loop to read a file " templist", line by line and perform an action. See the code below. The reason for not using a while read line loop is the for the use of the if condition that wouldn't work. I would appreciate some ideas as this has... (2 Replies)
Discussion started by: kabs
2 Replies

10. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies
Login or Register to Ask a Question