Compare two Mac-formatted text files

Apr 16, '02 09:57:54AM

Contributed by: hayne

The standard UNIX (command-line) utility 'diff' compares two text files, showing you the lines which differ between the two. It works well for all UNIX text files, including most of those you will encounter in OS X (e.g. the plist files holding preference information).

But it isn't useful for comparing files which use the traditional Mac end-of-line marker ("\r") since the 'diff' utility, like most UNIX tools, expects lines to be ended with "\n". OS X itself and most OS X applications use "\n" as the end-of-line marker, but some applications which exist in both OS 9 and OS X versions use the traditional Mac "\r" end-of-line marker for their files. An example is iMovie - its project files are editable text but they use "\r" and hence cannot be usefully compared with 'diff'.

All of the above was a long-winded motivation for the following shell script (for use in the Terminal) which provides a 'diff' comparison of two traditional Mac ("\r") files. Read the rest of the article for the script...

I haven't spent the time to polish up this script so I'm sure that some readers will contribute improved versions; here's the current incarnation:

#!/bin/csh -f

# mdiff:
# Provides the equivalent of 'diff' for comparing files
# that use the traditional Macintosh line ending: \r

# This script expects two filename arguments on the command line
# Note: the quotes are necessary since filenames might have spaces in them
# As a future enhancement, should use the last two args
# since then user could supply options to diff
set mfile1 = "$1"
set mfile2 = "$2"

# We create temporary files ufile1 & ufile2 with \r changed to \n
set ufile1 = "/tmp/mdiff$$.1"
set ufile2 = "/tmp/mdiff$$.2"
tr "\r" "\n" < "$mfile1" > $ufile1
tr "\r" "\n" < "$mfile2" > $ufile2

# Do the diff
/usr/bin/diff $ufile1 $ufile2

# Remove the temp files
/bin/rm $ufile1 $ufile2
As usual, to run the script you need to save it in a file (mdiff, for example) and make that file executable (chmod 755 mdiff) and put the file in a directory in your path.

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20020416095754508