Friday, 15 August 2014

"Sixty ways to leave your lover" - or, in this case, sixty ways to remove CR from a file using vi

So I had a need to remove the ^M character from a text file, created using Notepad, in vi.

Last time around, I simply paged through the file ( it was ~20 lines long ) and removed the character by hand.

This time, for a longer file, I thought "Surely there's a better way?" and turned to Google.

Guess what, there are MANY ways to do this.

Here's a few: -

Sometimes DOS files end up on unix systems without being converted. Files will then have those nasty ^M character at the line ending, which prevents some applications to work properly.

The reason for is is DOS to use CRLF (carriage return + line feed) for line endings while unix uses LF (line feed) only.

If only few files need to be changed, vi/vim is the tool of choice.

After opening up the file, enter command mode to run this macro:


To get the ^M do not actually enter it as is. Insert it by typing the CTRL-V CTRL-M sequence instead.


You could also do the following from the command line:

strings oldfile>>newfile

Not as eloquent, but does the job.

For me, I used the strings method for one file and, for the other, I used a third approach: -

vi filename.txt
:1,$ s/^M//g

The trick, as pointed out above, is that you need to actually press CTRL-V CTRL-M to get the ^M sequence; you can't simply type ^ and then M :-)


No comments: