📄 sed.html
字号:
</pre></dd><dt><b>[</b><i>0addr</i><b>]</b></dt><dd>Ignore this empty command.</dd><dt><b>[</b><i>0addr</i><b>]#</b></dt><dd>Ignore the <tt>'#'</tt> and the remainder of the line (treat them as a comment), with the single exception that if the firsttwo characters in the script are <tt>"#n"</tt> , the default output shall be suppressed; this shall be the equivalent of specifying<b>-n</b> on the command line.</dd></dl></blockquote><h4><a name="tag_04_126_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt> 0</dt><dd>Successful completion.</dd><dt>>0</dt><dd>An error occurred.</dd></dl></blockquote><h4><a name="tag_04_126_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_126_16"></a>APPLICATION USAGE</h4><blockquote><p>Regular expressions match entire strings, not just individual lines, but a <newline> is matched by <tt>'\n'</tt> in a<i>sed</i> RE; a <newline> is not allowed by the general definition of regular expression in IEEE Std 1003.1-2001.Also note that <tt>'\n'</tt> cannot be used to match a <newline> at the end of an arbitrary input line; <newline>sappear in the pattern space as a result of the <b>N</b> editing command.</p></blockquote><h4><a name="tag_04_126_17"></a>EXAMPLES</h4><blockquote><p>This <i>sed</i> script simulates the BSD <a href="../utilities/cat.html"><i>cat</i></a> <b>-s</b> command, squeezing excessblank lines from standard input.</p><pre><tt>sed -n '# Write non-empty lines././ { p d }# Write a single empty line, then look for more empty lines./^$/ p# Get next line, discard the held <newline> (empty line),# and look for more empty lines.:Empty/^$/ { N s/.// b Empty }# Write the non-empty line before going back to search# for the first in a set of empty lines. p'</tt></pre></blockquote><h4><a name="tag_04_126_18"></a>RATIONALE</h4><blockquote><p>This volume of IEEE Std 1003.1-2001 requires implementations to support at least ten distinct <i>wfile</i>s, matchinghistorical practice on many implementations. Implementations are encouraged to support more, but conforming applications should notexceed this limit.</p><p>The exit status codes specified here are different from those in System V. System V returns 2 for garbled <i>sed</i> commands,but returns zero with its usage message or if the input file could not be opened. The standard developers considered this to be abug.</p><p>The manner in which the <b>l</b> command writes non-printable characters was changed to avoid the historicalbackspace-overstrike method, and other requirements to achieve unambiguous output were added. See the RATIONALE for <a href="ed.html"><i>ed</i></a> for details of the format chosen, which is the same as that chosen for <i>sed</i>.</p><p>This volume of IEEE Std 1003.1-2001 requires implementations to provide pattern and hold spaces of at least 8192bytes, larger than the 4000 bytes spaces used by some historical implementations, but less than the 20480 bytes limit used in anearly proposal. Implementations are encouraged to allocate dynamically larger pattern and hold spaces as needed.</p><p>The requirements for acceptance of <blank>s and <space>s in command lines has been made more explicit than in earlyproposals to describe clearly the historical practice and to remove confusion about the phrase "protect initial blanks[<i>sic</i>] and tabs from the stripping that is done on every script line" that appears in much of the historical documentationof the <i>sed</i> utility description of text. (Not all implementations are known to have stripped <blank>s from text lines,although they all have allowed leading <blank>s preceding the address on a command line.)</p><p>The treatment of <tt>'#'</tt> comments differs from the SVID which only allows a comment as the first line of the script, butmatches BSD-derived implementations. The comment character is treated as a command, and it has the same properties in terms ofbeing accepted with leading <blank>s; the BSD implementation has historically supported this.</p><p>Early proposals required that a <i>script_file</i> have at least one non-comment line. Some historical implementations havebehaved in unexpected ways if this were not the case. The standard developers considered that this was incorrect behavior and thatapplication developers should not have to avoid this feature. A correct implementation of this volume ofIEEE Std 1003.1-2001 shall permit <i>script_file</i>s that consist only of comment lines.</p><p>Early proposals indicated that if <b>-e</b> and <b>-f</b> options were intermixed, all <b>-e</b> options were processed beforeany <b>-f</b> options. This has been changed to process them in the order presented because it matches historical practice and ismore intuitive.</p><p>The treatment of the <b>p</b> flag to the <b>s</b> command differs between System V and BSD-based systems when the defaultoutput is suppressed. In the two examples:</p><pre><tt>echo a | sed 's/a/A/p'echo a | sed -n 's/a/A/p'</tt></pre><p>this volume of IEEE Std 1003.1-2001, BSD, System V documentation, and the SVID indicate that the first example shouldwrite two lines with <b>A</b>, whereas the second should write one. Some System V systems write the <b>A</b> only once in bothexamples because the <b>p</b> flag is ignored if the <b>-n</b> option is not specified.</p><p>This is a case of a diametrical difference between systems that could not be reconciled through the compromise of declaring thebehavior to be unspecified. The SVID/BSD/System V documentation behavior was adopted for this volume ofIEEE Std 1003.1-2001 because:</p><ul><li><p>No known documentation for any historic system describes the interaction between the <b>p</b> flag and the <b>-n</b> option.</p></li><li><p>The selected behavior is more correct as there is no technical justification for any interaction between the <b>p</b> flag andthe <b>-n</b> option. A relationship between <b>-n</b> and the <b>p</b> flag might imply that they are only used together, but thisignores valid scripts that interrupt the cyclical nature of the processing through the use of the <b>D</b>, <b>d</b>, <b>q</b>, orbranching commands. Such scripts rely on the <b>p</b> suffix to write the pattern space because they do not make use of the defaultoutput at the "bottom" of the script.</p></li><li><p>Because the <b>-n</b> option makes the <b>p</b> flag unnecessary, any interaction would only be useful if <i>sed</i> scriptswere written to run both with and without the <b>-n</b> option. This is believed to be unlikely. It is even more unlikely thatprogrammers have coded the <b>p</b> flag expecting it to be unnecessary. Because the interaction was not documented, the likelihoodof a programmer discovering the interaction and depending on it is further decreased.</p></li><li><p>Finally, scripts that break under the specified behavior produce too much output instead of too little, which is easier todiagnose and correct.</p></li></ul><p>The form of the substitute command that uses the <b>n</b> suffix was limited to the first 512 matches in an early proposal. Thislimit has been removed because there is no reason an editor processing lines of {LINE_MAX} length should have this restriction. Thecommand <b>s/a/A/2047</b> should be able to substitute the 2047th occurrence of <b>a</b> on a line.</p><p>The <b>b</b>, <b>t</b>, and <b>:</b> commands are documented to ignore leading white space, but no mention is made of trailingwhite space. Historical implementations of <i>sed</i> assigned different locations to the labels <tt>'x'</tt> and<tt>"x "</tt> . This is not useful, and leads to subtle programming errors, but it is historical practice, and changing itcould theoretically break working scripts. Implementors are encouraged to provide warning messages about labels that are never usedor jumps to labels that do not exist.</p><p>Historically, the <i>sed</i> <b>!</b> and <b>}</b> editing commands did not permit multiple commands on a single line using asemicolon as a command delimiter. Implementations are permitted, but not required, to support this extension.</p></blockquote><h4><a name="tag_04_126_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_126_20"></a>SEE ALSO</h4><blockquote><p><a href="awk.html"><i>awk</i></a> , <a href="ed.html"><i>ed</i></a> , <a href="grep.html"><i>grep</i></a></p></blockquote><h4><a name="tag_04_126_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><h4><a name="tag_04_126_22"></a>Issue 5</h4><blockquote><p>The FUTURE DIRECTIONS section is added.</p></blockquote><h4><a name="tag_04_126_23"></a>Issue 6</h4><blockquote><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>Implementations are required to support at least ten <i>wfile</i> arguments in an editing command.</p></li></ul><p>The EXTENDED DESCRIPTION is changed to align with the IEEE P1003.2b draft standard.</p><p>IEEE PASC Interpretation 1003.2 #190 is applied.</p><p>IEEE PASC Interpretation 1003.2 #203 is applied, clarifying the meaning of the backslash escape sequences in a replacementstring for a BRE.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -