?? ch06_01.htm
字號:
<html><head><title>Pattern Matching (UNIX in a Nutshell: System V Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Arnold Robbins" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="1-56592-427-4" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="UNIX in a Nutshell: System V Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Book Title" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part2.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 6. Pattern Matching</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch06_01.htm#sect1">Filenames Versus Patterns</a><br /><a href="ch06_02.htm">Metacharacters, Listed by Unix Program</a><br /><a href="ch06_03.htm">Metacharacters</a><br /><a href="ch06_04.htm">Examples of Searching</a><br /></p></div><p>A number of Unix text-processing utilitieslet you search for, and in some cases change,text patterns rather than fixed strings. These utilities include the editing programs<tt class="literal">ed</tt>, <tt class="literal">ex</tt>, <tt class="literal">vi</tt>,and <tt class="literal">sed</tt>, the <tt class="literal">awk</tt> programming language, andthe commands <tt class="literal">grep</tt> and <tt class="literal">egrep</tt>.Text patterns (formally called regular expressions) contain normal characters mixed withspecial characters (called metacharacters).<a name="unut-ch-6-ix-pattern-matching" /><a name="unut-ch-6-ix-text-pattern-matching" /><a name="unut-ch-6-ix-searching-pattern-matching" /></p><p>This chapter presents the following topics:</p><ul><li><p>Filenames versus patterns</p></li><li><p>List of metacharacters available to each program</p></li><li><p>Description of metacharacters</p></li><li><p>Examples</p></li></ul><p>For more information on regular expressions,see <em class="citetitle">Mastering Regular Expressions</em>, listed inthe <a href="biblio.htm">Bibliography</a>.</p><div class="sect1"><a name="unut-ch-6-sect-1" /><h2 class="sect1"><a name="sect1" />6.1. Filenames Versus Patterns</h2><p><a name="unut-ch-6-ix-metacharacters-for-pattern-matching" /><a name="IXT-6-123092" />Metacharacters used in pattern matching are different frommetacharacters used for filename expansion (see<a href="ch04_01.htm">Chapter 4</a>, and <a href="ch05_01.htm">Chapter 5</a>).When you issue a command on thecommand line, special characters are seen first by theshell, then by the program; therefore, unquoted metacharacters are interpreted by the shell for filename expansion.The command:</p><blockquote><pre class="code">$ <tt class="userinput"><b>grep [A-Z]* chap[12]</b></tt></pre></blockquote><p>could, for example, be transformed by the shell into:</p><blockquote><pre class="code">$ <tt class="userinput"><b>grep Array.c Bug.c Comp.c chap1 chap2</b></tt></pre></blockquote><p>and would then try to find the pattern <tt class="literal">Array.c</tt> in files <tt class="literal">Bug.c</tt>, <tt class="literal">Comp.c</tt>, <tt class="literal">chap1</tt>, and <tt class="literal">chap2</tt>.To bypass the shell and pass the special charactersto <tt class="literal">grep</tt>, use quotes:</p><blockquote><pre class="code">$ <tt class="userinput"><b>grep "[A-Z]*" chap[12]</b></tt></pre></blockquote><p>Double quotes suffice in most cases, but single quotes are the safest bet.</p><p>Note also that in pattern matching, <tt class="literal">?</tt> matches zero or one instance ofa regular expression; in filename expansion, <tt class="literal">?</tt> matches a singlecharacter.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part2.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch06_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">II. Text Editing and Processing</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">6.2. Metacharacters, Listed by Unix Program</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2003</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,88,96" href="../index.htm"><area shape="rect" coords="90,0,165,96" href="../upt/index.htm"><area shape="rect" coords="168,1,253,107" href="../mac/index.htm"><area shape="rect" coords="255,0,335,97" href="../korn/index.htm"><area shape="rect" coords="337,0,415,109" href="../unixnut/index.htm"><area shape="rect" coords="417,0,512,122" href="../sedawk/index.htm"><area shape="rect" coords="514,0,605,105" href="../lunix/index.htm"><area shape="rect" coords="611,2,694,121" href="../vi/index.htm"> </map></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -