To show only the term I wish to find (for example, temperature
) and the immediately surrounding text, I am calling grep ".\{0,5\}temperature.\{0,5\}" *
This command works fine when the files have Unix end-of-lines. However, when searching files created in Windows (that end in CRLF), instead of grep showing the right result, the entire line appears blank whenever CRLF is within the 5-character range.
I have tried the -U
(binary) flag to no avail.
Here is the file I am using to test. I have a version saved with CRLF endings (test_dos.txt), and another with just LF (test_unix.txt):
the body temperature is the variable
temperature12345678
temperature1234567
temperature123456
temperature12345
temperature1234
temperature123
temperature12
temperature1
temperature
woie temperature!! tempe oaj
The result of calling grep -o ".\{0,5\}temperature.\{0,5\}" *
is:
test_dos.txt:body temperature is t
test_dos.txt:temperature12345
test_dos.txt:temperature12345
test_dos.txt:temperature12345
test_dos.txt:temperature12345
test_dos.txt:woie temperature!! te
test_unix.txt:body temperature is t
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature12345
test_unix.txt:temperature1234
test_unix.txt:temperature123
test_unix.txt:temperature12
test_unix.txt:temperature1
test_unix.txt:temperature
test_unix.txt:woie temperature!! te
What can I do so that grep shows the correct results with the Windows/DOS file?
grep
is this? I don't have time to dig into this, but if this is GNU grep, it seems the--color
option somehow interferes with its handling of CR in DOS-formatted files. I was not able to reproduce it when also using-o
, however. Try running it as/bin/grep
or\grep
to ensure you aren't executing an alias that specifies additional options.