Preserve line endings

WindowsSed

Windows Problem Overview


I run sed to do some substitution on windows and I noticed that it automatically converts line endings to Unix (\n). Is there an option to tell sed to use Windows line endings (\r\n) or even better to preserve the line endings from the file?

Note: I use sed from unxutils: http://unxutils.sourceforge.net/

Windows Solutions


Solution 1 - Windows

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.`

Solution 2 - Windows

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.

Solution 3 - Windows

Alternatively, (the cygwin version of) perl -pe doesn't seem to have this problem.

Solution 4 - Windows

Gnuwin can be suppressed to mess up the newlines (win->unix) if you only specify the -b switch and redirect. Using the -i (inline) switch will mess it up.

E.g. sed.exe -b "s/\xFF\xFE//" c:\temp\in.csv > c:\temp\out.csv

Solution 5 - Windows

I've found that sed-4.4.exe from https://github.com/mbuilov/sed-windows is pure win as it

  • uses windows CRLF line endings in default mode
  • preserves original line endings in -b mode
  • works correctly with in-place -i mode
  • also offers -z mode with \0 delimeters instead of \n which may be handy sometimes too

See also list of sed options and list of all windows sed ports.

Note that gnuwin32 sed 4.2.1 does corrupt line endings in -bi mode and doesn't have -z mode at all.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBogdan CalmacView Question on Stackoverflow
Solution 1 - WindowsShlomoView Answer on Stackoverflow
Solution 2 - WindowsSiegeXView Answer on Stackoverflow
Solution 3 - WindowsseekerView Answer on Stackoverflow
Solution 4 - WindowsbuckleyView Answer on Stackoverflow
Solution 5 - WindowsVadzimView Answer on Stackoverflow