简体   繁体   中英

Using sed to combine multiple csv files

I have 3 csv files I'd like to combine. Each file has 3 comma delimited columns.

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:

a,b,c,e,f,h

Can I use sed to do that?

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?

或者只是剪切和粘贴:

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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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