It is important to understand that sed works on a line by line basis. What sed does is basically : read a line into its buffer without the newline, execute your commands on the buffer, print the buffer (provided you haven't specified the
-n flag), read the next line into its buffer, etc. So to merge two lines with sed requires that you explicitly force sed to treat more than a single line at a time. To do that, the
D commands are your friend.
Now for your specific problem, to give you a specific and tested answer would require you to put a specific type of input, but here are some examples of what can be done :
This will merge every two lines together :
or if you are sure to always have \r\n line endings :
For a more tailored approach to what I understood of your question, although not the best solution, this should do the work provided you use bash or another shell that supports C escape via the
$'str' construct :
or without the C-style escape construct and with \r\n line endings (non-negotiable) :
What it does is basically append the next line to its buffer (
N) and test for the string you want (
/\r\\n";"/). The script loops (
bl --> branch to label
:l defined at the beginning) as long as it doesn't find a match. When a match is found, it executes the sed script between the curly braces : replace all occurrences of
s/\r\\n";"/";"/g) and flush the buffer and input the next line (
Of course, if the file is big and the "errors" are infrequent, this could run for a long time and take a lot of memory. If this is the case, another algorithm could be used, but I would need to have a better example of what you are up against to be sure that I understood your problem correctly.
Also, if you would like to learn a little more about sed, I strongly recommend this site which might not have the best background color, but is the best tutorial of sed out there IMO.