简体   繁体   English

融合两个csv文件

[英]Fuse two csv files

I am trying to fuse two csv files this way using BASH. 我正在尝试使用BASH以这种方式融合两个csv文件。

files1.csv : files1.csv:

Col1;Col2
a;b
b:c

file2.csv file2.csv

Col3;Col4
1;2
3;4

result.csv result.csv

Col1;Col2;Col3;Col4
a;b;0;0
b;c;0;0
0;0;1;2
0;0;3;4

The '0's in the result files are just empty cells. 结果文件中的“ 0”只是空单元格。 I tried using paste command but it doesn't fuse it the way I want. 我尝试使用粘贴命令,但它并没有按照我想要的方式融合。

paste -d';' file1 file2 

Is there a way to do it using BASH? 有没有办法使用BASH做到这一点?

Thanks. 谢谢。

One in awk: awk中的一个:

$ awk -v OFS=";" '
FNR==1  { a[1]=a[1] (a[1]==""?"":OFS) $0; next }    # mind headers
FNR==NR { a[NR]=$0 OFS 0 OFS 0; next }              # hash file1
        { a[NR]=0 OFS 0 OFS $0 }                    # hash file2
END     { for(i=1;i<=NR;i++)if(i in a)print a[i] }  # output
' file1 file2
Col1;Col2;Col3;Col4
a;b;0;0
b:c;0;0
0;0;1;2
0;0;3;4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM