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
line utilities tr
, awk, and Perl to
do the conversion. From Mac OS X, each can be accessed from the
Terminal application.
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 line feed
(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 line feed), respectively.
Thus, to convert a Unix text file to a Mac OS text file, enter:
tr '\n' '\r' < unixfile.txt > macfile.txt
Note: The escape sequences must be surrounded by single quotation marks for these commands to work.
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
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.
This is document agiz in the Knowledge Base.
Last modified on 2018-01-18 12:21:06.