📄 ch27_02.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 27] 27.2 Searching for Text with grep </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:17Z"><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_01.htm"TITLE="27.1 Different Versions of grep "><LINKREL="next"HREF="ch27_03.htm"TITLE="27.3 Finding Text That Doesn't Match "></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_01.htm"TITLE="27.1 Different Versions of grep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.1 Different Versions of grep "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_03.htm"TITLE="27.3 Finding Text That Doesn't Match "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.3 Finding Text That Doesn't Match "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2550">27.2 Searching for Text with grep </A></H2><PCLASS="para">There are many well-known benefits provided by <EMCLASS="emphasis">grep</EM> to the user who doesn't rememberwhat his or her files contain. Even users of non-UNIX systems who make fun of its obscure name wishthey had a utility with its power to search through a set of filesfor an arbitrary text pattern, known as a<ACLASS="indexterm"NAME="AUTOID-29972"></A><SPANCLASS="link">regular expression (<ACLASS="linkend"HREF="ch26_04.htm"TITLE="Using Metacharacters in Regular Expressions ">26.4</A>)</SPAN>.</P><PCLASS="para">The main function of <EMCLASS="emphasis">grep</EM> is to look for strings matching a regularexpression and print only the lines found.Use <EMCLASS="emphasis">grep</EM> when you want to look at how aparticular word is used in one or more files.For example, here's how to list the lines in the file <EMCLASS="emphasis">ch04</EM> that contain either <EMCLASS="emphasis">run-time</EM> or <EMCLASS="emphasis">run time</EM>:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch08_14.htm"TITLE="8.14 Bourne Shell Quoting ">".."</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>grep "run[- ]time" ch04</B></CODE>This procedure avoids run-time errors for not-assignedand a run-time error message is produced.run-time error message is produced.program aborts and a run-time error message is produced.DIMENSION statement in BASIC is executable at run time.This means that arrays can be redimensioned at run time.accessible or not open, the program aborts and a run-time</PRE></TD></TR></TABLE></P><PCLASS="para">Another use might be to look for a specific<SPANCLASS="link"><EMCLASS="emphasis">nroff</EM>/<EMCLASS="emphasis">troff</EM> macro (<ACLASS="linkend"HREF="ch43_14.htm"TITLE="nroff/troff and Macro Packages ">43.14</A>)</SPAN>in a file.In a file coded with mm macros, the following command will listtop-level (<CODECLASS="literal">.H1</CODE>) and second-level (<CODECLASS="literal">.H2</CODE>) headings:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch15_02.htm"TITLE="15.2 Filename Wildcards in a Nutshell ">[..]</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>grep "^\.H[12]" ch0[12]</B></CODE>ch01:.H1 "Introduction"ch01:.H1 "Windows, Screens, and Images"ch01:.H2 "The Standard Screen-stdscr"ch01:.H2 "Adding Characters"ch02:.H1 "Introduction"ch02:.H1 "What Is Terminal Independence?"ch02:.H2 "Termcap"ch02:.H2 "Terminfo"</PRE></TD></TR></TABLE></P><PCLASS="para">In effect, it produces a quick outline of the contents of these files.</P><PCLASS="para"><EMCLASS="emphasis">grep</EM> is also often used as a <SPANCLASS="link">filter (<ACLASS="linkend"HREF="ch01_03.htm"TITLE="Programs Are Designed to Work Together ">1.3</A>)</SPAN>,to select from the output of some other program.For example,not all versions of<SPANCLASS="link"><EMCLASS="emphasis">ps</EM> (<ACLASS="linkend"HREF="ch38_05.htm"TITLE="The ps Command ">38.5</A>)</SPAN>allow you to print out the processes belonging to another user, butit's easy to simulate this behavior by listing all processes andpiping the output to <EMCLASS="emphasis">grep</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ps -aux | grep jerry</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">There are several options commonly used with <EMCLASS="emphasis">grep</EM>.The <EMCLASS="emphasis">-i</EM> option specifiesthat the search ignore the distinction between uppercase and lowercase.The<SPANCLASS="link"><EMCLASS="emphasis">-c</EM> option (<ACLASS="linkend"HREF="ch15_08.htm"TITLE="Getting a List of Non-Matching Files with grep -c ">15.8</A>)</SPAN>tells <EMCLASS="emphasis">grep</EM> to return only a count of the number of lines matched.The <EMCLASS="emphasis">-w</EM> option searches for the pattern "as a word."For example, <CODECLASS="literal">grep if</CODE> would match words like <EMCLASS="emphasis">cliff</EM> or<EMCLASS="emphasis">knife</EM>, but <CODECLASS="literal">grep -w if</CODE> wouldn't.The<SPANCLASS="link"><EMCLASS="emphasis">-l</EM> option (<ACLASS="linkend"HREF="ch15_07.htm"TITLE="Getting a List of Matching Files with grep -l ">15.7</A>)</SPAN>returns only the name of the file when <EMCLASS="emphasis">grep</EM> finds a match.This can be used to prepare a list of files for another command.The<SPANCLASS="link"><EMCLASS="emphasis">-v</EM> option (<ACLASS="linkend"HREF="ch27_03.htm"TITLE="Finding Text That Doesn't Match ">27.3</A>)</SPAN>reverses the normal action, and only prints out lines that don'tmatch the search pattern.</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><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_01.htm"TITLE="27.1 Different Versions of grep "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 27.1 Different Versions of 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="ch27_03.htm"TITLE="27.3 Finding Text That Doesn't Match "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 27.3 Finding Text That Doesn't Match "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">27.1 Different Versions of 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">27.3 Finding Text That Doesn't Match </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 + -