2

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?

  • Doh - I literally found this a few seconds after posting my question: stackoverflow.com/questions/73833 – Kisama Oct 13 '15 at 11:40
  • Ubuntu is not MS-DOS or MS-Windows, so it does not apply. I'd think you'd have to grep $')\r$' to match the CR-NL line ending – glenn jackman Oct 13 '15 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.