1

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?

2
  • 1
    Which version of 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. Aug 18, 2021 at 17:42
  • 1
    @JamesSneeringer yes, that's it!! I would never have guessed. If you post it as an answer i will accept it. It's GNU grep 3.4 Aug 18, 2021 at 17:49

1 Answer 1

2

Make sure you don't have an alias for grep that specifies additional options. Experimentally, it seems the --color option of GNU grep somehow interferes with its handling of CR in DOS-formatted files, which results in the output described in some cases. You can bypass an alias definition by prefixing the command with a \ (e.g., \grep), or by specifying the full path to the executable (e.g., /bin/grep).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .