📄 ch27_11.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 27] 27.11 A Multiline Context grep Using sed </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:28Z"><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_10.htm"TITLE="27.10 Search RCS Files with rcsgrep "><LINKREL="next"HREF="ch27_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "></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_10.htm"TITLE="27.10 Search RCS Files with rcsgrep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.10 Search RCS Files with rcsgrep "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_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.12 Make Custom grep Commands (etc.) with perl "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7955">27.11 A Multiline Context grep Using sed </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-30482"></A><ACLASS="indexterm"NAME="AUTOID-30485"></A><ACLASS="indexterm"NAME="AUTOID-30489"></A><ACLASS="indexterm"NAME="AUTOID-30491"></A><ACLASS="indexterm"NAME="AUTOID-30495"></A>[One weakness of the <EMCLASS="emphasis">grep</EM> family of programs is that they areline-oriented.They read only one line at a time, and so they can'tfind patterns (such as phrases) that are split across two lines.<SPANCLASS="link">agrep (<ACLASS="linkend"HREF="ch27_08.htm"TITLE="glimpse and agrep ">27.8</A>)</SPAN>can do multiline searches.One advantage of the <ICLASS="filename">cgrep.sed</I> script is that it showshow to handle multiple-line patterns in <ICLASS="filename">sed</I>-and can be adaptedfor work other than searches. -JP ]</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="cgrep.sed">cgrep.sed</A><BR></TH><TDVALIGN="TOP"> It may surprise you to learn that a fairly decent context <EMCLASS="emphasis">grep</EM>program can be built using<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_01.htm#UPT-ART-7996"TITLE="Two Things You Must Know About sed ">34.1</A>)</SPAN>.This <EMCLASS="emphasis">sed</EM> version of <EMCLASS="emphasis">cgrep</EM> is used the same way as the Perl versionin article<ACLASS="xref"HREF="ch27_13.htm"TITLE="More grep-like Programs Written in Perl ">27.13</A>:</TD></TR></TABLE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cgrep -10 system main.c</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will find all lines containing the word <EMCLASS="emphasis">system</EM> in the file <EMCLASS="emphasis">main.c</EM>,and show 10 additional lines of context above and below each match.(The -<CODECLASS="replaceable"><I>context</I></CODE> option must be at least 1, and defaults to 2 lines.)It differs from the Perl version in that, if several matches occurwithin the same context, the lines are printed as one large "hunk"rather than repeated smaller hunks.Each new block of context ispreceded by the line number of the first occurrence in that hunk.This script can also search for patterns that span lines:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cgrep -3 "awk.*perl"</PRE></BLOCKQUOTE></P><PCLASS="para">will find all occurrences of the word "awk" where it is followed bythe word "perl" somewhere within the next 3 lines.The pattern canbe any<SPANCLASS="link">simple regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>,with one notable exception:because you can match across lines, you should use <CODECLASS="literal">\n</CODE>in place of the <CODECLASS="literal">^</CODE> and <CODECLASS="literal">$</CODE> metacharacters.</P><PCLASS="para">Article<ACLASS="xref"HREF="ch34_17.htm"TITLE="Searching for Patterns Split Across Lines ">34.17</A>explains how the script works.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">GU</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_10.htm"TITLE="27.10 Search RCS Files with rcsgrep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.10 Search RCS Files with rcsgrep "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_12.htm"TITLE="27.12 Make Custom grep Commands (etc.) with perl "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.12 Make Custom grep Commands (etc.) with perl "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">27.10 Search RCS Files with rcsgrep </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.12 Make Custom grep Commands (etc.) with perl </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 + -