Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 28.1 Checking Differences with diff Chapter 28
Comparing Files
Next: 28.3 Context diffs
 

28.2 Comparing Three Different Versions with diff3

diff3
You can use the diff3 command to look at differences between three files. (There's also a GNU version on the CD-ROM.) Here are three sample files, repeated from article 28.1:
test1test2test3
applesapplesoranges
orangesorangeswalnuts
walnutsgrapeschestnuts

For each set of differences, diff3 displays a row of equal signs (====) followed by 1, 2, or 3, indicating which file is different; if no number is specified, then all three files differ. Then, using ed-like notation (28.1), the differences are described for each file.

$ diff3 test1 test2 test3
====3
1:1c
2:1c
  apples
3:0a
====
1:3c
  walnuts
2:3c
  grapes
3:2,3c
  walnuts
  chestnuts

With the output of diff3, it is easy to keep track of which file is which; however, the prescription given is a little harder to decipher. To bring these files into agreement, the first range of text (after ====3) shows that you would have to add apples at the beginning of the third file (3:0a). The second range tells you to change line 3 of the second file to line 3 of the first file; and change lines 2 and 3 of the third file, effectively dropping the last line.

The diff3 command also has a -e option for creating an editing script for ed. It doesn't work quite the way you might think. Basically, it creates a script for building the first file from the second and third files.

$ diff3 -e test1 test2 test3
3c
walnuts
chestnuts
.
1d
.
w
q

If you reverse the second and third files, a different script is produced:

$ diff3 -e test1 test3 test2
3c
grapes
.
w
q

As you might guess, this is basically the same output as doing a diff on the first and third files. (The only difference in the output is the result of a rather errant inconsistency between diff and diff3. The System V version of diff3 produces an ed script that ends with the commands that save the edited version of the file. The Berkeley diff3, and both versions of diff, require that you supply the w and q. We show the System V version of diff3 here.)

- DD from UNIX Text Processing, Hayden Books, 1987, Chapter 11


Previous: 28.1 Checking Differences with diff UNIX Power ToolsNext: 28.3 Context diffs
28.1 Checking Differences with diff Book Index28.3 Context diffs

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