我想知道是否有任何一种方式可以同时嵌套嵌套循环读取两个输入文件。例如,假设我有两个文件FileA
和FileB
。
FileA:
[jaypal:~/Temp] cat filea
this is File A line1
this is File A line2
this is File A line3
文件B:
[jaypal:~/Temp] cat fileb
this is File B line1
this is File B line2
this is File B line3
当前示例脚本:
[jaypal:~/Temp] cat read.sh
#!/bin/bash
while read lineA
do echo $lineA
while read lineB
do echo $lineB
done < fileb
done < filea
执行:
[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
问题和期望的输出:
对于FileA中的每一行,这将完全遍历FileB。我尝试使用continue,break,exit,但是它们都不是为了实现我想要的输出。我希望脚本仅从文件A中读取一行,然后从文件B中读取一行,然后退出循环并继续执行文件A的第二行和文件B的第二行。类似于以下脚本的内容-
[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
使用while循环可以实现吗?
paste -d '\n' file1 file2