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

📄 _chapter 3.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 3</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body><table width="100%" border="1" bgcolor="#EBEBFF"><tr><td width="5%" align="left" valign="middle"><a href="_chapter 2.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td><td align="center" valign="middle"><a class="docLink" href="Front matter.htm">CONTENTS</a></td><td width="5%" align="right" valign="middle"><a href="_chapter 4.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>


<h2 class="docChapterTitle">Chapter 3. The grep Family</h2><ul><li>&nbsp;<a class="docLink" href="#ch03lev1sec1">3.1 The <span class="docEmphasis">grep</span> Command</a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec2">3.2 <span class="docEmphasis">grep</span> Examples with Regular Expressions</a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec3">3.3 <span class="docEmphasis">grep</span> with Pipes</a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec4">3.4 <span class="docEmphasis">grep</span> with Options</a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec5">3.5 <span class="docEmphasis">egrep</span> (Extended <span class="docEmphasis">grep</span>)</a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec6">3.6 Fixed <span class="docEmphasis">grep</span> or Fast <span class="docEmphasis">grep</span></a></li>
<li>&nbsp;<a class="docLink" href="#ch03lev1sec7">UNIX TOOLS LAB EXERCISE</a></li>
</ul>
<p class="docText">
<img alt="graphics/ch03.gif" src="ch03.gif" border="0" width="500" height="411"></p>
<p class="docText">The <span class="docEmphasis">grep</span> family consists of 
the commands <span class="docEmphasis">grep, egrep,</span> and
<span class="docEmphasis">fgrep.</span> The <span class="docEmphasis">grep</span> 
command globally searches for regular expressions in files and prints all lines 
that contain the expression. The <span class="docEmphasis">egrep</span> and
<span class="docEmphasis">fgrep</span> commands are simply variants of
<span class="docEmphasis">grep.</span> The <span class="docEmphasis">egrep</span> 
command is an extended <span class="docEmphasis">grep,</span> supporting more 
regular expression metacharacters. The <span class="docEmphasis">fgrep</span> 
command, called <span class="docEmphasis">fixed grep,</span> and sometimes
<span class="docEmphasis">fast grep,</span> treats all characters as literals; 
that is, regular expression metacharacters aren't special梩hey match themselves. 
The Free Software Foundation provides a free version of
<span class="docEmphasis">grep,</span> called GNU <span class="docEmphasis">grep.</span> 
These versions of <span class="docEmphasis">grep</span> are the ones used on 
Linux systems, and can be found in <span class="docEmphasis">/usr/xpg4/bin</span> 
on Sun's Solaris OS. The GNU version of <span class="docEmphasis">grep</span> 
has extended the basic regular expression metacharacter set, added POSIX 
compliancy, and included a number of new command line options. They also provide 
a recursive <span class="docEmphasis">grep</span> called
<span class="docEmphasis">rgrep</span> for descending entire directory trees.</p>
<h3 class="docSection1Title" id="ch03lev1sec1">3.1 The <span class="docEmphasis">grep</span> 
Command</h3>
<h4 class="docSection2Title" id="ch03lev2sec1">3.1.1 The Meaning of <span class="docEmphasis">grep</span></h4>
<p class="docText">The name <span class="docEmphasis">grep</span> can be traced 
back to the <span class="docEmphasis">ex</span> editor. If you invoked that 
editor and wanted to search for a string, you would type at the
<span class="docEmphasis">ex</span> prompt:</p>
<pre>: /pattern/p
</pre>
<p class="docText">The first line containing the string
<span class="docEmphasis">pattern</span> would be printed as &quot;<span class="docEmphasis">p</span>&quot; 
by the <span class="docEmphasis">print</span> command. If you wanted all the 
lines that contained <span class="docEmphasis">pattern</span> to be printed, you 
would type:</p>
<pre>: g/pattern/p
</pre>
<p class="docText">When <span class="docEmphasis">g</span> precedes
<span class="docEmphasis">pattern,</span> it means &quot;all lines in the file,&quot; or 
&quot;perform a global substitution.&quot;</p>
<p class="docText">Because the search pattern is called a
<span class="docEmphasis">regular expression,</span> we can substitute
<span class="docEmphasis">RE</span> for <span class="docEmphasis">pattern</span> 
and the command reads:</p>
<pre>: g/RE/p
</pre>
<p class="docText">And there you have it: the meaning of
<span class="docEmphasis">grep</span> and the origin of its name. It means &quot;<span class="docEmphasis">g</span>lobally 
search for the <span class="docEmphasis">r</span>egular
<span class="docEmphasis">e</span>xpression (<span class="docEmphasis">RE</span>) 
and <span class="docEmphasis">p</span>rint out the line.&quot; The nice part of using
<span class="docEmphasis">grep</span> is that you do --not have to invoke an 
editor to perform a search, and you do not need to enclose the regular 
expression in forward slashes. It is much faster than using
<span class="docEmphasis">ex</span> or <span class="docEmphasis">vi.</span></p>
<h4 class="docSection2Title" id="ch03lev2sec2">3.1.2 How <span class="docEmphasis">grep</span> 
Works</h4>
<p class="docText">The <span class="docEmphasis">grep</span> command searches 
for a pattern of characters in a file or multiple files. If the pattern contains 
whitespace, it must be quoted. The pattern is either a quoted string or a single 
word<span id="ENB3-1"><a class="docLink" href="#EN3-1"><sup>[1]</sup></a></span>, 
and all other words following it are treated as filenames.
<span class="docEmphasis">Grep</span> sends its output to the screen and does 
not change or affect the input file in any way.</p>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">FORMAT</h2>
    <pre>grep word filename filename
