在unix,bash中合并csv文件 [英] merge csv files in unix, bash

查看:106
本文介绍了在unix,bash中合并csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些csv文件,其格式如下:

I have a few csv files that are formatted like:

test1.csv:

test1.csv:

    field,port1
    a1,0.2
    a2,0.3
    a3,0.6

test2.csv:

test2.csv:

field,port2
b1,0.5
b2,0.6
b3,0.7
b4,0.1
b5,0.5

test3.csv:

test3.csv:

field, port3
c1,0.1
c2,0.4

依此类推.我想将这些csvs合并为一个,这样看起来就像:

and so forth.I want to merge these csvs into a single so it would look like:

field,port1,field,port2,field,port3
a1,0.2,b1,0.5,c1,0.1
a2,0.3,b2,0.6,c2,0.4
a3,0.6,b3,0.7,,
,,b4,0.1,,
,,b5,0.5,,

我该怎么做?我喜欢>>,但是那只是前两列中的所有内容.如果必须的话,我可以采取这种方式,但是进行这样的合并可以使我的生活变得更加简单.

how can I do this? I cat >> but that would but everything in the first two columns. I can go about that way if I have to but having a merge like this can make my life a lot simpler.

谢谢

推荐答案

以fedorqui的答案为基础:

Building on fedorqui's answer:

paste -d: test[1-3].csv | sed -e's/^:/,:/' -e's/::/:,:/g' -e's/::/:,:/g' -e's/:$/:,/' -e's/:/,/g'

(假设文件中没有:-但您可以选择其他临时分隔符)

(assuming you have no : in your files - but you can choose another temporary separator)

这将恢复您期望的所有逗号.这对相同的替换指令是必需的,因为另一个替换不会考虑替换的字符串.

This restores all the commas you expect. The pair of identical substitution instructions is needed because a substituted string is not taken into account for another substitution.

通常:

paste -d'T' file... | sed -e's/^T/ET/' -e's/TT/TET/g' -e's/TT/TET/g' -e's/T$/TE/' -e's/T/S/g'

其中,T是临时分隔符(上面的:),E是应该替换空行或缺少行的字符串(上面的,),而S是以下行之间的分隔符paste d个文件(上面的,).临时分隔符T(通用字符串)不得出现在文件和E中,而最终分隔符S可以出现.

where T is the temporary separator (: above), E is the string that should replace an empty or missing line (, above), and S is the separator between the lines of the pasted files (, above). The temporary separator T (a generic string) must not appear in the files and in E, while the final separator S can.

警告:以上命令可能在外壳程序中带引号的字符串之前需要空格

Warning: The above commands may need spaces before the quoted strings in your shell

这篇关于在unix,bash中合并csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