0%

shell同时读取多个文件合并

现有多个文本文件想要合并在一起,以两个文件为例子吧:

1
2
3
4
#fileA.txt
this is File A line1
this is File A line2
this is File A line3
1
2
3
4
#fileB.txt
this is File B line1
this is File B line2
this is File B line3

想要实现交叉的合并,合并后结果为:

1
2
3
4
5
6
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

command1:

1
paste $fileA $fileB |tr "\t" "\n" > newfile

command2:

1
awk '{getline line < "fileB.txt";print $0;print line}' fileA.txt