Would be a nice solution, except that it doesn't work. Sed doesn't do regular expressions in the replacement part. Newish GNU versions of sed do actually support backslash-newline in the replacement part, like this:
Note how the first line ends in a backslash. The single quotes are important here.
I don't know whether newline-confusion is really the reason, or what else. I would try following: Search for an character, which will not occur normally in input. Assuming '#'. Translate with sed @->@#:
now I would compare sizes.
should be the difference between them. Then I would 'translate'.