Fixing Dos Line Endings

Linux Tux

Sometimes, when you're running coder on a module, you'll get a lot of errors complaining about Windows line endings. This is because you should set your editor to use Unix Line endings to be consistent with all developers. See the Drupal Coding Standards for more details.

Below is a handy bash script which will help you batch convert many files from DOS to Unix line endings.

grep -lIUr "^M" . | xargs sed -i 's/^M//'

First up, we use Grep to find ^M(which you can produce using Ctrl+V and then Ctrl+M). This is a special code for Carriage Return (Windows uses CRLF, Unix uses just LF).

-l
This tells grep to halt searching once its found the first instance - we only need 1 result per file.
-I
This tells grep to treat binary files as if there was no matching data
-U
This tells grep to treat the file as a binary file. Grep usually tries to guess the file type. If it guesses text, it will remove the CR characters from line endings to help keep Regex consistent across operating systems.
-r
This tells grep to be recursive

Next up, we pipe that result set into the sedcommand.

-i
This tells sed that we should edit all files in-place. If you like, you can change this to -ibakwhich would create a backup using the supplied suffix.
's/^M//'
This is a regular expressions find-and-replace. This tells sed to find all ^M characters (which are CR (Carriage Return) characters) and replace them with nothing (ie remove them).

This seemed to work really well for me - please post below if you have any alternative/better ways of doing this!

Note: I did try to use dos2unix however this did not remove a trailing ^Mfor some reason.