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.
Enjoy!



October 26th, 2009 at 11:01 am
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