Try
sed 'N;s/\n"/"/'
It seems to work for me:
[VladD@Kenga] [00:59:25] [~] {0,504}$> cat xx.txt data1,data2,"data3 ",data4 data1,data2,"data3 ",data4 data1,data2,"data3 ",data4 [VladD@Kenga] [00:59:32] [~] {0,505}$> sed 'N;s/\n"/"/' xx.txt data1,data2,"data3",data4 data1,data2,"data3",data4 data1,data2,"data3",data4
For more complex cases (“ordinary” lines are possible) try this:
sed '/^",/{H;x;s/\n//;x;d}; x' | sed '1d'
Check:
[VladD@Kenga] [01:35:47] [~] {0,539}$> cat xx.txt header "data1",data2,"data3 ",data4 intermediate data data1,"data2 ","data3 ",data4 data1,data2,"data3 ",data4 [VladD@Kenga] [01:35:52] [~] {0,540}$> sed '/^",/{H;x;s/\n//;x;d}; x' xx.txt header "data1",data2,"data3",data4 intermediate data data1,"data2","data3",data4 data1,data2,"data3",data4 [VladD@Kenga] [01:35:57] [~] {0,541}$> sed '/^",/{H;x;s/\n//;x;d}; x' xx.txt | sed '1d' header "data1",data2,"data3",data4 intermediate data data1,"data2","data3",data4 data1,data2,"data3",data4
Attention : the last line must end with a line break , otherwise it will be “swallowed”!
Explanation: we need, when we see a line starting with quotation marks, to know the previous line in order to glue them. To do this, we “delay” the output of rows, sending them to hold space instead of output, and displaying instead the previous line lying in the same place ( x ).
For the case when the line starts with a quotation mark ( /^"/ ), we take effect. In hold space is the previous line, dock the current one ( H ) to it, and exchange hold space with the pattern space ( x ) so that the text can be processed. Delete \n ( s/\n// ), and send back the line to the hold space to analyze and output it in the next cycle. The stubs of the string that turned out in the pattern space are deleted, and we complete this iteration ( d ).