Using sed to merge multiple csv files

I have 3 csv files that I would like to combine. Each file has 3 comma-separated columns.

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

I would like to combine 3 files into one file:

 a,b,c,e,f,h 

Can I use sed for this?

I could write a console application or script quite easily, but am I trying to get some sed skills and think this should be a suitable task?

+4
source share
3 answers

Or just cut and paste:

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

You can do:

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

Mat Mendelโ€™s answer is good if you didnโ€™t end up on Windows using cygwin, in which case some annoying line endings come into play. This applies to unix command-line utilities, in which case we paste and cut using \ n as the end-of-line character instead of the \ r \ n that Windows requires.

I couldnโ€™t easily figure out how to change the end of the line character for these utils or cygwin in general, so I was happy to be able to use sed in the end.

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

All Articles