Get rid of Ctrl-M characters

carriage return

Image by holeymoon via Flickr

Have you found yourself opening an old text file in your shinny new Mac and strange characters such as ^M appear all over the place? Do not panic, nothing that this post would not help to fix.

Those strange characters come from the format in which different operating systems encode things like carriage returns at the end of line.  Some  applications won’t recognise the carriage returns and will display a file as a single line, interspersed with Ctrl-m characters.

In Mac OS X, the situation is more complicated given that it is a flavour of Unix itself. In some cases text files have carriage returns and in others they have new lines. For the most part, classic applications still require text files to have carriage returns, while the command-line Unix utilities require new lines (aka line feeds). Mac OS X-native applications are usually capable of interpreting both.

There are many ways to resolve the differences in format. There are some Unix command line utilities such  tr, awk, and Perl to do the conversion. From Mac OS X, each can be accessed from the Terminal application. In order to understand some of the syntax used below, it is important to mention that Unix character sequences to identify different types of “spaces”

  • r: CarriageReturn
  • n: New Line
  • t: Tab
  • v: VerticalTab
  • f: FormFeed
  • b: BackSpace

tr

The Unix program tr is used to translate between two sets of characters. Characters specified in one set are converted to the matching character in the second set. Thus, to convert the Ctrl-m of a Mac OS text file to the new line (Ctrl-j) of a Unix text file, at the Unix command line, enter:

  • tr ‘r’ ‘n’ < macfile.txt > unixfile.txt

Here, r and n are special escape sequences that tr interprets as Ctrl-m (a carriage return) and Ctrl-j (a new line), respectively. Thus, to convert a Unix text file to a Mac OS text file, enter:

  • tr ‘n’ ‘r’ < unixfile.txt > macfile.txt

awk

To use awk to convert a Mac OS file to Unix, at the Unix prompt, enter:

  • awk ‘{ gsub(“r”, “n”); print $0;}’ macfile.txt > unixfile.txt

To convert a Unix file to Mac OS using awk, at the command line, enter:

  • awk ‘{ gsub(“n, “r”); print $0;}’ unixfile.txt > macfile.txt

On some systems, the version of awk may be old and not include the function gsub. If so, try the same command, but replace awk with gawk or nawk.

Perl

To convert a Mac OS text file to a Unix text file using Perl, at the Unix shell prompt, enter:

  • perl -p -e ‘s/r/n/g’ < macfile.txt > unixfile.txt

To convert from a Unix text file to a Mac OS text file with Perl, at the Unix shell prompt, enter:

  • perl -p -e ‘s/n/r/g’ < unixfile.txt > macfile.txt

I hope this helps.

UPDATE – Thanks to D Asirvadem for some comments and corrections. He adds in regards to “In Mac OS X, the situation is more complicated given that it is a flavour of Unix itself”:

Yes and no. I would not put it that way. The problem only happens when you import a text file from an older variant of Unix (eg AIX) to a newer variant of Unix (eg. MacOS or Linux RHEL). It is no more complicated on MacOS.
Enhanced by Zemanta