交织两个文件的bash脚本 [英] Interweave two files bash script
问题描述
我正在尝试交织每行包含一个句子的两个文件.我将第一个文件加倍(
I am trying to interweave two files that contain one sentence per line. I double spaced (sed G
) the first file and I would like to incorporate the content of the second file into those empty lines.
如何交织两个文件,以便文件B的第一行低于文件A的第一行,文件B的第二行低于文件A的第二行,直到到达末尾?
How can I interweave both files so that the 1st line of file B goes below the 1st line in file A, the 2nd line of file B below the 2nd line of file A, until it reaches the end ?
示例:[行号|句子号|句子]
Example: [line number|sentence number|sentence]
1 1 fileA
2
3 2 fileA
4
5 3 fileA
6
7 4 fileA
预期结果:
1 1 fileA
2 1 FILEB
3 2 fileA
4 2 FILEB
5 3 fileA
6 3 FILEB
7 4 fileA
这是一个bash脚本:可以用 sed
或 awk
完成吗?
This is for a bash script: can it be done with sed
or awk
?
推荐答案
这可能对您有用(GNU sed):
This might work for you (GNU sed):
sed 'R fileB' fileA
您不需要先将文件加倍.
You don't need to double space the file first.
如果您想替换空行:
sed -e '/./!{R fileB' -e ';d}' fileA
这篇关于交织两个文件的bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!