Using cat readline within cat readline while loop
Hi All,
I'working using bash and the SHELL script that is executed is written in #!/bin/sh . Situation: A There are 2 textfiles (file1 + file2) that contain lines that need to be processed individually. B File1 consist of 5 lines C File2 consist of 11 lines The following functionality is required: I) 1st line of file1 need to be retrieved and then all lines of file2 need to be processed II) repeat step I for 2nd line of file1, 3rd line ... etc until all lines of file1 are processed. My code that i'm using: cat file 1 | while read line do echo $line ... do some processing cat file2 | while read line do echo $line ... do some processing done done Result when running above code: - Line 1 of file1 is read then All lines of file 2 are read - script continues, Line 2,3,4 and 5 of file1 isn't processed After testing it seems having 2 filehandles open (cat read line within cat read line is techinically possible? Maybe there is a better solution in geting this functionality, maybe using (n)awk, I'm open for it? Thanks in advance for your hints. CU Olaf |
I did also check other forums:
Is the problem using the same variable name 'line'? So orginal code: Quote:
cat file 1 | while read linefile1 do echo $linefile1 ... do some processing cat file2 | while read linefile2 do echo $linefile2 ... do some processing done done Unfortunately I'm not able to test it right nwo not having unix environment at my current location :-) |
If you post a sample from your input files and the desired output,
it would be easier. |
see here for similar situation.
|
Quote:
carrier a carrier b carrier c carrier d file2: carrier a item 1 carrier a item 2 carrier a item 3 carrier a item 4 carrier a item 5 carrier a item 6 carrier a item 7 carrier a item 8 carrier a item 9 carrier a item 10 file 1 1st line is read 'carrier a', then file 2 is read line by line and some processing is done. Finally script stops after processing 1st line of file1, 2nd line until last line of file1 aren't processed anymore altough it supposed to do so. What can be the case why these aren't processed anymore? Thanks to you all contributors. :-) |
Strange... your original code works for me, using BASH 3.2.25. Anyway, you can try to explicitly assign file descriptors and keep input separate (use -u option to read from one descriptor or another)
Code:
#!/bin/bash |
All times are GMT -5. The time now is 04:56 PM. |