Discussion:
Bug in grep.exe
(too old to reply)
Nicholas Hawthorn
2017-08-26 00:03:58 UTC
Permalink
Say we make a one line text file called "a" that contains
w CR LF

and then give the command:
grep -v "<" < a > b

File "b" will consist of:
w LF

In effect, grep has removed the CR. The bug was first found in a 66 line
.bat file where it was a bit of a nuisance.

For what it's worth grep.exe on my system is 80412 bytes and is dated 23
Jan 2014.

Best wishes,
Nick Hawthorn
Eli Zaretskii
2017-08-26 11:04:58 UTC
Permalink
Date: Sat, 26 Aug 2017 12:03:58 +1200
Say we make a one line text file called "a" that contains
w CR LF
grep -v "<" < a > b
w LF
In effect, grep has removed the CR.
Yes, and this is a feature. The exact command-line arguments are
unimportant: Grep on Windows _always_ strips CR characters from
Windows-style CRLF EOLs, and never adds them back when writing output
to files or pipes. This is because the alternative, of always adding
CR to LF-only Unix-style EOLs, which is what Windows text-mode output
would do, is much worse and will break many scripts and test suites.
It would also make it impossible to output anything past the first ^Z
byte, which serves as a "software EOF" indicator.
The bug was first found in a 66 line .bat file where it was a bit of
a nuisance.
Why is that a problem in a batch file? AFAIK, the stock Windows shell
in modern versions of MS-Windows has no problems running batch files
with Unix-style LF-only EOLs, unlike on old versions of the OS.

P.S. I think (but I'm not sure) latest versions of Grep removed this
feature, so perhaps you will be better off if you upgrade your Grep
installation.

Loading...