Pages

Thursday, March 10, 2016

Newlines: DOS/Windows to UNIX

If you're in UNIX and happen to have a source file originally created in DOS/Windows, then you most definitely have to get rid of the extra \r (a.k.a. ^M) that DOS\Windows inserts on a text file in order to mark line breaks.

While in vim the extra ^M are easily noticed:

#ifndef ..._BASICS_HXX^M
#define ..._BASICS_HXX^M

^M
namespace ...
^M
{
^M
^M
//
^M
//
^M
//
^M
using byte = unsigned char;
^M
^M 
//^M
//
^M
//
^M
using capacity_type = unsigned long long int;
^M 
^M
} // namespace ...
^M 
^M
#endif // ..._BASICS_HXX
^M 
^M
~

...
~  
"basics.hxx" 20 lines, 224 characters

While still in vim simply issue the following command:

:%s/\r//g

Which means to globally (g) substitute all (%s) the \r for nothing, in other words, to remove all the \r. Just save the file and you're done. Naturally you'll find a dozen other ways to accomplish the same thing and even more efficiently by means of a myriad of alternative shell external commands. So, suit yourself :-)