Unix Wiki

When editing files in Windows, the resulting files will have ^M control characters. Most of the time, this will render the file useless on Unix

You can solve that easily using the dos2unix (or dos2ux on HP-UX) command, but, sometimes, you may want only to report such files, not fix them

Method 1: Using tr[]

tr '\015' '!' < BadFile | grep '!'

This will replace the ^M (specified as '\015' in the command line) for the exclamation mark. Then, a grep will inform you which lines had the offending character.

Of course, replace the '!' for a meaningful character for your problem. If you do this, for example, for a script, the ! will not work, since most of the unix scripts starts with #! in the first line.

Adding a -c to grep will inform you how many ^Ms the file has.

Method 2: Using grep and a auxiliary file[]

First, create a file in Windows (notepad.exe is enough) with ONE NEWLINE CHARACTER. Then, copy it to your unix machine IN BINARY MODE. Edit it on vi, then make sure that the file has only one ^M.

In the following image, the file controlm has only one Windows newline.

Viewing a file in vi with one Windows Newline character

Then, use the following command line

egrep -l -fcontrolm Badfile1 Badfile2 Badfile3

This line will show the name of the offending files.