I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA
and FileB
.
FileA:
[jaypal:~/Temp] cat filea
this is File A line1
this is File A line2
this is File A line3
FileB:
[jaypal:~/Temp] cat fileb
this is File B line1
this is File B line2
this is File B line3
Current Sample Script:
[jaypal:~/Temp] cat read.sh
#!/bin/bash
while read lineA
do echo $lineA
while read lineB
do echo $lineB
done < fileb
done < filea
Execution:
[jaypal:~/Temp] ./read.sh
this is File A line1
this is File B line1
this is File B line2
this is File B line3
this is File A line2
this is File B line1
this is File B line2
this is File B line3
this is File A line3
this is File B line1
this is File B line2
this is File B line3
Problem and desired output:
This loops over FileB completely for each line in FileA. I tried using continue, break, exit but none of them are meant for achieving the output I am looking for. I would like the script to read just one line from File A and then one line from FileB and exit the loop and continue with second line of File A and second line of File B. Something similar to the following script -
[jaypal:~/Temp] cat read1.sh
#!/bin/bash
count=1
while read lineA
do echo $lineA
lineB=`sed -n "$count"p fileb`
echo $lineB
count=`expr $count + 1`
done < filea
[jaypal:~/Temp] ./read1.sh
this is File A line1
this is File B line1
this is File A line2
this is File B line2
this is File A line3
this is File B line3
Is this possible to achieve with while loop?
paste -d '\n' file1 file2
– whoan Dec 30 '14 at 00:48