708 lines
33 KiB
HTML
708 lines
33 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>sed</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="sed"></a> <a name="tag_04_126"></a><!-- sed -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_04_126_01"></a>NAME</h4>
|
|
|
|
<blockquote>sed - stream editor</blockquote>
|
|
|
|
<h4><a name="tag_04_126_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>sed</tt> <b>[</b><tt>-n</tt><b>]</b> <i>script</i><b>[</b><i>file</i><tt>...</tt><b>]</b><tt><br>
|
|
<br>
|
|
sed</tt> <b>[</b><tt>-n</tt><b>][</b><tt>-e</tt> <i>script</i><b>]</b><tt>...</tt><b>[</b><tt>-f</tt>
|
|
<i>script_file</i><b>]</b><tt>...</tt><b>[</b><i>file</i><tt>...</tt><b>]</b><tt><br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>sed</i> utility is a stream editor that shall read one or more text files, make editing changes according to a script of
|
|
editing commands, and write the results to standard output. The script shall be obtained from either the <i>script</i> operand
|
|
string or a combination of the option-arguments from the <b>-e</b> <i>script</i> and <b>-f</b> <i>script_file</i> options.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_04"></a>OPTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>sed</i> utility shall conform to the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/xbd_chap12.html#tag_12_02">Section 12.2, Utility Syntax Guidelines</a>, except that the order of presentation of the
|
|
<b>-e</b> and <b>-f</b> options is significant.</p>
|
|
|
|
<p>The following options shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><b>-e </b> <i>script</i></dt>
|
|
|
|
<dd>Add the editing commands specified by the <i>script</i> option-argument to the end of the script of editing commands. The
|
|
<i>script</i> option-argument shall have the same properties as the <i>script</i> operand, described in the OPERANDS section.</dd>
|
|
|
|
<dt><b>-f </b> <i>script_file</i></dt>
|
|
|
|
<dd>Add the editing commands in the file <i>script_file</i> to the end of the script.</dd>
|
|
|
|
<dt><b>-n</b></dt>
|
|
|
|
<dd>Suppress the default output (in which each line, after it is examined for editing, is written to standard output). Only lines
|
|
explicitly selected for output are written.</dd>
|
|
</dl>
|
|
|
|
<p>Multiple <b>-e</b> and <b>-f</b> options may be specified. All commands shall be added to the script in the order specified,
|
|
regardless of their origin.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_05"></a>OPERANDS</h4>
|
|
|
|
<blockquote>
|
|
<p>The following operands shall be supported:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>file</i></dt>
|
|
|
|
<dd>A pathname of a file whose contents are read and edited. If multiple <i>file</i> operands are specified, the named files shall
|
|
be read in the order specified and the concatenation shall be edited. If no <i>file</i> operands are specified, the standard input
|
|
shall be used.</dd>
|
|
|
|
<dt><i>script</i></dt>
|
|
|
|
<dd>A string to be used as the script of editing commands. The application shall not present a <i>script</i> that violates the
|
|
restrictions of a text file except that the final character need not be a <newline>.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_06"></a>STDIN</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard input shall be used only if no <i>file</i> operands are specified. See the INPUT FILES section.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_07"></a>INPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>The input files shall be text files. The <i>script_file</i>s named by the <b>-f</b> option shall consist of editing
|
|
commands.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_08"></a>ENVIRONMENT VARIABLES</h4>
|
|
|
|
<blockquote>
|
|
<p>The following environment variables shall affect the execution of <i>sed</i>:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>LANG</i></dt>
|
|
|
|
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
|
|
IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
|
|
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
|
|
|
|
<dt><i>LC_ALL</i></dt>
|
|
|
|
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
|
|
|
|
<dt><i>LC_COLLATE</i></dt>
|
|
|
|
<dd><br>
|
|
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular
|
|
expressions.</dd>
|
|
|
|
<dt><i>LC_CTYPE</i></dt>
|
|
|
|
<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
|
|
opposed to multi-byte characters in arguments and input files), and the behavior of character classes within regular
|
|
expressions.</dd>
|
|
|
|
<dt><i>LC_MESSAGES</i></dt>
|
|
|
|
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
|
|
error.</dd>
|
|
|
|
<dt><i>NLSPATH</i></dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
|
|
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
|
|
"[Option End]" border="0"></dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_09"></a>ASYNCHRONOUS EVENTS</h4>
|
|
|
|
<blockquote>
|
|
<p>Default.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_10"></a>STDOUT</h4>
|
|
|
|
<blockquote>
|
|
<p>The input files shall be written to standard output, with the editing commands specified in the script applied. If the <b>-n</b>
|
|
option is specified, only those input lines selected by the script shall be written to standard output.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_11"></a>STDERR</h4>
|
|
|
|
<blockquote>
|
|
<p>The standard error shall be used only for diagnostic messages.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_12"></a>OUTPUT FILES</h4>
|
|
|
|
<blockquote>
|
|
<p>The output files shall be text files whose formats are dependent on the editing commands given.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_04_126_13"></a>EXTENDED DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>script</i> shall consist of editing commands of the following form:</p>
|
|
|
|
<pre>
|
|
<b>[</b><i>address</i><b>[</b><tt>,</tt><i>address</i><b>]]</b><i>function</i>
|
|
</pre>
|
|
|
|
<p>where <i>function</i> represents a single-character command verb from the list in <a href="#tag_04_126_13_03">Editing Commands
|
|
in sed</a> , followed by any applicable arguments.</p>
|
|
|
|
<p>The command can be preceded by <blank>s and/or semicolons. The function can be preceded by <blank>s. These optional
|
|
characters shall have no effect.</p>
|
|
|
|
<p>In default operation, <i>sed</i> cyclically shall append a line of input, less its terminating <newline>, into the pattern
|
|
space. Normally the pattern space will be empty, unless a <b>D</b> command terminated the last cycle. The <i>sed</i> utility shall
|
|
then apply in sequence all commands whose addresses select that pattern space, and at the end of the script copy the pattern space
|
|
to standard output (except when <b>-n</b> is specified) and delete the pattern space. Whenever the pattern space is written to
|
|
standard output or a named file, <i>sed</i> shall immediately follow it with a <newline>.</p>
|
|
|
|
<p>Some of the editing commands use a hold space to save all or part of the pattern space for subsequent retrieval. The pattern and
|
|
hold spaces shall each be able to hold at least 8192 bytes.</p>
|
|
|
|
<h5><a name="tag_04_126_13_01"></a>Addresses in sed</h5>
|
|
|
|
<p>An address is either a decimal number that counts input lines cumulatively across files, a <tt>'$'</tt> character that addresses
|
|
the last line of input, or a context address (which consists of a BRE, as described in <a href="#tag_04_126_13_02">Regular
|
|
Expressions in sed</a> , preceded and followed by a delimiter, usually a slash).</p>
|
|
|
|
<p>An editing command with no addresses shall select every pattern space.</p>
|
|
|
|
<p>An editing command with one address shall select each pattern space that matches the address.</p>
|
|
|
|
<p>An editing command with two addresses shall select the inclusive range from the first pattern space that matches the first
|
|
address through the next pattern space that matches the second. (If the second address is a number less than or equal to the line
|
|
number first selected, only one line shall be selected.) Starting at the first line following the selected range, <i>sed</i> shall
|
|
look again for the first address. Thereafter, the process shall be repeated. Omitting either or both of the address components in
|
|
the following form produces undefined results:</p>
|
|
|
|
<pre>
|
|
<b>[</b><i>address</i><b>[</b><tt>,</tt><i>address</i><b>]]</b>
|
|
</pre>
|
|
|
|
<h5><a name="tag_04_126_13_02"></a>Regular Expressions in sed</h5>
|
|
|
|
<p>The <i>sed</i> utility shall support the BREs described in the Base Definitions volume of IEEE Std 1003.1-2001, <a
|
|
href="../basedefs/xbd_chap09.html#tag_09_03">Section 9.3, Basic Regular Expressions</a>, with the following additions:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>In a context address, the construction <tt>"\cBREc"</tt> , where <i>c</i> is any character other than backslash or
|
|
<newline>, shall be identical to <tt>"/BRE/"</tt> . If the character designated by <i>c</i> appears following a backslash,
|
|
then it shall be considered to be that literal character, which shall not terminate the BRE. For example, in the context address
|
|
<tt>"\xabc\xdefx"</tt> , the second <i>x</i> stands for itself, so that the BRE is <tt>"abcxdef"</tt> .</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The escape sequence <tt>'\n'</tt> shall match a <newline> embedded in the pattern space. A literal <newline> shall
|
|
not be used in the BRE of a context address or in the substitute function.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>If an RE is empty (that is, no pattern is specified) <i>sed</i> shall behave as if the last RE used in the last command applied
|
|
(either as an address or as part of a substitute command) was specified.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<h5><a name="tag_04_126_13_03"></a>Editing Commands in sed</h5>
|
|
|
|
<p>In the following list of editing commands, the maximum number of permissible addresses for each function is indicated by [
|
|
<i>0addr</i>], [ <i>1addr</i>], or [ <i>2addr</i>], representing zero, one, or two addresses.</p>
|
|
|
|
<p>The argument <i>text</i> shall consist of one or more lines. Each embedded <newline> in the text shall be preceded by a
|
|
backslash. Other backslashes in text shall be removed, and the following character shall be treated literally.</p>
|
|
|
|
<p>The <b>r</b> and <b>w</b> command verbs, and the <i>w</i> flag to the <b>s</b> command, take an optional <i>rfile</i> (or
|
|
<i>wfile</i>) parameter, separated from the command verb letter or flag by one or more <blank>s; implementations may allow
|
|
zero separation as an extension.</p>
|
|
|
|
<p>The argument <i>rfile</i> or the argument <i>wfile</i> shall terminate the editing command. Each <i>wfile</i> shall be created
|
|
before processing begins. Implementations shall support at least ten <i>wfile</i> arguments in the script; the actual number
|
|
(greater than or equal to 10) that is supported by the implementation is unspecified. The use of the <i>wfile</i> parameter shall
|
|
cause that file to be initially created, if it does not exist, or shall replace the contents of an existing file.</p>
|
|
|
|
<p>The <b>b</b>, <b>r</b>, <b>s</b>, <b>t</b>, <b>w</b>, <b>y</b>, and <b>:</b> command verbs shall accept additional arguments.
|
|
The following synopses indicate which arguments shall be separated from the command verbs by a single <space>.</p>
|
|
|
|
<p>The <b>a</b> and <b>r</b> commands schedule text for later output. The text specified for the <b>a</b> command, and the contents
|
|
of the file specified for the <b>r</b> command, shall be written to standard output just before the next attempt to fetch a line of
|
|
input when executing the <b>N</b> or <b>n</b> commands, or when reaching the end of the script. If written when reaching the end of
|
|
the script, and the <b>-n</b> option was not specified, the text shall be written after copying the pattern space to standard
|
|
output. The contents of the file specified for the <b>r</b> command shall be as of the time the output is written, not the time the
|
|
<b>r</b> command is applied. The text shall be output in the order in which the <b>a</b> and <b>r</b> commands were applied to the
|
|
input.</p>
|
|
|
|
<p>Command verbs other than <b>{</b>, <b>a</b>, <b>b</b>, <b>c</b>, <b>i</b>, <b>r</b>, <b>t</b>, <b>w</b>, <b>:</b>, and <b>#</b>
|
|
can be followed by a semicolon, optional <blank>s, and another command verb. However, when the <b>s</b> command verb is used
|
|
with the <i>w</i> flag, following it with another command in this manner produces undefined results.</p>
|
|
|
|
<p>A function can be preceded by one or more <tt>'!'</tt> characters, in which case the function shall be applied if the addresses
|
|
do not select the pattern space. Zero or more <blank>s shall be accepted before the first <tt>'!'</tt> character. It is
|
|
unspecified whether <blank>s can follow a <tt>'!'</tt> character, and conforming applications shall not follow a <tt>'!'</tt>
|
|
character with <blank>s.</p>
|
|
|
|
<dl compact>
|
|
<dt><b>[</b><i>2addr</i><b>] {</b><i>function</i></dt>
|
|
|
|
<dt><i>function</i></dt>
|
|
|
|
<dt>...</dt>
|
|
|
|
<dt><b>}</b></dt>
|
|
|
|
<dd>Execute a list of <i>sed</i> functions only when the pattern space is selected. The list of <i>sed</i> functions shall be
|
|
surrounded by braces and separated by <newline>s, and conform to the following rules. The braces can be preceded or followed
|
|
by <blank>s. The functions can be preceded by <blank>s, but shall not be followed by <blank>s. The
|
|
<right-brace> shall be preceded by a <newline> and can be preceded or followed by <blank>s.</dd>
|
|
|
|
<dt><b>[</b><i>1addr</i><b>]a\</b></dt>
|
|
|
|
<dt><i>text</i></dt>
|
|
|
|
<dd>Write text to standard output as described previously.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]b [</b><i>label</i><b>]</b></dt>
|
|
|
|
<dd><br>
|
|
Branch to the <b>:</b> function bearing the <i>label</i>. If <i>label</i> is not specified, branch to the end of the script. The
|
|
implementation shall support <i>label</i>s recognized as unique up to at least 8 characters; the actual length (greater than or
|
|
equal to 8) that shall be supported by the implementation is unspecified. It is unspecified whether exceeding a label length causes
|
|
an error or a silent truncation.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]c\</b></dt>
|
|
|
|
<dt><i>text</i></dt>
|
|
|
|
<dd>Delete the pattern space. With a 0 or 1 address or at the end of a 2-address range, place <i>text</i> on the output and start
|
|
the next cycle.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]d</b></dt>
|
|
|
|
<dd>Delete the pattern space and start the next cycle.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]D</b></dt>
|
|
|
|
<dd>Delete the initial segment of the pattern space through the first <newline> and start the next cycle.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]g</b></dt>
|
|
|
|
<dd>Replace the contents of the pattern space by the contents of the hold space.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]G</b></dt>
|
|
|
|
<dd>Append to the pattern space a <newline> followed by the contents of the hold space.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]h</b></dt>
|
|
|
|
<dd>Replace the contents of the hold space with the contents of the pattern space.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]H</b></dt>
|
|
|
|
<dd>Append to the hold space a <newline> followed by the contents of the pattern space.</dd>
|
|
|
|
<dt><b>[</b><i>1addr</i><b>]i\</b></dt>
|
|
|
|
<dt><i>text</i></dt>
|
|
|
|
<dd>Write <i>text</i> to standard output.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]l</b></dt>
|
|
|
|
<dd>(The letter ell.) Write the pattern space to standard output in a visually unambiguous form. The characters listed in the Base
|
|
Definitions volume of IEEE Std 1003.1-2001, Table 5-1, Escape Sequences and Associated Actions ( <tt>'\\'</tt> ,
|
|
<tt>'\a'</tt> , <tt>'\b'</tt> , <tt>'\f'</tt> , <tt>'\r'</tt> , <tt>'\t'</tt> , <tt>'\v'</tt> ) shall be written as the
|
|
corresponding escape sequence; the <tt>'\n'</tt> in that table is not applicable. Non-printable characters not in that table shall
|
|
be written as one three-digit octal number (with a preceding backslash) for each byte in the character (most significant byte
|
|
first). If the size of a byte on the system is greater than 9 bits, the format used for non-printable characters is
|
|
implementation-defined.
|
|
|
|
<p>Long lines shall be folded, with the point of folding indicated by writing a backslash followed by a <newline>; the length
|
|
at which folding occurs is unspecified, but should be appropriate for the output device. The end of each line shall be marked with
|
|
a <tt>'$'</tt> .</p>
|
|
</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]n</b></dt>
|
|
|
|
<dd>Write the pattern space to standard output if the default output has not been suppressed, and replace the pattern space with
|
|
the next line of input, less its terminating <newline>.
|
|
|
|
<p>If no next line of input is available, the <b>n</b> command verb shall branch to the end of the script and quit without starting
|
|
a new cycle.</p>
|
|
</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]N</b></dt>
|
|
|
|
<dd>Append the next line of input, less its terminating <newline>, to the pattern space, using an embedded <newline> to
|
|
separate the appended material from the original material. Note that the current line number changes.
|
|
|
|
<p>If no next line of input is available, the <b>N</b> command verb shall branch to the end of the script and quit without starting
|
|
a new cycle or copying the pattern space to standard output.</p>
|
|
</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]p</b></dt>
|
|
|
|
<dd>Write the pattern space to standard output.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]P</b></dt>
|
|
|
|
<dd>Write the pattern space, up to the first <newline>, to standard output.</dd>
|
|
|
|
<dt><b>[</b><i>1addr</i><b>]q</b></dt>
|
|
|
|
<dd>Branch to the end of the script and quit without starting a new cycle.</dd>
|
|
|
|
<dt><b>[</b><i>1addr</i><b>]r </b> <i>rfile</i></dt>
|
|
|
|
<dd>Copy the contents of <i>rfile</i> to standard output as described previously. If <i>rfile</i> does not exist or cannot be read,
|
|
it shall be treated as if it were an empty file, causing no error condition.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]s/</b><i>BRE</i><b>/</b><i>replacement</i><b>/</b><i>flags</i></dt>
|
|
|
|
<dd><br>
|
|
Substitute the replacement string for instances of the BRE in the pattern space. Any character other than backslash or
|
|
<newline> can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE
|
|
delimiter itself can be used as a literal character if it is preceded by a backslash.
|
|
|
|
<p>The replacement string shall be scanned from beginning to end. An ampersand ( <tt>'&'</tt> ) appearing in the replacement
|
|
shall be replaced by the string matching the BRE. The special meaning of <tt>'&'</tt> in this context can be suppressed by
|
|
preceding it by a backslash. The characters <tt>"\</tt><i>n"</i>, where <i>n</i> is a digit, shall be replaced by the text matched
|
|
by the corresponding backreference expression. The special meaning of <tt>"\</tt><i>n"</i> where <i>n</i> is a digit in this
|
|
context, can be suppressed by preceding it by a backslash. For each other backslash ( <tt>'\'</tt> ) encountered, the following
|
|
character shall lose its special meaning (if any). The meaning of a <tt>'\'</tt> immediately followed by any character other than
|
|
<tt>'&'</tt> , <tt>'\'</tt> , a digit, or the delimiter character used for this command, is unspecified.</p>
|
|
|
|
<p>A line can be split by substituting a <newline> into it. The application shall escape the <newline> in the
|
|
replacement by preceding it by a backslash. A substitution shall be considered to have been performed even if the replacement
|
|
string is identical to the string that it replaces. Any backslash used to alter the default meaning of a subsequent character shall
|
|
be discarded from the BRE or the replacement before evaluating the BRE or using the replacement.</p>
|
|
|
|
<p>The value of <i>flags</i> shall be zero or more of:</p>
|
|
|
|
<dl compact>
|
|
<dt><i>n</i></dt>
|
|
|
|
<dd>Substitute for the <i>n</i>th occurrence only of the BRE found within the pattern space.</dd>
|
|
|
|
<dt><b>g</b></dt>
|
|
|
|
<dd>Globally substitute for all non-overlapping instances of the BRE rather than just the first one. If both <b>g</b> and <i>n</i>
|
|
are specified, the results are unspecified.</dd>
|
|
|
|
<dt><b>p</b></dt>
|
|
|
|
<dd>Write the pattern space to standard output if a replacement was made.</dd>
|
|
|
|
<dt><b>w </b> <i>wfile</i></dt>
|
|
|
|
<dd>Write. Append the pattern space to <i>wfile</i> if a replacement was made. A conforming application shall precede the
|
|
<i>wfile</i> argument with one or more <blank>s. If the <b>w</b> flag is not the last flag value given in a concatenation of
|
|
multiple flag values, the results are undefined.</dd>
|
|
</dl>
|
|
</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]t [</b><i>label</i><b>]</b></dt>
|
|
|
|
<dd><br>
|
|
Test. Branch to the <b>:</b> command verb bearing the <i>label</i> if any substitutions have been made since the most recent
|
|
reading of an input line or execution of a <b>t</b>. If <i>label</i> is not specified, branch to the end of the script.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]w </b> <i>wfile</i></dt>
|
|
|
|
<dd><br>
|
|
Append (write) the pattern space to <i>wfile</i>.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]x</b></dt>
|
|
|
|
<dd>Exchange the contents of the pattern and hold spaces.</dd>
|
|
|
|
<dt><b>[</b><i>2addr</i><b>]y/</b><i>string1</i><b>/</b><i>string2</i><b>/</b></dt>
|
|
|
|
<dd><br>
|
|
Replace all occurrences of characters in <i>string1</i> with the corresponding characters in <i>string2</i>. If a backslash
|
|
followed by an <tt>'n'</tt> appear in <i>string1</i> or <i>string2</i>, the two characters shall be handled as a single
|
|
<newline>. If the number of characters in <i>string1</i> and <i>string2</i> are not equal, or if any of the characters in
|
|
<i>string1</i> appear more than once, the results are undefined. Any character other than backslash or <newline> can be used
|
|
instead of slash to delimit the strings. If the delimiter is not <i>n</i>, within <i>string1</i> and <i>string2</i>, the delimiter
|
|
itself can be used as a literal character if it is preceded by a backslash. If a backslash character is immediately followed by a
|
|
backslash character in <i>string1</i> or <i>string2</i>, the two backslash characters shall be counted as a single literal
|
|
backslash character. The meaning of a backslash followed by any character that is not <tt>'n'</tt> , a backslash, or the delimiter
|
|
character is undefined.</dd>
|
|
|
|
<dt><b>[</b><i>0addr</i><b>]:</b><i>label</i></dt>
|
|
|
|
<dd>Do nothing. This command bears a <i>label</i> to which the <b>b</b> and <b>t</b> commands branch.</dd>
|
|
|
|
<dt><b>[</b><i>1addr</i><b>]=</b></dt>
|
|
|
|
<dd>Write the following to standard output:
|
|
|
|
<pre>
|
|
<tt>"%d\n", <</tt><i>current line number</i><tt>>
|
|
</tt>
|
|
</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 first
|
|
two 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>s
|
|
appear 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 excess
|
|
blank 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, matching
|
|
historical practice on many implementations. Implementations are encouraged to support more, but conforming applications should not
|
|
exceed 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 a
|
|
bug.</p>
|
|
|
|
<p>The manner in which the <b>l</b> command writes non-printable characters was changed to avoid the historical
|
|
backspace-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 8192
|
|
bytes, larger than the 4000 bytes spaces used by some historical implementations, but less than the 20480 bytes limit used in an
|
|
early 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 early
|
|
proposals 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 documentation
|
|
of 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, but
|
|
matches BSD-derived implementations. The comment character is treated as a command, and it has the same properties in terms of
|
|
being 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 have
|
|
behaved in unexpected ways if this were not the case. The standard developers considered that this was incorrect behavior and that
|
|
application developers should not have to avoid this feature. A correct implementation of this volume of
|
|
IEEE 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 before
|
|
any <b>-f</b> options. This has been changed to process them in the order presented because it matches historical practice and is
|
|
more 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 default
|
|
output 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 should
|
|
write two lines with <b>A</b>, whereas the second should write one. Some System V systems write the <b>A</b> only once in both
|
|
examples 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 the
|
|
behavior to be unspecified. The SVID/BSD/System V documentation behavior was adopted for this volume of
|
|
IEEE 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 and
|
|
the <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 this
|
|
ignores valid scripts that interrupt the cyclical nature of the processing through the use of the <b>D</b>, <b>d</b>, <b>q</b>, or
|
|
branching commands. Such scripts rely on the <b>p</b> suffix to write the pattern space because they do not make use of the default
|
|
output 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> scripts
|
|
were written to run both with and without the <b>-n</b> option. This is believed to be unlikely. It is even more unlikely that
|
|
programmers have coded the <b>p</b> flag expecting it to be unnecessary. Because the interaction was not documented, the likelihood
|
|
of 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 to
|
|
diagnose 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. This
|
|
limit has been removed because there is no reason an editor processing lines of {LINE_MAX} length should have this restriction. The
|
|
command <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 trailing
|
|
white 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 it
|
|
could theoretically break working scripts. Implementors are encouraged to provide warning messages about labels that are never used
|
|
or 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 a
|
|
semicolon 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 replacement
|
|
string 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>
|
|
|