简体   繁体   English

使用sed组合多个csv文件

[英]Using sed to combine multiple csv files

I have 3 csv files I'd like to combine. 我有3个csv文件,我想结合起来。 Each file has 3 comma delimited columns. 每个文件都有3个逗号分隔的列。

File 1 has columns a,b,c
File 2 has columns d,e,f
File 3 has columns g,h,i

I'd like to combine the 3 files into a single file of: 我想将3个文件合并为一个文件:

a,b,c,e,f,h

Can I use sed to do that? 我可以使用sed来做到这一点吗?

I could write a console app or script easily enough but I'm attempting to get some sed skills and believe this should be a suitable task? 我可以轻松地编写控制台应用程序或脚本,但我试图获得一些sed技能,并认为这应该是一个合适的任务?

或者只是剪切和粘贴:

paste -d ',' file[123] | cut -d ',' -f 1,2,3,5,6,8

你可以做:

paste file[123] | sed 's/\t/,/g' | cut -d',' -f 1,2,3,5,6,8

Mat Mendel's answer is good to go unless you happen to be on Windows using cygwin in which case some annoying end of line character quirks come into play. Mat Mendel的答案很好,除非你恰好在Windows上使用cygwin,在这种情况下,一些烦人的行尾字符怪癖会发挥作用。 This is down to the unix command utilities, in this case paste and cut, using \\n as the end of line character instead of the \\r\\n that Windows wants. 这取决于unix命令实用程序,在本例中为粘贴和剪切,使用\\ n作为行尾字符而不是Windows所需的\\ r \\ n。

I couldn't qucikly work out how to change the end of line character for those utils or cygwin in general so I was happily able to make use of sed after all. 我无法彻底解决如何更改那些utils或cygwin的行尾字符,所以我很高兴能够使用sed。

paste -d ',' file1 file2 file3 | sed 's/\r//g' | cut -d ',' -f 1,2,3,5,6,8 | sed 's/$/\r/'

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

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