📄 _chapter 4.htm
字号:
<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 4</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 3.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 5.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td></tr></table>
<h2 class="docChapterTitle">Chapter 4. <span class="docEmphasis">sed,</span> the
Streamlined Editor</h2><ul><li> <a class="docLink" href="#ch04lev1sec1">4.1 What Is <span class="docEmphasis">sed?</span></a></li>
<li> <a class="docLink" href="#ch04lev1sec2">4.2 How Does <span class="docEmphasis">sed</span> Work?</a></li>
<li> <a class="docLink" href="#ch04lev1sec3">4.3 Addressing</a></li>
<li> <a class="docLink" href="#ch04lev1sec4">4.4 Commands and Options</a></li>
<li> <a class="docLink" href="#ch04lev1sec5">4.5 Error Messages and Exit Status</a></li>
<li> <a class="docLink" href="#ch04lev1sec6">4.6 <span class="docEmphasis">sed</span> Examples</a></li>
<li> <a class="docLink" href="#ch04lev1sec7">4.7 <span class="docEmphasis">sed</span> Scripting</a></li>
<li> <a class="docLink" href="#ch04lev1sec8">UNIX TOOLS LAB EXERCISE</a></li>
</ul>
<p class="docText" align="left">
<img alt="graphics/ch04.gif" src="ch04.gif" border="0" width="500" height="350"></p>
<h3 class="docSection1Title" id="ch04lev1sec1">4.1 What Is <span class="docEmphasis">sed?</span></h3>
<p class="docText">S<span class="docEmphasis">ed</span> is a streamlined,
noninteractive editor. It allows you to perform the same kind of editing tasks
used in the <span class="docEmphasis">vi</span> and <span class="docEmphasis">ex</span>
editors. Instead of working interactively with the editor, the
<span class="docEmphasis">sed</span> program lets you type your editing commands
at the command line, name the file, and then see the output of the editing
command on the screen. The <span class="docEmphasis">sed</span> editor is
nondestructive. It does not change your file unless you save the output with
shell redirection. All lines are printed to the screen by default.</p>
<p class="docText">The <span class="docEmphasis">sed</span> editor is useful in
shell scripts, where using interactive editors such as <span class="docEmphasis">
vi</span> or <span class="docEmphasis">ex</span> would require the user of the
script to have familiarity with the editor and would expose the user to making
unwanted modifications to the open file. You can also put
<span class="docEmphasis">sed</span> commands in a file called a
<span class="docEmphasis">sed</span> script, if you need to perform multiple
edits or do not like worrying about quoting the <span class="docEmphasis">sed</span>
commands at the shell command line.<span id="ENB4-1"><a class="docLink" href="#EN4-1"><sup>[1]</sup></a></span></p>
<h3 class="docSection1Title" id="ch04lev1sec2">4.2 How Does <span class="docEmphasis">sed</span>
Work?</h3>
<p class="docText">The <span class="docEmphasis">sed</span> editor processes a
file (or input) one line at a time and sends its output to the screen. Its
commands are those you may recognize from the <span class="docEmphasis">vi</span>
and <span class="docEmphasis">ed/ex</span> editors. <span class="docEmphasis">
Sed</span> stores the line it is currently processing in a temporary buffer
called a <span class="docEmphasis">pattern space.</span> Once
<span class="docEmphasis">sed</span> is finished processing the line in the
pattern space (i.e., executing <span class="docEmphasis">sed</span> commands on
that line), the line in the pattern space is sent to the screen (unless the
command was to delete the line or suppress its printing). After the line has
been processed, it is removed from the pattern space and the next line is then
read into the pattern space, processed, and displayed. <span class="docEmphasis">
Sed</span> ends when the last line of the input file has been processed. By
storing each line in a temporary buffer and performing edits on that line, the
original file is never altered or destroyed.</p>
<h3 class="docSection1Title" id="ch04lev1sec3">4.3 Addressing</h3>
<p class="docText">You can use addressing to decide which lines you want to
edit. The addresses can be in the form of numbers or regular expressions, or a
combination of both. Without specifying an address, <span class="docEmphasis">
sed</span> processes all lines of the input file.</p>
<p class="docText">When an address consists of a number, the number represents a
line number. A dollar sign can be used to represent the last line of the input
file. If a comma separates two line numbers, the addresses that will be
processed are within that range of lines, including the first and last line in
the range. The range may be numbers, regular expressions, or a combination of
numbers and regular expressions.</p>
<p class="docText">S<span class="docEmphasis">ed</span> commands tell
<span class="docEmphasis">sed</span> what to do with the line: print it, remove
it, change it, and so forth.</p>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">FORMAT</h2>
<pre>sed 'command' filename(s)
</pre>
</td>
</tr>
</table>
<h5 id="ch04list01" class="docExampleTitle">Example 4.1 </h5>
<pre>1 % <span class="docEmphStrong">sed '1,3p' myfile</span>
2 % <span class="docEmphStrong">sed -n '/[Jj]ohn/p' datafile</span></pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Lines 1 through 3 of <span class="docEmphasis">myfile</span>
are printed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Only lines matching the pattern
<span class="docEmphasis">John</span> or <span class="docEmphasis">john</span>
in <span class="docEmphasis">myfile</span> are printed.</span></li>
</ol>
</span></td>
</tr>
</table>
<h3 class="docSection1Title" id="ch04lev1sec4">4.4 Commands and Options</h3>
<p class="docText"><span class="docEmphasis">Sed</span> commands tell
<span class="docEmphasis">sed</span> how to process each line of input specified
by an address. If an address is not given, <span class="docEmphasis">sed</span>
processes every line of input. (The % is the csh prompt.) See
<a class="docLink" href="#ch04table01">Table 4.1</a> for a list of
<span class="docEmphasis">sed</span> commands and what they do, and see
<a class="docLink" href="#ch04table02">Table 4.2</a> for a list of options and
how they control <span class="docEmphasis">sed</span>'s behavior.</p>
<h5 id="ch04list02" class="docExampleTitle">Example 4.2 </h5>
<pre>% <span class="docEmphStrong">sed '1,3d' file</span>
</pre>
<table cellSpacing="0" width="90%" border="1" align="center">
<tr>
<td>
<h2 class="docSidebarTitle">EXPLANATION</h2>
<p class="docText"><span class="docEmphasis">Sed</span> will delete lines 1
through 3.</td>
</tr>
</table>
<p> </p>
<table cellSpacing="0" cellPadding="1" width="100%" border="1">
<caption>
<h5 id="ch04table01" class="docTableTitle">Table 4.1. <span class="docEmphasis">sed</span>
Commands</h5>
</caption>
<colgroup span="2" align="left">
</colgroup>
<tr>
<th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
Command</span> </th>
<th class="docTableHeader" vAlign="top"><span class="docEmphBoldItalic">
Function</span> </th>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">a\</span>
</td>
<td class="docTableCell" vAlign="top">Appends one or more lines of text to
the current line. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">c\</span>
</td>
<td class="docTableCell" vAlign="top">Changes (replaces) text in the current
line with new text. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">d</span>
</td>
<td class="docTableCell" vAlign="top">Deletes lines. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">i\</span>
</td>
<td class="docTableCell" vAlign="top">Inserts text above the current line.
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">h</span>
</td>
<td class="docTableCell" vAlign="top">Copies the contents of the pattern
space to a holding buffer. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">H</span>
</td>
<td class="docTableCell" vAlign="top">Appends the contents of the pattern
space to a holding buffer. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">g</span>
</td>
<td class="docTableCell" vAlign="top">Gets what is in the holding buffer and
copies it into the pattern buffer, overwriting what was there. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">G</span>
</td>
<td class="docTableCell" vAlign="top">Gets what is in the holding buffer and
copies it into the pattern buffer, appending to what was there. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">l</span>
</td>
<td class="docTableCell" vAlign="top">Lists nonprinting characters. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">p</span>
</td>
<td class="docTableCell" vAlign="top">Prints lines. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">n</span>
</td>
<td class="docTableCell" vAlign="top">Reads the next input line and starts
processing the newline with the next command rather than the first command.
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">q</span>
</td>
<td class="docTableCell" vAlign="top">Quits or exits
<span class="docEmphasis">sed.</span> </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">r</span>
</td>
<td class="docTableCell" vAlign="top">Reads lines from a file. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">!</span>
</td>
<td class="docTableCell" vAlign="top">Applies the command to all lines
<span class="docEmphasis">except</span> the selected ones. </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">s</span>
</td>
<td class="docTableCell" vAlign="top">Substitutes one string for another.
</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top" colSpan="2">
<span class="docEmphBoldItalic">Substitution Flags:</span> </td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">g</span>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -