[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sed+windows/cygwin+newlines: improving documentation

From: Assaf Gordon
Subject: sed+windows/cygwin+newlines: improving documentation
Date: Tue, 9 Oct 2018 13:03:15 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1


(Eric and Eli - your cygwin/windows related input is very appreciated).

There are two remaining open bugs in sed, both relating to windows,
cygwin and newlines:

It seems the behavior isn't trivial or intuitive, I'm thinking of adding
a dedicated chapter to the sed manual about windows. Hopefully it should
clear things up and save some support issues.

Comments are welcomed, to make sure I get it right.


There are multiple variables that affect how sed behaves on windows:

1. Does the input file contains CR/LF (\r\n) or just LF (\n).
2. Running under cygwin or in cmd.exe
3. If cygwin:
   3.1. Is it recent enough cygwin that does only uses LF.
   3.2. Is the drive mounted with "text" or "binary".
4. which version of sed
5. Using a cygwin-based binary (i.e. one that requires cygwin1.dll)
   or a mingw-based binary
6. Using the "-b" option
7. Reading from a file or from STDIN


The default cygwin binary (or one built with cygwin's gcc)
in a typical modern cygwin installation is always in binary mode, and CR
(\r) is just another character, like under unix. The "-b" is
essentially a no-op.
This is good, because it's easy enough to explain.

It seems a mingw-based binary is (almost) always in 'text mode'
(i.e. trims CR/LF and adds CR/LF at end of lines),
which is also OK, and reasonable enough to explain.

The one strange exception is:
1. mingw binary
2. reading from a file (not pipe or redirection)
3. using "-b"
Then, upon *reading*, only LF (\n) are counted,
while CR (\r) are treated like just another character (like in unix).
But when writing, it still adds CR+LF (and converts LF-only input to
CRLF output).

Based on the code, I thought it would also create files
with only "LF" when using in-place file output, but I couldn't make it

Are there any other conditions/variables?

 - assaf


## PIPE ##

$ printf "a\r\n" | sed-4.5.32-gcc    's/$/X/' | od -tc -An
   a  \r   X  \n

$ printf "a\r\n" | sed-4.5.32-gcc -b 's/$/X/' | od -tc -An
   a  \r   X  \n

$ printf "a\r\n" | sed-4.5.32-mingw64   's/$/X/' | od -tc -An
   a   X  \r  \n

$ printf "a\r\n" | sed-4.5.32-mingw64 -b 's/$/X/' | od -tc -An
   a   X  \r  \n

## Redirection ###

$ env printf 'a\r\n' > test

$ sed-4.5.32-mingw64    's/$/X/' < test | od -tc -An
   a   X  \r  \n

$ sed-4.5.32-mingw64 -b 's/$/X/' < test | od -tc -An
   a   X  \r  \n

## FILE ##

$ sed-4.5.32-mingw64    's/$/X/' test | od -tc -An
   a   X  \r  \n

$ sed-4.5.32-mingw64 -b 's/$/X/' test | od -tc -An
   a  \r   X  \r  \n

reply via email to

[Prev in Thread] Current Thread [Next in Thread]