Removing ^M characters in UNIX

I’m sure that this has been done to death on a number of fora, but I never know where to look – so I’ll post it here.

I recently needed to remove spurious ^M characters from files in UNIX, which had been caused by FTPing the file in the wrong mode.  There are a number of different ways that this can be done – here’s four of them:

1 – In vi, type :g/(ctrl-V)(ctrl-M)/s///

2 – In vi, type :1,$s/(ctrl-V)(ctrl-M)//

3 – In UNIX, run “tr -d ‘\r’ < oldfilename > newfilename

4 – In UNIX run “tr -d ‘\015\032’ < oldfilename > newfilename

The last one will also remove the DOS EOF character.

There are plenty of different ways to do it, but I tend to use numbers 1 and 3 of these.


1 thought on “Removing ^M characters in UNIX

  1. Hi Matt
    Like the blog, some bits are quite useful.
    On Solaris, if I forget to ascii transfer a text file, (or in the case of sftp where you can’t) I use dos2unix (or unix2dos) from the shell. Think the parameters are just infile and outfile. Quite handy, and one doesn’t have to mess about with regular expressions.
    Cheers – Tony

Comments are closed.

By continuing to use the site, you agree to the use of cookies. more information

In common with almost all professionally run websites, this website logs the IP address of each visitor in order to keep it running reliably. This is also essential for protecting the website and its visitors from malicious attacks, including infection with malware.

This website provides information as a service to visitors such as yourself, and to do this reliably and efficiently, it sometimes places small amounts of information on your computer or device (e.g. mobile phone). This includes small files known as cookies. The cookies stored by this website cannot be used to identify you personally.

We use cookies to understand what pages and information visitors find useful, and to detect problems such as broken links, or pages which are taking a long time to load.

We sometimes use cookies to remember a choice you make on one page, when you have moved to another page if that information can be used to make the website work better. For example:
- avoiding the need to ask for the same information several times during a session (e.g. when filling in forms), or
- remembering that you have logged in, so that you don’t have to re-enter your username and password on every page.

You can prevent the setting of cookies by adjusting the settings on your browser (see your browser Help for how to do this). Be aware that disabling cookies will affect the functionality of this and many other websites that you visit.