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

📄 ch28_09.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 28] 28.9 ex Scripts Built by diff </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:46Z"><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="ch28_01.htm"TITLE="28. Comparing Files"><LINKREL="prev"HREF="ch28_08.htm"TITLE="28.8 More Friendly diff Output "><LINKREL="next"HREF="ch28_10.htm"TITLE="28.10 Problems with diff and Tabstops "></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="ch28_08.htm"TITLE="28.8 More Friendly diff Output "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.8 More Friendly diff Output "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 28<BR>Comparing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_10.htm"TITLE="28.10 Problems with diff and Tabstops "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.10 Problems with diff and Tabstops "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2820">28.9 ex Scripts Built by diff </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-31352"></A><ACLASS="indexterm"NAME="AUTOID-31355"></A><ACLASS="indexterm"NAME="AUTOID-31358"></A><ACLASS="indexterm"NAME="UPT-ART-2820-IX-EDITING-WITH-FILE-COMPARISONS"></A><ACLASS="indexterm"NAME="UPT-ART-2820-IX-COMPARING-EDITING-AND"></A>The <EMCLASS="emphasis">-e</EM> option of <EMCLASS="emphasis">diff</EM> produces an editing scriptusable with either<SPANCLASS="link"><EMCLASS="emphasis">ex</EM> (<ACLASS="linkend"HREF="ch33_04.htm"TITLE="Useful ex Commands ">33.4</A>)</SPAN>or <EMCLASS="emphasis">ed</EM>, instead of the usual output.This script consists of a sequence of <CODECLASS="literal">a</CODE> (add), <CODECLASS="literal">c</CODE>(change), and <CODECLASS="literal">d</CODE> (delete) commands necessary to re-create <EMCLASS="emphasis">file2</EM> from <EMCLASS="emphasis">file1</EM> (the first and second files specified on the <EMCLASS="emphasis">diff</EM> command line).</P><PCLASS="para">Obviously there is no need to completely re-create the first file fromthe second, because you could do that easily with <EMCLASS="emphasis">cp</EM>.However, by editing the script produced by <EMCLASS="emphasis">diff</EM>, you can come upwith some desired combination of the two versions.</P><PCLASS="para">It might take you a moment to think of a case in which you might haveuse for this feature.Consider this one: two people have unknowingly made edits todifferent copies of a file, and you need the two versions merged.(This can happen especially easily in a networked environment, inwhich people copy files between machines.Poor coordination can easily result in this kind of problem.)</P><PCLASS="para">To make this situation concrete, let's take a look at two versions ofthe same paragraph, that we want to combine:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><EMCLASS="emphasis">Version 1:</EM>The Book of Kells, now one of the treasures of the TrinityCollege Library in Dublin, was found in the ancientmonastery at Ceannanus Mor, now called Kells. It is abeautifully illustrated manuscript of the Latin Gospels,and also contains notes on local history. It was written in the eighth century. The manuscript is generally regarded as the finest exampleof Celtic illumination.<EMCLASS="emphasis">Version 2:</EM>The Book of Kells was found in the ancientmonastery at Ceannanus Mor, now called Kells. It is abeautifully illustrated manuscript of the Latin Gospels,and also contains notes on local history. It is believed to have been written in the eighth century. The manuscript is generally regarded as the finest exampleof Celtic illumination.</PRE></BLOCKQUOTE></P><PCLASS="para">As you can see, there is one additional phrase in each of the two files.We can merge them into one file that incorporates bothedits.Typing:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>diff -e version1 version2 &gt; exscript</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will yield the following output in the file <EMCLASS="emphasis">exscript</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">6cIt is believed to have been written in the eighth century. .1,2cThe Book of Kells was found in the ancient.</PRE></BLOCKQUOTE></P><PCLASS="para">You'll notice that the script appears in reverse order, with thechanges later in the file appearing first.This is essential whenever you're making changes based on linenumbers;  otherwise, changes made earlier in the file may change thenumbering, rendering the later parts of the script ineffective.You'll also notice that, as mentioned, this script will simplyrecreate <EMCLASS="emphasis">version2</EM>, which is not what we want.We want the change to line 5, but not the change to lines 1 and 2.We want to edit the script so that it looks like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">6cIt is believed to have been written in the eighth century. .w</PRE></BLOCKQUOTE></P><PCLASS="para">(Notice that we had to add the <CODECLASS="literal">w</CODE> command to write the resultsof the edit back into the file.)Now we can type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>ex - version1 &lt; exscript</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">to get the resulting merged file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">The Book of Kells, now one of the treasures of the TrinityCollege Library in Dublin, was found in the ancientmonastery at Ceannanus Mor, now called Kells. It is abeautifully illustrated manuscript of the Latin Gospels,and also contains notes on local history. It is believed to have been written in the eighth century. The manuscript is generally regarded as the finest exampleof Celtic illumination.</PRE></BLOCKQUOTE></P><PCLASS="para">Using <EMCLASS="emphasis">diff</EM> like this can get confusing, especially when thereare many changes.It is easy to get the direction of changes confused or to make the wrong edits.Just remember to do the following:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">Specify the file that is closest in content to your eventual target asthe first file on the <EMCLASS="emphasis">diff</EM> command line.This will minimize the size of the editing script that is produced.</P></LI><LICLASS="listitem"><PCLASS="para">After you have corrected the editing script so that it makes only thechanges that you want, apply it to that same file (the first file).</P></LI></UL><PCLASS="para">Nonetheless, because there is so much room for error, it is better notto have your script write the changes back directly into one of yoursource files.Instead of adding a <CODECLASS="literal">w</CODE> command at the end of the script, addthe command <CODECLASS="literal">%p</CODE> (or <CODECLASS="literal">1,$p</CODE>) to write the results to <SPANCLASS="link">standard output (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>.This is almost always preferable when you are using a complex editingscript.</P><PCLASS="para">&#13;If we use this command in the editing script, the command line toactually make the edits would look like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>ex - version1 &lt; exscript &gt; version3</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Writers often find themselves making extensive changes and thenwishing they could go back and recover some part of an earlierversion.Obviously, frequent backups will help.However, if backup storage space is at a premium, it is possibleto save only some older version of a fileand then keep incremental <EMCLASS="emphasis">diff</EM> <EMCLASS="emphasis">-e</EM> scripts to mark thedifferences between each successive version.(As it turns out, this is what version control systems like<SPANCLASS="link">SCCS and RCS (<ACLASS="linkend"HREF="ch20_12.htm"TITLE="Protecting Files with SCCS or RCS ">20.12</A>)</SPAN>do.)</P><PCLASS="para">To apply multiple scripts to a single file, you can simply pipe themto <EMCLASS="emphasis">ex</EM> rather than redirecting input:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch25_02.htm"TITLE="25.2 Four Ways to Skin a cat ">cat</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cat script1 script2 script3 | ex - oldfile</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">But wait!How do you get your <CODECLASS="literal">w</CODE> (or <CODECLASS="literal">%p</CODE>) command into thepipeline?You could edit the last script to include one of these commands.But there's another trick that we ought to look at because itillustrates another useful feature of the shell that many people areunaware of.If you enclose a semicolon-separated list of commands in<SPANCLASS="link">parentheses (<ACLASS="linkend"HREF="ch13_07.htm"TITLE="The () Subshell Operators ">13.7</A>)</SPAN>,the standard output of all of the commands are combined, and can beredirected together.The immediate application is that, if you type:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments ">echo</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cat script1 script2 script3; echo '%p' | ex - oldfile</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">the results of the <EMCLASS="emphasis">cat</EM> command will be sent, as usual, tostandard output, and only the results of <EMCLASS="emphasis">echo</EM> will be piped to<EMCLASS="emphasis">ex</EM>.But if you type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>(cat script1 script2 script3; echo '%p') | ex - oldfile</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">the output of the entire sequence will make it into the pipeline,which is what we want.</P><ACLASS="indexterm"NAME="AUTOID-31450"></A><ACLASS="indexterm"NAME="AUTOID-31451"></A><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN> <SPANCLASS="bibliomisc">from <CITECLASS="citetitle">UNIX Text Processing</CITE>, Hayden Books, 1987, Chapter 12</SPAN></P></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="ch28_08.htm"TITLE="28.8 More Friendly diff Output "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.8 More Friendly diff Output "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_10.htm"TITLE="28.10 Problems with diff and Tabstops "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.10 Problems with diff and Tabstops "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">28.8 More Friendly diff Output </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.10 Problems with diff and Tabstops </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 + -