Karl Knechtel
Karl Knechtel

Reputation: 61519

Any way to force grep to understand Windows line endings?

I recently installed PortableGit from GitForWindows after finding out that Github for Windows is no longer packaging the Git Shell (which I had grown rather fond of). I would like to be able to grep my source files for text at the end of a line: e.g. grep -R -n --include "*.py" -P ":$" .. As written, that doesn't work due to the Windows line endings - I must use ":\r$" (or perhaps something fancier, to be more robust) instead. This is irritating; I find it hard to remember and I don't need such a workaround in gVim. I don't recall needing to do it with the grep utility built into Git Shell, either; but PortableGit's tools appear to play rather strictly by the book.

Is there any way I can force grep to treat \r\n as an end-of-line, or failing that any other amelioration here? I would prefer not to convert all my source to Unix file endings; but if that's what it takes then I would also need help in getting gVim to respect that convention (including for new files). (That's still not ideal, though; I also may want to grep the program output, and since I'm writing files in text mode in Python, they have Windows line endings naturally.)

Upvotes: 5

Views: 979

Answers (2)

Reinier Kleipool
Reinier Kleipool

Reputation: 1

Run dos2unix to convert the file to UNIX line endings then grep on the new file.

Upvotes: -1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

You cannot set a line break sequence other than the default one with grep.

However, you may do it with pcregrep. To force CRLF line endings, use the -N option like this:

pcregrep -N CRLF 'regex' file

Or, if you want to set it to CR (as on old MacOS):

pcregrep -N CR 'regex' file

Upvotes: 4

Related Questions