Comparing Two Documents

Problem

It is easy to compare two text files (see Using diff and patch). But what about documents produced by your suite of office applications? They are not stored as text, so how can you compare them? If you have two versions of the same document, and you need to know what the content changes are (if any) between the two versions, is there anything you can do besides printing them out and comparing page after page?

Solution

First, use an office suite that will let you save your documents in Open Document Format (ODF). This is the case for packages like OpenOffice.org while other commercial packages have promised to add support soon. Once you have your files in ODF, you can use a shell script to compare just the content of the files. We stress the word content here because the formatting differences are another issue, and it is (usually) the content that is the most important determinant of which version is new or more important to the end user.

Here is a bash script that can be used to compare two OpenOffice.org files, which are saved in ODF (but use the conventional suffix odt to indicate a text-oriented document, as opposed to a spreadsheet or a presentation file).

 1 #!/usr/bin/env bash 2 # cookbook filename: oodiff 3 # oodiff -- diff the CONTENTS of two OpenOffice.org files 4 # works only on .odt files 5 # 6 function usagexit () 7 { 8 echo "usage: $0 file1 file2" 9 echo "where both files must be .odt files" 10 exit $1 11 } >&2 12 13 # assure two readable ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.