views:

51

answers:

3

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?

+2  A: 

You can do:

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

Or just cut and paste:

paste -d ',' file[123] | cut -d ',' -f 1,2,3,5,6,8
Matt Mendell
+1, lot better than mine :)
codaddict
Thanks! Symmetry is pretty :)
Matt Mendell
+1 - but be aware that if there are commas embedded in any of the data fields, all hell will break loose.
Jonathan Leffler
PS: You would find that [CSVFIX](http://code.google.com/p/csvfix/) can handle the pasted-together file even if there are embedded commas in fields.
Jonathan Leffler
sipwiz
+1  A: 

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/'
sipwiz
Ah yes, the extra '\r' issue. Nice to see you solved it!You could use tr -d '\r', instead of the first sed command.Don't get me wrong, sed is awesome, but a bit heavy weight for some tasks. :)
Matt Mendell