Preserve newlines

We currently process files line by line by splitting and then joining on '\n'.
This is a problem when newline is not '\n', e.g. '\r\n' on Windows. In this case, we replace all newlines with Unix newlines, and change all lines.

Here is a fix.
It replaces manual line-by-line iteration with a global multiline regex that drops trailing space without touching '\r' and '\n'.