⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch28_01.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 28] Comparing Files</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:41Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="part04.htm"TITLE="IV. Looking Inside Files "><LINKREL="prev"HREF="ch27_20.htm"TITLE="27.20 A Highlighting grep "><LINKREL="next"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch27_20.htm"TITLE="27.20 A Highlighting grep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.20 A Highlighting grep "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 28</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.2 Comparing Three Different Versions with diff3 "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-28">28. Comparing Files</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-2680"TITLE="28.1 Checking Differences with diff ">Checking Differences with diff </A><BR><ACLASS="sect1"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 ">Comparing Three Different Versions with diff3 </A><BR><ACLASS="sect1"HREF="ch28_03.htm"TITLE="28.3 Context diffs ">Context diffs </A><BR><ACLASS="sect1"HREF="ch28_04.htm"TITLE="28.4 Side-by-Side diffs: sdiff ">Side-by-Side diffs: sdiff </A><BR><ACLASS="sect1"HREF="ch28_05.htm"TITLE="28.5 Comparing Files Alongside One Another ">Comparing Files Alongside One Another </A><BR><ACLASS="sect1"HREF="ch28_06.htm"TITLE="28.6 Choosing Sides with sdiff ">Choosing Sides with sdiff </A><BR><ACLASS="sect1"HREF="ch28_07.htm"TITLE="28.7 diff for Very Long Files: bdiff ">diff for Very Long Files: bdiff </A><BR><ACLASS="sect1"HREF="ch28_08.htm"TITLE="28.8 More Friendly diff Output ">More Friendly diff Output </A><BR><ACLASS="sect1"HREF="ch28_09.htm"TITLE="28.9 ex Scripts Built by diff ">ex Scripts Built by diff </A><BR><ACLASS="sect1"HREF="ch28_10.htm"TITLE="28.10 Problems with diff and Tabstops ">Problems with diff and Tabstops </A><BR><ACLASS="sect1"HREF="ch28_11.htm"TITLE="28.11 cmp and diff ">cmp and diff </A><BR><ACLASS="sect1"HREF="ch28_12.htm"TITLE="28.12 Comparing Two Files with comm ">Comparing Two Files with comm </A><BR><ACLASS="sect1"HREF="ch28_13.htm"TITLE="28.13 make Isn't Just for Programmers! ">make Isn't Just for Programmers! </A><BR><ACLASS="sect1"HREF="ch28_14.htm"TITLE="28.14 Even More Uses for make ">Even More Uses for make </A><BR><ACLASS="sect1"HREF="ch28_15.htm"TITLE="28.15 Show Changes in a troff File with diffmk ">Show Changes in a troff File with diffmk </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2680">28.1 Checking Differences with diff </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="diff">diff</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-30971"></A><ACLASS="indexterm"NAME="AUTOID-30975"></A><ACLASS="indexterm"NAME="AUTOID-30979"></A>The <EMCLASS="emphasis">diff</EM> command displays different versions of lines that are found whencomparing two files.(There's also a GNU version on the CD-ROM.)It prints a message that uses <EMCLASS="emphasis">ed</EM>-like notation (<EMCLASS="emphasis">a</EM> for append,<EMCLASS="emphasis">c</EM> for change, and <EMCLASS="emphasis">d</EM> for delete) to describehow a set of lines has changed.This is followed by the lines themselves.The <CODECLASS="literal">&lt;</CODE> character precedes lines from the first file and <CODECLASS="literal">&gt;</CODE>precedes lines from the second file.</TD></TR></TABLE><PCLASS="para">Let's create an example to explain the output produced by <EMCLASS="emphasis">diff</EM>. Look at the contents of three sample files:</P><TABLECLASS="informaltable"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">test1</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">test2</TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1">test3</TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">apples</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">apples</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">oranges</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">oranges</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">oranges</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">walnuts</TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">walnuts</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">grapes</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">chestnuts</TD></TR></TBODY></TABLE><PCLASS="para">When you run <EMCLASS="emphasis">diff</EM> on <EMCLASS="emphasis">test1</EM> and <EMCLASS="emphasis">test2</EM>, the following outputis produced:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff test1 test2</B></CODE>3c3&lt; walnuts--&gt; grapes</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-31019"></A>The <EMCLASS="emphasis">diff</EM> command displays the only line that differs between the two files.To understand the report, remember that <EMCLASS="emphasis">diff</EM> is prescriptive, describingwhat changesneed to be made to the first file to make it the same as the second file.This report specifies that only the third line is affected,exchanging <CODECLASS="literal">walnuts</CODE> for <CODECLASS="literal">grapes</CODE>.This is more apparent if you use the <EMCLASS="emphasis">-e</EM> option, which produces anediting script that can be submitted to <EMCLASS="emphasis">ed</EM>, the UNIX line editor.(You must<SPANCLASS="link">redirect standard output (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>to capture this script in a file.)</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff -e test1 test2</B></CODE>3cgrapes.</PRE></BLOCKQUOTE></P><PCLASS="para">This script, if run on <EMCLASS="emphasis">test1</EM>, will bring <EMCLASS="emphasis">test1</EM> into agreement with<EMCLASS="emphasis">test2</EM>.(Article<ACLASS="xref"HREF="ch28_09.htm"TITLE="ex Scripts Built by diff ">28.9</A>describes how to get <EMCLASS="emphasis">ed</EM> to execute this script.)</P><PCLASS="para">If you compare the first and third files, you find more differences:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff test1 test3</B></CODE>1dO&lt; apples3a3&gt; chestnuts</PRE></BLOCKQUOTE></P><PCLASS="para">To make <EMCLASS="emphasis">test1</EM> the same as <EMCLASS="emphasis">test3</EM>, you'd have to delete the first line(apples) and append the third line from <EMCLASS="emphasis">test3</EM> after the third linein <EMCLASS="emphasis">test1</EM>.Again, this can be seen more clearly in the editing script producedby the <EMCLASS="emphasis">-e</EM> option.Notice that the script specifies editing lines in reverse order;otherwise, changing the first line would alter all succeeding line numbers.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff -e test1 test3</B></CODE>3achestnuts.1d</PRE></BLOCKQUOTE></P><PCLASS="para">So what's this good for?Here's one example. </P><PCLASS="para">When working on a document, it is not an uncommon practice to make a copy ofa file and edit the copy rather than the original.This might be done, for example, if someone other than the writer isinputing edits from a written copy.The <EMCLASS="emphasis">diff</EM> command can be used to compare the two versions of a document.A writer could use it to proof an edited copy against the original.</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff brochure brochure.edits</B></CODE>49c43,44&lt; environment for program development and communications,--&gt; environment for multiprocessing, program development&gt; and communications, programmers56c51&lt; offering even more power and productivity for commericial--&gt; offering even more power and productivity for commercial76c69&lt; Languages such as FORTRAN, COBOL, Pascal, and C can be--&gt; Additional languages such as FORTRAN, COBOL, Pascal, and</PRE></BLOCKQUOTE></P><PCLASS="para">Using <EMCLASS="emphasis">diff</EM> in this manner is a simple way for a writer to examinechanges without reading the entire document.By redirecting <EMCLASS="emphasis">diff</EM> output to a file, you can keep arecord of changes made to any document.In fact, just that techniqueis used by <SPANCLASS="link">SCCS and RCS (<ACLASS="linkend"HREF="ch20_12.htm"TITLE="Protecting Files with SCCS or RCS ">20.12</A>)</SPAN>to manage multiple revisions of source code and documents.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from <CITECLASS="citetitle">UNIX Text Processing</CITE>, Hayden Books, 1987, Chapter 11</SPAN></P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch27_20.htm"TITLE="27.20 A Highlighting grep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.20 A Highlighting grep "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_02.htm"TITLE="28.2 Comparing Three Different Versions with diff3 "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.2 Comparing Three Different Versions with diff3 "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">27.20 A Highlighting grep </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">28.2 Comparing Three Different Versions with diff3 </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -