如何在bash中附加两个文件

时间:2015-10-06 01:59:56

标签: linux bash

我的文件如下所示:

File 1
------
Field A, Field B, Field C
1,2,3

aa,bb,cc

File 2
------
Field D, Field E, Field F
4,5,6

dd,ee,ff

I want to join them so the file looks like the following:

Joined File
-----------
Field A, Field B, Field C, Field D, Field E, Field F
1,2,3,4,5,6

aa,bb,cc,dd,ee,ff

所以它应该跳过空行并将其他所有内容附加到该行,并使用逗号作为前缀。我这样做,所以我可以生成"列"在CSV文件中并维护标头。空行总是匹配,所以我不需要逻辑来检查这个方面。

1 个答案:

答案 0 :(得分:0)

此: paste -d, "file 1" "file 2" | sed -e 's/^,$//g;s/^,/,,,/g'

会给:

Field A, Field B, Field C,Field D, Field E, Field F
1,2,3,4,5,6

aa,bb,cc,dd,ee,ff
,,,o1,o2,o3

此: paste -d, "file 1" "file 2" | sed -e 's/^,/,,,/g;s/,$/,,,/g'

会给:

Field A, Field B, Field C,Field D, Field E, Field F
1,2,3,4,5,6
,,,,,
aa,bb,cc,dd,ee,ff
,,,o1,o2,o3

此: paste -d, "file 1" "file 2" | sed 's/^,$//g'

会给:

Field A, Field B, Field C,Field D, Field E, Field F
1,2,3,4,5,6

aa,bb,cc,dd,ee,ff
,o1,o2,o3