« Newcastle in July | Main | Perfect »

removing ^m from files

I work a lot with content uploaded and authored on windows machines and I often get files littered with those ^M's.
I used to use this:
tr -d '\015' < in-file > out-file
but this works nicely in vi:
In command mode:

1,$s/^M//g

The ^M in the substitution above is typed as Ctrl-V Ctrl M.

Comments

Andy,

#!/usr/local/bin/perl
# ctrlm.pl infile > outfile
while (>) {
   $_ =~ s/\cM\n/\n/g;
   print $_;
}

works a treat as well.

I saw that mentioned once before, but I never seem to have it on any box I am on.

If you really want a perl version then:

perl -pi -e 's/\015\012/\n/g' filename

Your VI editor can do this for you. In Vi command mode, to convert a file to unix format, type :-

:set ff=unix

and save the file.

To convert a file to DOS format, type :-

:set ff=dos

My god there is a lot of clever people that read this blog.
I must post more nerdy stuff, so I can tap into all these ideas.
TIMTOWTDI

% does an operation on every line in a file. In this case, %s/^M//g would do what you want, saving a few keystrokes (not that it matters much, just a bit handier).

Post a comment

The following bizarre question is in place to try to stop Comment Spam.