ARCHIVED: How do I convert between Unix and Mac OS or Mac OS X text files?
Note: This document is primarily concerned with older systems and text files, and does not factor in Unicode or word processed files.
Traditionally, Unix and Mac OS differ in the
format in which they store text files. Mac OS places a carriage
return character at the end of each line of a text file, but Unix uses
a line feed character. Some Unix applications won't recognize the
carriage returns added by Mac OS, and will display a file as a single
line, interspersed with
Ctrl-m characters. This appears
on the screen as
^M. Similarly, some Mac OS applications
need to see carriage return characters at the ends of lines, and may
treat Unix-format files as one long line.
In Mac OS X, the situation is more complicated. Because Mac OS X is a meld of Unix and the older Mac OS, in some cases text files have carriage returns and in others they have line feeds. For the most part, classic applications still require text files to have carriage returns, while the command-line Unix utilities require line feeds. Mac OS X-native applications are usually capable of interpreting both.
There are many ways to resolve the differences in format. In this
document you will find instructions on how to use the Unix command
tr, awk, and Perl to
do the conversion. From Mac OS X, each can be accessed from the
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
Ctrl-m of a Mac OS text file to the line feed
Ctrl-j) of a Unix text file, at the Unix command line,
\n are special escape
tr interprets as
carriage return) and
Ctrl-j (a line feed), respectively.
Thus, to convert a Unix text file to a Mac OS text file, enter:
Note: The escape sequences must be surrounded by single quotation marks for these commands to work.
awk to convert a Mac OS file to Unix, at the
Unix prompt, enter:
To convert a Unix file to Mac OS using
awk, at the
command line, enter:
On some systems, the version of
awk may be old and not
include the function
gsub. If so, try the same command,
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
Note: You must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside the quotation marks. At Indiana University, Perl is installed on all UITS shared central Unix systems.
Last modified on February 17, 2011.