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

📄 ch15_07.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 15] 15.7 Getting a List of Matching Files with grep -l </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:37:26Z"><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="ch15_01.htm"TITLE="15. Wildcards"><LINKREL="prev"HREF="ch15_06.htm"TITLE="15.6 Maybe You Shouldn't Use Wildcards in Pathnames "><LINKREL="next"HREF="ch15_08.htm"TITLE="15.8 Getting a List of Non-Matching Files with grep -c "></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="ch15_06.htm"TITLE="15.6 Maybe You Shouldn't Use Wildcards in Pathnames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 15.6 Maybe You Shouldn't Use Wildcards in Pathnames "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 15<BR>Wildcards</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch15_08.htm"TITLE="15.8 Getting a List of Non-Matching Files with grep -c "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 15.8 Getting a List of Non-Matching Files with grep -c "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1510">15.7 Getting a List of Matching Files with grep -l </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-16427"></A>Normally when you run<SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_01.htm#UPT-ART-7420"TITLE="Different Versions of grep ">27.1</A>)</SPAN>on a group of files, the output lists thefilename along with the line containing the search pattern. Sometimes youwant to know only the names of the files, and you don't care to know theline (or lines) that match.In this case, use the <EMCLASS="emphasis">-l</EM> (lowercase letter &quot;l&quot;)option to list only filenames wherematches occur.For example, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>grep -l R5 </B></CODE><CODECLASS="replaceable"><I>file1 file2 ... </I></CODE><CODECLASS="userinput"><B>&gt; r5.filelist</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">searches the files for a line containing the string <CODECLASS="literal">R5</CODE>,produces a list of those filenames, andstores the list in <EMCLASS="emphasis">r5.filelist</EM>. (This list might represent the filescontaining Release 5 documentation of a particular product.) Because theseRelease 5 files can now be referenced by one list, you can treat them as asingle entity and run various commands on them all at once:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>print `cat r5.filelist`   </B></CODE><EMCLASS="emphasis">Print only the Release 5 files</EM>% <CODECLASS="userinput"><B>grep UNIX `cat r5.filelist`   </B></CODE><EMCLASS="emphasis">Search limited to the Release 5 files</EM></PRE></TD></TR></TABLE></P><PCLASS="para">You don't have to create a file list, though.You can insert the output of a <EMCLASS="emphasis">grep</EM> directly into a command linewith command substitution.For example, to edit only the subset of files containing <CODECLASS="literal">R5</CODE>, you would type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>vi `grep -l R5 <CODECLASS="replaceable"><I>files</I></CODE>`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">grep&nbsp;-l</EM> is also good for shell programs that need to checkwhether a file contains a particular string.The traditional way to do that test is by throwing away <EMCLASS="emphasis">grep</EM>'soutput and checking its exit status:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">if grep <CODECLASS="replaceable"><I>something somefile</I></CODE> &gt;/dev/nullthen ...</PRE></BLOCKQUOTE></P><PCLASS="para">If <EMCLASS="emphasis">somefile</EM> is huge, though, <EMCLASS="emphasis">grep</EM> has to search all of it.Adding the <EMCLASS="emphasis">grep -l</EM> option saves time because <EMCLASS="emphasis">grep</EM> can stopsearching after it finds the first matching line.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DG</SPAN>, <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="ch15_06.htm"TITLE="15.6 Maybe You Shouldn't Use Wildcards in Pathnames "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 15.6 Maybe You Shouldn't Use Wildcards in Pathnames "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="ch15_08.htm"TITLE="15.8 Getting a List of Non-Matching Files with grep -c "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 15.8 Getting a List of Non-Matching Files with grep -c "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">15.6 Maybe You Shouldn't Use Wildcards in Pathnames </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">15.8 Getting a List of Non-Matching Files with grep -c </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 + -