📄 ch27_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 27] 27.13 More grep-like Programs Written in Perl </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:30Z"><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_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "><LINKREL="next"HREF="ch27_14.htm"TITLE="27.14 Compound Searches "></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_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.12 Make Custom grep Commands (etc.) with perl "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_14.htm"TITLE="27.14 Compound Searches "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.14 Compound Searches "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-8012">27.13 More grep-like Programs Written in Perl </A></H2><PCLASS="para">[Article<ACLASS="xref"HREF="ch27_12.htm"TITLE="Make Custom grep Commands (etc.) with perl ">27.12</A>gave a sense of how easy it might be to write custom search programsin Perl...but until you learn the language, you can't rip these offwhenever you find yourself in need. This article describes a fewmore custom greps written in Perl. It doesn't show the scriptsthemselves, just how to use them. If you like them, they are on thedisc and the online archive. -TOR ] </P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="tgrep">tgrep</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-30589"></A>The <EMCLASS="emphasis">tgrep</EM> program <EMCLASS="emphasis">grep</EM>s only those files containing textualdata. It's useful in a directory that has mixed binary and textualfiles, when the filenames aren't a sufficient clue to the nature of thefile. <EMCLASS="emphasis">tgrep</EM> has one option, <EMCLASS="emphasis">-l</EM>, which causes it to listthe files containing the pattern rather than listing the linescontaining the pattern.</TD></TR></TABLE><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="pipegrep">pipegrep</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-30597"></A>The <EMCLASS="emphasis">pipegrep</EM> program <EMCLASS="emphasis">grep</EM>s the output of a series of commands.The difficulty with doing this using the normal <EMCLASS="emphasis">grep</EM> program isthat you lose track of which file was being processed. This programprints out the command it was executing at the time, including thefilename. The command, which is a single argument, will be executedonce for each file in the list. If you give the string <CODECLASS="literal">{}</CODE>anywhere in the command, the filename will be substituted at thatpoint. Otherwise the filename will be added on tothe end of thecommand. This program has one option, <EMCLASS="emphasis">-l</EM>, which causes it tolist the files containing the pattern.For example [<EMCLASS="emphasis">nm</EM> is a programmers' utility that prints symbol namelists-<EMCLASS="emphasis">JP</EM> ]:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cd /usr/lib</B></CODE>$ <CODECLASS="userinput"><B>pipegrep 'sys_nerr' nm lib*.a</B></CODE>nm /usr/lib/libX11.a |: U _sys_nerrnm /usr/lib/libXaw.a |: U _sys_nerrnm /usr/lib/libXaw.a |: U _sys_nerrnm /usr/lib/libc.a |: U _sys_nerr . . .</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="cgrep">cgrep</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-30612"></A>The <EMCLASS="emphasis">cgrep</EM> program <EMCLASS="emphasis">grep</EM>s for a pattern in the specified files, andprints out that line with several lines of surrounding context.This context grep scriptlets you specify how many lines of context you want if youwant more or less than the default. For example:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cgrep -3 </B></CODE><CODECLASS="replaceable"><I>pattern files</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">would give you three lines of context above and below the matchingline. Each occurrence is separated from the next by a shorthorizontal line (<CODECLASS="literal">-----</CODE>).<ACLASS="indexterm"NAME="AUTOID-30623"></A><ACLASS="indexterm"NAME="AUTOID-30624"></A><ACLASS="indexterm"NAME="AUTOID-30625"></A><ACLASS="indexterm"NAME="AUTOID-30626"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">LW</SPAN>, <SPANCLASS="authorinitials">RS</SPAN>, <SPANCLASS="authorinitials">TC</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Programming Perl</CITE></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_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.12 Make Custom grep Commands (etc.) with perl "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_14.htm"TITLE="27.14 Compound Searches "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.14 Compound Searches "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">27.12 Make Custom grep Commands (etc.) with perl </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.14 Compound Searches </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 & 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 + -