Reset while loop to loop same file multiple times


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reset while loop to loop same file multiple times
# 1  
Old 10-09-2013
Reset while loop to loop same file multiple times

Hi,
I want to read file multiple times. Right now i am using while loop but that is not working.

ex.
Code:
While read line
do
    while read line2
      do
           echo stmt1
            #processing some data based on data.,
     done < file2.txt
done < file1.txt  # This will have 10 records,

First while loop running 10 times but inter while loop is only executed 1 time , 2nd time it is not reading same file. I assume that is because same file already looped. want to overcome this issue.

Please suggest some solution.

Thanks,
Mallik.

Last edited by Franklin52; 10-09-2013 at 11:04 AM.. Reason: Please use code tags
# 2  
Old 10-09-2013
Hi,

Could you please give your exact requirement please.



Thanks,
R. Singh
# 3  
Old 10-09-2013
You want to read what file multiple times?

You could consider adding a third loop. Where you add it depends on which file(s) you want to read multiple times.

Code:
for n in {1..10}; do
  while ...
    while ...
      ..
    done < file2
  done < file1
done

# 4  
Old 10-09-2013
Works for me. Outer loop reads a line from file1 then reads a line from file2 and so on:

Code:
while read file1;do echo $file1;while read file2;do echo $file2;done < file2;done < file1
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.
This is file #1.
This is file #2.

# 5  
Old 10-09-2013
Quote:
Originally Posted by in2nix4life
Works for me.
Good point!

I missed the statement:
Quote:
but inter while loop is only executed 1 time
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Variable inside while loop got reset

hi, I am using hp unix server and not getting variable output present inside the while loop. I have tried changing the code and need to verify whether it is proper practice of code. I am expecting the output of varible RUN_FILE 3 to TRUE which i get inside the while loop. RUN_FILE 1=TRUE... (8 Replies)
Discussion started by: gowthamsoft
8 Replies

2. Shell Programming and Scripting

Loop to execute 2 times and send an email alert

After the successful start of server, it should check the status again, if it is not running ,it should go through the loop for 2 times. Even after two times of execution if still the server is not running it should send an alert email. Please help (1 Reply)
Discussion started by: thomas9192
1 Replies

3. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

4. Shell Programming and Scripting

How to loop script for multiple file?

hi, i am running a java script on few thousand files (e1.rnk....en.rnk) but manually,. is there any way to run it automatically by changing the output directory (-out) ? thanx in advance ex: java -jar commands -res /path/e1.rnk -out /path/e1 -gui false java -jar commands -res /path/e2.rnk... (1 Reply)
Discussion started by: quincyjones
1 Replies

5. UNIX for Dummies Questions & Answers

How to remove first few characters from multiple file names without do loop?

Hi Fellows, I was wondering how I can remove first few characters from multiple file names without do loop in unix? e.g. water123.xyz water456.xyz to 123.xyz 456.xyz Thanks Paul Thanks. (3 Replies)
Discussion started by: Paul Moghadam
3 Replies

6. Shell Programming and Scripting

While loop reading file with multiple conditions

Hi Am trying to print the PIDs of process in a file and trying to grep any PID from that file I set the if condition as $value != "PID" and $value != "-" Assign that number to a variable Am confused since am using while loop to read the line from file and again if condition to check those... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

7. Shell Programming and Scripting

FASTEST way to loop a script 10k times

Is there any FASTEST way to loop a script 10k times my script works likes this c-randomnumbers-script -i input1.bed -g g19 -e DB >> output1 I need to run this 10k times by using consecutive outputs to get my final output i.e, output10000 c-random-script -i input1.bed -g g19 -e DB >>... (5 Replies)
Discussion started by: quincyjones
5 Replies

8. UNIX for Advanced & Expert Users

System call failed with 127 .. after 500 times when called in loop

Hi Experts, I have a code like this. ===== #include.... int main() { int count = 0; while(1){ printf("\n Interation number is: %d \n ",count); rv = system(" test.sh > log.txt " ); if (-1 == rv) { printf("Could not generate static log: error... (12 Replies)
Discussion started by: binnyjeshan
12 Replies

9. Shell Programming and Scripting

The loop was executed $count times

#!/bin/sh count=0 for i in 2 4 6 do echo "i is $i" count='expr $count + 1' done echo "The loop was executed $count times" with these scripts my output is : i is 2 i is 4 i is 6 The loop was executed expr $count + 1 times What should I do to get the value instead of 'expr... (17 Replies)
Discussion started by: ymwong
17 Replies

10. Shell Programming and Scripting

Loop to check for file up to 3 times

Please forgive this I think rather basic question. I have been away from UNIX for a very long time and am in need of some help. I need to be able to check for the existance of a specific file name say 'file.dat' in a particular location If the file exists then run a second process (at... (2 Replies)
Discussion started by: JohnCrump
2 Replies
Login or Register to Ask a Question