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

📄 ch10_03.htm

📁 unix基础教程
💻 HTM
字号:
<html><head><title>Syntax of sed Commands (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 &amp; 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="ch10_02.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="ch10_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">10.3. Syntax of sed Commands</h2><p><a name="unut-ch-10-ix-commands-sed-editor" /><a name="unut-ch-10-ix-sed-editor-commands-for" /><tt class="literal">sed</tt> commands have the general form:</p><blockquote><pre class="code">[<em class="replaceable"><tt>address</em>[,<em class="replaceable">address</em>]][!]<em class="replaceable">command</em> [<em class="replaceable">arguments</tt></em>]</pre></blockquote><p><tt class="literal">sed</tt> copies each line of input into the pattern space.<tt class="literal">sed</tt> instructionsconsist of <em class="emphasis">addresses</em> andediting <em class="emphasis">commands</em>.  If the address of thecommand matches the line in the pattern space, the command isapplied to that line.  If a command has no address, it is appliedto each input line.  If a command changes the contents of the pattern space,subsequent commands and addresses are applied to the current line inthe pattern space, not the original input line.</p><p><em class="emphasis">commands</em> consist of a single letter or symbol; they aredescribed later, alphabetically and by group.<em class="emphasis">arguments</em> include the label supplied to <tt class="literal">b</tt> or<tt class="literal">t</tt>, thefilename supplied to <tt class="literal">r</tt> or <tt class="literal">w</tt>, and the substitution flagsfor <tt class="literal">s</tt>.<em class="emphasis">addresses</em> are described below.</p><a name="unut-ch-10-sect-3.1" /><div class="sect2"><h3 class="sect2">10.3.1. Pattern Addressing</h3><p><a name="IXT-10-123222" /><a name="IXT-10-123223" />A <tt class="literal">sed</tt> command can specify zero, one, or two addresses.  An address can be a line number, the symbol <tt class="literal">$</tt> (for last line),or a regular expression enclosed in slashes (/<em class="emphasis">pattern</em>/).Regular expressions are described in<a href="ch06_01.htm">Chapter 6</a>.Additionally, <tt class="literal">\n</tt> matches any newline in thepattern space (resulting from the <tt class="literal">N</tt> command), but not thenewline at the end of the pattern space.</p><table border="1" cellpadding="3"><tr><th><em class="emphasis">If the Command Specifies:</em></th><th><em class="emphasis">Then the Command is Applied to:</em></th></tr><tr><td>No address</td><td>Each input line.</td></tr><tr><td>One address</td><td><p>Any line matching the address.Some commands accept only one address:  <tt class="literal">a</tt>, <tt class="literal">i</tt>, <tt class="literal">r</tt>, <tt class="literal">q</tt>, and <tt class="literal">=</tt>.</p></td></tr><tr><td>Two comma-separated addresses</td><td><p>First matching line and all succeeding lines up to and including a line matching the second address. </p></td></tr><tr><td>An address followed by <tt class="literal">!</tt></td><td>All lines that do <em class="emphasis">not</em> match the address.<a name="IXT-10-123224" /></td></tr></table><p></div><a name="unut-ch-10-sect-3.2" /><div class="sect2"><h3 class="sect2">10.3.2. Examples</h3><table border="1" cellpadding="3"><tr><td><tt class="literal">s/xx/yy/g</tt></td><td>Substitute on all lines (all occurrences).</td></tr><tr><td><tt class="literal">/BSD/d</tt></td><td>Delete lines containing <tt class="literal">BSD</tt>.</td></tr><tr><td><tt class="literal">/^BEGIN/,/^END/p</tt></td><td>Print between <tt class="literal">BEGIN</tt> and <tt class="literal">END</tt>, inclusive.</td></tr><tr><td><tt class="literal">/SAVE/!d</tt></td><td>Delete any line that doesn't contain <tt class="literal">SAVE</tt>.</td></tr><tr><td><tt class="literal">/BEGIN/,/END/!s/xx/yy/g</tt></td><td>Substitute on all lines, except between <tt class="literal">BEGIN</tt> and <tt class="literal">END</tt>.</td></tr></table><p><p>Braces (<tt class="literal">{}</tt>) are used in <tt class="literal">sed</tt>to nest one address inside another orto apply multiple commands at the same address.</p><blockquote><pre class="code">[/<em class="replaceable"><tt>pattern</em>/[,/<em class="replaceable">pattern</tt></em>/]]{<em class="replaceable">command1command2</em>}</pre></blockquote><p>The opening curly brace must end its line, and the closing curlybrace must be on a line by itself.Be sure there are no spaces after the braces.</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="ch10_02.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="ch10_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">10.2. Command-Line Syntax</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">10.4. Group Summary of sed Commands</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 &copy; 2003</a> O'Reilly &amp; 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>

⌨️ 快捷键说明

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