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

📄 ch27_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 27] 27.10 Search RCS Files with rcsgrep </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:25Z"><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="ch27_01.htm"TITLE="27. Searching Through Files"><LINKREL="prev"HREF="ch27_09.htm"TITLE="27.9 New greps Are Much Faster "><LINKREL="next"HREF="ch27_11.htm"TITLE="27.11 A Multiline Context grep Using sed "></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_09.htm"TITLE="27.9 New greps Are Much Faster "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.9 New greps Are Much Faster "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 27<BR>Searching Through Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch27_11.htm"TITLE="27.11 A Multiline Context grep Using sed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.11 A Multiline Context grep Using sed "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7969">27.10 Search RCS Files with rcsgrep </A></H2><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="rcsgrep">rcsgrep</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-30390"></A><ACLASS="indexterm"NAME="AUTOID-30393"></A><ACLASS="indexterm"NAME="AUTOID-30397"></A><ACLASS="indexterm"NAME="AUTOID-30400"></A>Storing multiple versions of a file in<SPANCLASS="link">RCS (<ACLASS="linkend"HREF="ch20_14.htm"TITLE="RCS Basics ">20.14</A>)</SPAN>saves space.How can you search a lot of those files at once?You could check out all the files, then run <EMCLASS="emphasis">grep</EM>-but you'll have toremove the files after you're done searching.Or, you could search the RCS files themselves with a command like<CODECLASS="literal">grep</CODE>&nbsp;<CODECLASS="literal">foo</CODE>&nbsp;<CODECLASS="literal">RCS/*,v</CODE>-but that can show you garbagelines from previous revisions, log messages, and other text that isn'tin the latest revision of your file.This article has two ways to solve that problem.</TD></TR></TABLE><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7969-SECT-1.1">27.10.1 rcsgrep, rcsegrep, rcsfgrep </A></H3><PCLASS="para">The <EMCLASS="emphasis">rcsgrep</EM> script&nbsp;- and two links to it named <EMCLASS="emphasis">rcsegrep</EM> and<EMCLASS="emphasis">rcsfgrep</EM>-run <EMCLASS="emphasis">grep</EM>,<SPANCLASS="link"><EMCLASS="emphasis">egrep</EM> (<ACLASS="linkend"HREF="ch27_05.htm"TITLE="Extended Searching for Text with egrep ">27.5</A>)</SPAN>,and<SPANCLASS="link"><EMCLASS="emphasis">fgrep</EM> (<ACLASS="linkend"HREF="ch27_06.htm"TITLE="Fast grep Isn't ">27.6</A>)</SPAN>on allfiles in the RCS directory.(You can also choose the files to search.)</P><PCLASS="para">The script tests its name to decide whether to act like<EMCLASS="emphasis">grep</EM>, <EMCLASS="emphasis">egrep</EM>, or <EMCLASS="emphasis">fgrep</EM>.Then it checks out each file and pipes it to the version of grep you chose.The output looks just like <EMCLASS="emphasis">grep</EM>'s&nbsp;- although, by default, you'llalso see the messages from the <EMCLASS="emphasis">co</EM> command (the <EMCLASS="emphasis">-s</EM> optionsilences those messages).</P><PCLASS="para">By default, <EMCLASS="emphasis">rcsgrep</EM> searches the latest revision of every file.With the <EMCLASS="emphasis">-a</EM> option, <EMCLASS="emphasis">rcsgrep</EM> will search all revisions ofevery file, from first to last.This is very handy when you're trying to see what was changed in aparticular place&nbsp;- and to find which revision(s) have some text thatwas deleted some time ago.(<EMCLASS="emphasis">rcsgrep</EM> uses<SPANCLASS="link"><EMCLASS="emphasis">rcsrevs</EM> (<ACLASS="linkend"HREF="ch20_15.htm"TITLE="List RCS Revision Numbers with rcsrevs ">20.15</A>)</SPAN>to implement <EMCLASS="emphasis">-a</EM>.)</P><PCLASS="para">Some <EMCLASS="emphasis">grep</EM> options need special handling to work right in the script:<EMCLASS="emphasis">-e</EM>, <EMCLASS="emphasis">-f</EM>, and <EMCLASS="emphasis">-l</EM>.(For instance, <EMCLASS="emphasis">-e</EM> and <EMCLASS="emphasis">-f</EM> have an argument after them.The script has to pass both the option and its argument.)The script passes any other options you type to the <EMCLASS="emphasis">grep</EM> command.Your <EMCLASS="emphasis">grep</EM> versions may have some other options that need special handling,too.Just edit the script to handle them.</P><PCLASS="para">You can install this script from the CD-ROMor from the<SPANCLASS="link">online archive (<ACLASS="linkend"HREF="ch52_07.htm"TITLE="Other Ways to Get the Software ">52.7</A>)</SPAN>.If you get it from the archive,ask tar to install <EMCLASS="emphasis">rcsgrep</EM>, its two other links <EMCLASS="emphasis">rcsegrep</EM>and <EMCLASS="emphasis">rcsfgrep</EM>, as well as <EMCLASS="emphasis">rcsrevs</EM>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7969-SECT-1.2">27.10.2 rcsegrep.fast </A></H3><PCLASS="para">To search an RCS file, <EMCLASS="emphasis">rcsgrep</EM> and its cousins run several UNIXprocesses: <EMCLASS="emphasis">co</EM>, <EMCLASS="emphasis">grep</EM>, <EMCLASS="emphasis">sed</EM> and others.Each process takes time to start and run.If your directory has hundreds of RCS files (like our directory forthis book does), searching the whole thing can take a lot of time.I could have cut the number of processes by rewriting <EMCLASS="emphasis">rcsgrep</EM>in Perl; Perl has the functionality of <EMCLASS="emphasis">grep</EM>, <EMCLASS="emphasis">sed</EM> andothers built in, so all it would need to do is run hundreds of<EMCLASS="emphasis">co</EM> processes...which would still make it too slow.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="rcsegrep.fast">rcsegrep.fast</A><BR></TH><TDVALIGN="TOP">The solution I came up with was to do everything in (basically)one process: a<SPANCLASS="link"><EMCLASS="emphasis">gawk</EM> (<ACLASS="linkend"HREF="ch33_12.htm"TITLE="Versions of awk ">33.12</A>)</SPAN>script.Instead of using the RCS<EMCLASS="emphasis">co</EM> command to extract each file's latest revision,the <EMCLASS="emphasis">rcsegrep.fast</EM> script reads each RCS file directly(The <EMCLASS="emphasis">rcsfile</EM>(5) manpage explains the format of an RCS file.)An RCS file contains the latest revision of its working file asplain text, with one difference: each <CODECLASS="literal">@</CODE> character is changedto <CODECLASS="literal">@@</CODE>.<EMCLASS="emphasis">rcsegrep.fast</EM> searches the RCS file until it finds thebeginning of the working file.Then it applies an <EMCLASS="emphasis">egrep</EM>-like regular expression to each line.Matching lines are written to standard output with the filename first;the <EMCLASS="emphasis">-n</EM> option gives a line number after the filename.</TD></TR></TABLE><PCLASS="para"><EMCLASS="emphasis">rcsegrep.fast</EM> is sort of a kludge because it's accessing RCSfiles without using RCS tools.There's a chance that it won't work on some versions of RCS, or thatI've made some other programming goof.But it's worked very well for us.It's much faster than <EMCLASS="emphasis">rcsgrep</EM> and friends.I'd recommend using <EMCLASS="emphasis">rcsegrep.fast</EM> when you need to search thelatest revisions of a lot of RCS files; otherwise, stick to the<EMCLASS="emphasis">rcsgrep</EM>s.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch27_09.htm"TITLE="27.9 New greps Are Much Faster "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.9 New greps Are Much Faster "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="ch27_11.htm"TITLE="27.11 A Multiline Context grep Using sed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.11 A Multiline Context grep Using sed "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">27.9 New greps Are Much Faster </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">27.11 A Multiline Context grep Using sed </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 + -