LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using cat readline within cat readline while loop (https://www.linuxquestions.org/questions/programming-9/using-cat-readline-within-cat-readline-while-loop-606827/)

demxkn66 12-14-2007 02:33 PM

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

demxkn66 12-14-2007 02:49 PM

I did also check other forums:

Is the problem using the same variable name 'line'?

So orginal code:
Quote:

Originally Posted by demxkn66 (Post 2990835)
Hi All,

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

Should be replaced by

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 :-)

radoulov 12-14-2007 03:29 PM

If you post a sample from your input files and the desired output,
it would be easier.

ghostdog74 12-14-2007 09:13 PM

see here for similar situation.

demxkn66 12-16-2007 04:41 PM

Quote:

Originally Posted by radoulov (Post 2990875)
If you post a sample from your input files and the desired output,
it would be easier.

file1:
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. :-)

colucix 12-16-2007 05:10 PM

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
exec 3< file1
while read -u 3 line
do
  echo $line
  exec 4< file2
  while read -u 4 line
  do
      echo $line
  done
  exec 4<&-
done
exec 3<&-



All times are GMT -5. The time now is 04:56 PM.