Linux bash 使用所有可用行将两个文本文件编织成一个替代行

Linux bash weave two text files into one alternative line using all available lines

我希望将两个文本文件合并为一个, 一个文本文件中的所有行都变成奇数行号 另一个文件变成偶数行号 比如将文件编织在一起

input1.txt

1, 267, Note_on_c, 0, 67, 100
1, 758, Note_on_c, 0, 58, 100
1, 1248, Note_on_c, 0, 79, 100
1, 1739, Note_on_c, 0, 52, 100

input2.txt

1, 368, Note_off_c, 0, 67, 127
1, 936, Note_off_c, 0, 58, 127
1, 1415, Note_off_c, 0, 79, 127
1, 1917, Note_off_c, 0, 52, 127

并且需要组合这些文本文件来创建以下输出

1, 267, Note_on_c, 0, 67, 100
1, 368, Note_off_c, 0, 67, 127
1, 758, Note_on_c, 0, 58, 100
1, 936, Note_off_c, 0, 58, 127
1, 1248, Note_on_c, 0, 79, 100
1, 1415, Note_off_c, 0, 79, 127
1, 1739, Note_on_c, 0, 52, 100
1, 1917, Note_off_c, 0, 52, 127

所以我希望合并这些文件,因为 weave

使用 GNU sedR 命令:

sed 'R input2.txt' input1.txt

输出:

1, 267, Note_on_c, 0, 67, 100
1, 368, Note_off_c, 0, 67, 127
1, 758, Note_on_c, 0, 58, 100
1, 936, Note_off_c, 0, 58, 127
1, 1248, Note_on_c, 0, 79, 100
1, 1415, Note_off_c, 0, 79, 127
1, 1739, Note_on_c, 0, 52, 100
1, 1917, Note_off_c, 0, 52, 127

来自man sed

R filename: Append a line read from filename. Each invocation of the command reads a line from the file. This is a GNU extension.

另一种选择是使用 paste 和换行符作为分隔符

paste -d '\n' input1.txt input2.txt
1, 267, Note_on_c, 0, 67, 100
1, 368, Note_off_c, 0, 67, 127
1, 758, Note_on_c, 0, 58, 100
1, 936, Note_off_c, 0, 58, 127
1, 1248, Note_on_c, 0, 79, 100
1, 1415, Note_off_c, 0, 79, 127
1, 1739, Note_on_c, 0, 52, 100
1, 1917, Note_off_c, 0, 52, 127

来自man paste

Write lines consisting of the sequentially corresponding lines from each FILE