Preserve line endings

You can use the -b option for sed to have it treat the file as binary. This will fix the problem with cygwin's sed on Windows.

Example: sed -b 's/foo/bar/'

If you wish to match the end of the line, remember to match, capture and copy the optional carriage return.

Example: sed -b 's/foo\(\r\?\)$/bar\1/'

From the sed man page:

-b      --binary

This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made—as is the case for MS-DOS, Windows, Cygwin—text files are composed of lines separated by a carriage return and a line feed character, and sed does not see the ending CR. When this option is specified, sed will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed.`


You could try to sub the \n for \r\n at the end of your existing script like so:

sed 's/foo/bar/;s/$/\r/'

or perhaps

 sed -e 's/foo/bar/' -e 's/$/\r/'

If neither of the above two work, you'll have to consult the specific man page for your version of sed to see if such an option exists. Note that the *nix versions of sed do not alter the line terminators without being told to do so.

Another alternative is to use the cygwin version of sed which shouldn't have this undesirable behavior.

Tags:

Windows

Sed