</pre>
    </td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list01" class="docExampleTitle">Example 3.1 </h5>
<pre><span class="docEmphStrong">grep Tom /etc/passwd</span>
</pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Grep</span> will search for the 
    pattern <span class="docEmphasis">Tom</span> in a file called
    <span class="docEmphasis">/etc/passwd.</span> If successful, the line from 
    the file will appear on the screen; if the pattern is not found, there will 
    be no output at all; and if the file is not a legitimate file, an error will 
    be sent to the screen. If the pattern is found, <span class="docEmphasis">
    grep</span> returns an exit status of 0, indicating success; if the pattern 
    is not found, the exit status returned is 1; and if the file is not found, 
    the exit status is 2.</p>
    <p class="docText">The <span class="docEmphasis">grep</span> program can get 
    its input from a standard input or a pipe, as well as from files. If you 
    forget to name a file, <span class="docEmphasis">grep</span> will assume it 
    is getting input from standard input, the keyboard, and will stop until you 
    type something. If coming from a pipe, the output of a command will be piped 
    as input to the <span class="docEmphasis">grep</span> command, and if a 
    desired pattern is matched, <span class="docEmphasis">grep</span> will print 
    the output to the screen.</td>
  </tr>
</table>
  </center>
</div>
<h5 id="ch03list02" class="docExampleTitle">Example 3.2 </h5>
<pre>% <span class="docEmphStrong">ps -ef | grep root</span>
</pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText">The output of the <span class="docEmphasis">ps</span> 
    command (<span class="docEmphasis">ps 杄f</span> displays all processes 
    running on this system) is sent to <span class="docEmphasis">grep</span> and 
    all lines containing <span class="docEmphasis">root</span> are printed.</td>
  </tr>
</table>
  </center>
</div>
<p class="docText">The <span class="docEmphasis">grep</span> command supports a 
number of regular expression metacharacters (see
<a class="docLink" href="#ch03table01">Table 3.1</a>) to help further define the 
search pattern. It also provides a number of options (see
<a class="docLink" href="#ch03table02">Table 3.2</a>) to modify the way it does 
its search or displays lines. For example, you can provide options to turn off 
case sensitivity, display line numbers, display errors only, and so on.</p>
<h5 id="ch03list03" class="docExampleTitle">Example 3.3 </h5>
<pre>% <span class="docEmphStrong">grep -n  '^jack:' /etc/passwd</span>
</pre>
<div align="center">
  <center>
<table cellSpacing="0" width="90%" border="1" style="border-collapse: collapse" bordercolor="#111111" cellpadding="5">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <p class="docText"><span class="docEmphasis">Grep</span> searches the
    <span class="docEmphasis">/etc/passwd</span> file for
    <span class="docEmphasis">jack;</span> if <span class="docEmphasis">jack</span> 
    is at the beginning of a line, <span class="docEmphasis">grep</span> prints 
    out the number of the line on which <span class="docEmphasis">jack</span> 
    was found and where in the line <span class="docEmphasis">jack</span> was 
    found.</td>
  </tr>
</table>
  </center>
</div>

<p>&nbsp;</p>

<table cellSpacing="0" cellPadding="1" width="100%" border="1">
  <caption>
  <h5 id="ch03table01" class="docTableTitle">Table 3.1. <span class="docEmphasis">grep</span>'s 
  Regular Expression Metacharacters</h5>
  </caption>
  <colgroup span="4" align="left">
  </colgroup>
  <tr>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Metacharacter</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Function</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
    Example</span> </th>
    <th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">What 
    It Matches</span> </th>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">^</span>
    </td>
    <td class="docTableCell" vAlign="top">Beginning-of-line anchor </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'^love'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches all lines beginning with
    <span class="docEmphasis">love.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">$</span>
    </td>
    <td class="docTableCell" vAlign="top">End-of-line anchor </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'love$'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches all lines ending with
    <span class="docEmphasis">love.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">. </td>
    <td class="docTableCell" vAlign="top">Matches one character </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'l..e'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines containing an
    <span class="docEmphasis">l,</span> followed by two characters, followed by 
    an <span class="docEmphasis">e.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top">* </td>
    <td class="docTableCell" vAlign="top">Matches zero or more characters </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">' *love'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines with zero or more 
    spaces, followed by the pattern <span class="docEmphasis">love.</span> </td>
  </tr>
  <tr>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">[ ]</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches one character in the set </td>
    <td class="docTableCell" vAlign="top"><span class="docEmphasis">'[Ll]ove'</span>
    </td>
    <td class="docTableCell" vAlign="top">Matches lines containing
    <span class="docEmphasis">love</span> or <span class="docEmphasis">Love.</span>
    </td>
  </tr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -