Providing practical examples since 1998

Things look quiet here. But I've been doing a lot of blogging at
dan.langille.org because I prefer WordPress now.
Not all my posts there are FreeBSD related.
I am in the midst of migrating The FreeBSD Diary over to WordPress
(and you can read about that here).
Once the migration is completed, I'll move the FreeBSD posts into the
new FreeBSD Diary website.

That "^M" is control-M, which is a carriage return, and is not
needed in Unix file systems. To remove this extra character, run the following
script:

perl -pi -e "s:^V^M::g" <filenames>

where ^V is actually control-V and ^M is actually control-M (you must type these
yourself, don't just copy and paste this command). Control-V will not be displayed
on your screen.

<filenames> is the list of files to be converted.

NOTE: I'm told that if you use ASCII mode to transfer your files, this problem won't
occur.

Other options

I like people writing in with options. Here are two other ways to
get rid of the ^M characters:

Marc Silver writes that this will work:

cat <filename1> | tr -d "^V^M" > <newfile>

Remember to actually type control-V then control-M. Don't just copy and paste the
above.

Trevor R.H. Clarke gave this example:

sed -e "s/^V^M//" <filename> > <output filename>

And remember to actually type control-V then control-M. Don't just copy and paste
the above.

Mario Sergio Fujikawa Ferreira writes of this vi solution.

hit the ESC key

:%s/^V^M//

remember the colon ( : ) at the start of step two.

Marius Strom mentions this port:

cd /usr/ports/converters/unix2dos make && make install

This port will also install dos2unix, which is useful for removing the
control-M characters.

Oliver Crow reports this:

col <infile >outfile

[ed. note: make sure infile and outfile are not the same file or
you'll lose the contents]

KeMpKeS reports this:

Here's one you might want to add to the removing ^M from files page:
open the file in pico, make a trivial change, then change it back (like
add a space then delete it), then try to quit pico. When it asks you if
you want to save changes, say yes. That''ll do it.