UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 33.8 Batch Editing Gotcha: Editors Bomb on Big Files Chapter 33
Batch Editing
Next: 33.10 Quick Globals from the Command Line with qsubst
 

33.9 patch: Generalized Updating of Files that Differ

patch
Like all of Larry Wall's widely used programs (including perl (37.1), a software configuration script called Configure, and the rn news reader), patch betrays a whimsical intelligence of its own. Feed it any kind of diff listing (28.1) (not just an editing script produced with the -e option (28.9)). patch figures out what it needs to do to apply the diff, and updates the file, supplying all the while a breezy commentary on what it's doing:

% patch < testfile.diff
Hmm...  Looks like a normal diff to me...
File to patch: testfile
Patching file testfile using Plan A...
Hunk #1 succeeded at 2.
done

As Larry once noted, patch has done an awful lot to "change the culture of computing." Almost all free software is now updated by means of patches rather than complete new releases. patch is smart enough to discard any leading or trailing garbage (such as mail headers or signatures) so that a program source file can be updated by piping a mail message containing a diff listing between old and new versions directly to patch.

Here are a few of the other things patch is smart enough to do:

If you are a programmer, patch is worth studying just to see how much a program can do to anticipate errors, deal with fuzzy input, and in general "make the computer do the dirty work." But if you're a programmer, you doubtless already know about patch....

One last note: patch is so useful that it's been added to many UNIX systems. Check to see if your system has it before installing it from the CD-ROM. [I've seen buggy or crippled versions from software vendors, though. The one on the disc is worth comparing to yours. –JP]

- TOR


Previous: 33.8 Batch Editing Gotcha: Editors Bomb on Big Files UNIX Power ToolsNext: 33.10 Quick Globals from the Command Line with qsubst
33.8 Batch Editing Gotcha: Editors Bomb on Big Files Book Index33.10 Quick Globals from the Command Line with qsubst

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System