Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Compare two Mac-formatted text files UNIX
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.
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[22,795 views]  

Compare two Mac-formatted text files | 3 comments | Create New Account
Click here to return to the 'Compare two Mac-formatted text files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Don't use csh for scripting!
Authored by: a1291762 on Apr 16, '02 09:43:09PM

Go and look at http://www.gregor.com/dgregor/csh_whynot.html for why you shouldn't be using csh for scripting.

Here's my version (using bourne shell programming of course) that's even more fancy. (Replace <BACKSLASH> with a backslash)

#!/bin/sh
# mdiff:
# Provides the equivalent of 'diff' for comparing files
# that use the traditional Macintosh line ending: r

# So people don't make weird versions of printf, diff, rm etc. run
PATH=/bin:/usr/bin:$PATH

# Print the usage and exit
usage()
{
diff -h
printf "Usage: %s [ -bcefhintwlrs ] file1 file2" `basename $0`
exit 2;;
}

# Cleanup when exiting
cleanup()
{
rm "$ufile1" "$ufile2"
exit;
}

# pass on arguments to diff except for h (print help)
while getopts bcefhintwlrs arg; do
case $arg in
<BACKSLASH>?) usage;;
h) usage;;
?) args="$args -$arg";;
esac
done
shift `expr $OPTIND - 1`

# We need 2 arguments
[ $# -ne 2 ] && usage
mfile1="$1"
mfile2="$2"

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

# Remove the temporary files when exiting (via Ctrl+C or normal exit or kill etc.)
trap "cleanup" 0

# Do the diff
diff $args "$ufile1" "$ufile2"



[ Reply to This | # ]
FileMerge
Authored by: Anonymous on Apr 17, '02 05:24:51AM

You should also try FileMerge in the Developer tools. Its is a (great) GUI tool to compare and merge files... and it works with any type of file.



[ Reply to This | # ]
FileMerge needs correct end of line characters
Authored by: Ian Downie on Aug 12, '03 07:26:16AM

Filemerge is indeed better than diff because it displays a nice side by side comparison of the two files with a visual display of the differences, and then allows you to merge the files if you want. However, I found that it crashes if you use it on files with Mac end of line characters - I needed to compare two database files produced by Gene, a family history application which I have to run in Classic.

If you have files like that, you need to use tr to convert the end of line characters before you can run Filemerge on them. It's not really worth a shell script; just go to the terminal and navigate to the directory in which your files are saved, then enter:

tr "\r" "\n" <filename >tempfilename

where filename is the name of your first file and tempfilename is a name of your choice for the converted file.

Do the same thing again for the second file, then run FileMerge (it's in Developer/Applications if you have installed Developer Tools), choosing your two temporary files to compare.

After merging the files, replace the original end of line characters:

tr "\n" "\r" <mergefilename >newmergefilename

where mergefilename is the name you chose when saving the merged file in Filemerge and newmergefilename is your chosen name for the final, merged file with the original end of line characters, ready to view in your Classic application. You will probably also have to change the filetype and creator depending on whether your application can recognise the merged file.



[ Reply to This | # ]