sipsorcery
sipsorcery

Reputation: 30699

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?

Upvotes: 4

Views: 4179

Answers (3)

sipsorcery
sipsorcery

Reputation: 30699

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/'

Upvotes: 1

Matt Mendell
Matt Mendell

Reputation: 106

Or just cut and paste:

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

Upvotes: 9

codaddict
codaddict

Reputation: 454960

You can do:

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

Upvotes: 3

Related Questions