Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
2 edited tags
| link
1
source | link

Grep can't find end of line in DOS format text file?

In Ubuntu I'm working on a C file that was originally created under Windows and then committed to source control. Vim shows the file format is "dos", which I gather just means it has CR/LF line endings. I wanted to get a list of function definitions in the file by using grep to find the regular expression ')$', but it returned nothing. When I changed the file format to "unix" (:set ff=unix in vim) then the grep worked as expected.

Is this a bug, or does grep officially not support CR/LF line endings? I did find this on the man page:

-U --binary Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses whether a file is text or binary as described for the --binary-files option. If grep decides the file is a text file, it strips carriage returns from the original file contents (to make regular expressions with ^ and $ work correctly). Specifying -U overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line, this will cause some regular expressions to fail. This option has no effect on platforms other than MS-DOS and MS-Windows.

But it does say under MS-DOS and MS-Windows in the previous sentence, so I don't know if that applies at all here?

Is there some other regular expression that should match CR/LF, or some other command-line option to grep I'm not seeing that would help? Or is the only solution really for me to create a bunch of commit-noise by reformatting my colleagues' files?