<p>This document is released under the terms of the GNU Free Documentation
License as published by the Free Software Foundation; either version 1.1, or
(at your option) any later version.
<p>You should have received a copy of the GNU Free Documentation License along
with <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>; see the file <samp><span class="file">COPYING.DOC</span></samp>. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
<p>There are no Cover Texts and no Invariant Sections; this text, along
with its equivalent in the printed manual, constitutes the Title Page.
<li><a accesskey="4" href="#Examples">Examples</a>: Some sample scripts
<li><a accesskey="5" href="#Limitations">Limitations</a>: Limitations and (non-)limitations of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
<li><a accesskey="6" href="#Other-Resources">Other Resources</a>: Other resources for learning about <samp><span class="command">sed</span></samp>
<p><samp><span class="command">sed</span></samp> may be invoked with the following command-line options:
<dl>
<dt><code>-V</code><dt><code>--version</code><dd><a name="index-g_t_002dV-2"></a><a name="index-g_t_002d_002dversion-3"></a><a name="index-Version_002c-printing-4"></a>Print out the version of <samp><span class="command">sed</span></samp> that is being run and a copyright notice,
then exit.
<br><dt><code>-h</code><dt><code>--help</code><dd><a name="index-g_t_002dh-5"></a><a name="index-g_t_002d_002dhelp-6"></a><a name="index-Usage-summary_002c-printing-7"></a>Print a usage message briefly summarizing these command-line options
and the bug-reporting address,
then exit.
<br><dt><code>-n</code><dt><code>--quiet</code><dt><code>--silent</code><dd><a name="index-g_t_002dn-8"></a><a name="index-g_t_002d_002dquiet-9"></a><a name="index-g_t_002d_002dsilent-10"></a><a name="index-Disabling-autoprint_002c-from-command-line-11"></a>By default, <samp><span class="command">sed</span></samp> prints out the pattern space
at the end of each cycle through the script.
These options disable this automatic printing,
and <samp><span class="command">sed</span></samp> only produces output when explicitly told to
via the <code>p</code> command.
<br><dt><code>-i[</code><var>SUFFIX</var><code>]</code><dt><code>--in-place[=</code><var>SUFFIX</var><code>]</code><dd><a name="index-g_t_002di-12"></a><a name="index-g_t_002d_002din_002dplace-13"></a><a name="index-In_002dplace-editing-14"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-in_002dplace-editing-15"></a>This option specifies that files are to be edited in-place.
<acronym>GNU</acronym> <samp><span class="command">sed</span></samp> does this by creating a temporary file and
sending output to this file rather than to the standard
<br><dt><code>-l </code><var>N</var><dt><code>--line-length=</code><var>N</var><dd><a name="index-g_t_002dl-16"></a><a name="index-g_t_002d_002dline_002dlength-17"></a><a name="index-Line-length_002c-setting-18"></a>Specify the default line-wrap length for the <code>l</code> command.
A length of 0 (zero) means to never wrap long lines. If
not specified, it is taken to be 70.
<br><dt><code>-r</code><dt><code>--regexp-extended</code><dd><a name="index-g_t_002dr-19"></a><a name="index-g_t_002d_002dregexp_002dextended-20"></a><a name="index-Extended-regular-expressions_002c-choosing-21"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-extended-regular-expressions-22"></a>Use extended regular expressions rather than basic
regular expressions. Extended regexps are those that
<samp><span class="command">egrep</span></samp> accepts; they can be clearer because they
usually have less backslashes, but are a <acronym>GNU</acronym> extension
and hence scripts that use them are not portable.
See <a href="#Extended-regexps">Extended regular expressions</a>.
<br><dt><code>-s</code><dt><code>--separate</code><dd><a name="index-Working-on-separate-files-23"></a>By default, <samp><span class="command">sed</span></samp> will consider the files specified on the
command line as a single continuous long stream. This <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
extension allows the user to consider them as separate files:
range addresses (such as `<samp><span class="samp">/abc/,/def/</span></samp>') are not allowed
to span several files, line numbers are relative to the start
of each file, <code>$</code> refers to the last line of each file,
and files invoked from the <code>R</code> commands are rewound at the
start of each file.
<br><dt><code>-u</code><dt><code>--unbuffered</code><dd><a name="index-g_t_002du-24"></a><a name="index-g_t_002d_002dunbuffered-25"></a><a name="index-Unbuffered-I_002fO_002c-choosing-26"></a>Buffer both input and output as minimally as practical.
(This is particularly useful if the input is coming from
the likes of `<samp><span class="samp">tail -f</span></samp>', and you wish to see the transformed
output as soon as possible.)
<br><dt><code>-e </code><var>script</var><dt><code>--expression=</code><var>script</var><dd><a name="index-g_t_002de-27"></a><a name="index-g_t_002d_002dexpression-28"></a><a name="index-Script_002c-from-command-line-29"></a>Add the commands in <var>script</var> to the set of commands to be
run while processing the input.
<br><dt><code>-f </code><var>script-file</var><dt><code>--file=</code><var>script-file</var><dd><a name="index-g_t_002df-30"></a><a name="index-g_t_002d_002dfile-31"></a><a name="index-Script_002c-from-a-file-32"></a>Add the commands contained in the file <var>script-file</var>
to the set of commands to be run while processing the input.
</dl>
<p>If no <samp><span class="option">-e</span></samp>, <samp><span class="option">-f</span></samp>, <samp><span class="option">--expression</span></samp>, or <samp><span class="option">--file</span></samp>
options are given on the command-line,
then the first non-option argument on the command line is
taken to be the <var>script</var> to be executed.
<p><a name="index-Files-to-be-processed-as-input-33"></a>If any command-line parameters remain after processing the above,
these parameters are interpreted as the names of input files to
be processed.
<a name="index-Standard-input_002c-processing-as-input-34"></a>A file name of `<samp><span class="samp">-</span></samp>' refers to the standard input stream.
The standard input will be processed if no file names are specified.
<p><a name="index-g_t_0040command_007bsed_007d-program-structure-35"></a><a name="index-Script-structure-36"></a>A <samp><span class="command">sed</span></samp> program consists of one or more <samp><span class="command">sed</span></samp> commands,
passed in by one or more of the
<samp><span class="option">-e</span></samp>, <samp><span class="option">-f</span></samp>, <samp><span class="option">--expression</span></samp>, and <samp><span class="option">--file</span></samp>
options, or the first non-option argument if zero of these
options are used.
This document will refer to “the” <samp><span class="command">sed</span></samp> script;
this is understood to mean the in-order catenation
of all of the <var>script</var>s and <var>script-file</var>s passed in.
<p>Each <code>sed</code> command consists of an optional address or
address range, followed by a one-character command name
and any additional command-specific code.
<ul class="menu">
<li><a accesskey="1" href="#Addresses">Addresses</a>: Selecting lines with <samp><span class="command">sed</span></samp>
<li><a accesskey="2" href="#Regular-Expressions">Regular Expressions</a>: Overview of regular expression syntax
<li><a accesskey="3" href="#Data-Spaces">Data Spaces</a>: Where <samp><span class="command">sed</span></samp> buffers data
<li><a accesskey="4" href="#Common-Commands">Common Commands</a>: Often used commands
<li><a accesskey="5" href="#The-_0022s_0022-Command">The "s" Command</a>: <samp><span class="command">sed</span></samp>'s Swiss Army Knife
<li><a accesskey="6" href="#Other-Commands">Other Commands</a>: Less frequently used commands
<li><a accesskey="7" href="#Programming-Commands">Programming Commands</a>: Commands for <samp><span class="command">sed</span></samp> gurus
<li><a accesskey="8" href="#Extended-Commands">Extended Commands</a>: Commands specific of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
<li><a accesskey="9" href="#Escapes">Escapes</a>: Specifying special characters
Addresses in a <samp><span class="command">sed</span></samp> script can be in any of the following forms:
<dl>
<dt><var>number</var><dd><a name="index-Address_002c-numeric-40"></a><a name="index-Line_002c-selecting-by-number-41"></a>Specifying a line number will match only that line in the input.
(Note that <samp><span class="command">sed</span></samp> counts lines continuously across all input files
unless <samp><span class="option">-i</span></samp> or <samp><span class="option">-s</span></samp> options are specified.)
<br><dt><var>first</var><code>~</code><var>step</var><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040samp_007b_0040var_007bn_007d_007e_0040var_007bm_007d_007d-addresses-42"></a>This <acronym>GNU</acronym> extension matches every <var>step</var>th line
starting with line <var>first</var>.
In particular, lines will be selected when there exists
a non-negative <var>n</var> such that the current line-number equals
to pick every third line starting with the second, `<samp><span class="samp">2~3</span></samp>' would be used;
to pick every fifth line starting with the tenth, use `<samp><span class="samp">10~5</span></samp>';
and `<samp><span class="samp">50~0</span></samp>' is just an obscure way of saying <code>50</code>.
<br><dt><code>$</code><dd><a name="index-Address_002c-last-line-43"></a><a name="index-Last-line_002c-selecting-44"></a><a name="index-Line_002c-selecting-last-45"></a>This address matches the last line of the last file of input, or
the last line of each file when the <samp><span class="option">-i</span></samp> or <samp><span class="option">-s</span></samp> options
are specified.
<br><dt><code>/</code><var>regexp</var><code>/</code><dd><a name="index-Address_002c-as-a-regular-expression-46"></a><a name="index-Line_002c-selecting-by-regular-expression-match-47"></a>This will select any line which matches the regular expression <var>regexp</var>.
If <var>regexp</var> itself includes any <code>/</code> characters,
each must be escaped by a backslash (<code>\</code>).
<p><a name="index-empty-regular-expression-48"></a><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-empty-regular-expression-49"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-modifiers-and-the-empty-regular-expression-50"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-modifiers-and-the-empty-regular-expression-51"></a>Unless <code>POSIXLY_CORRECT</code> is set, the empty regular expression
`<samp><span class="samp">//</span></samp>' repeats the last regular expression match (the same holds
if the empty regular expression is passed to the <code>s</code> command).
Note that modifiers to regular expressions are evaluated
when the regular expression is compiled, thus it is illegal to specify
them together with the empty regular expression.
<!-- *** CHECK CURRENT POSIX, I'M NOT SURE THIS IS STILL TRUE. ADR. -->
If <code>POSIXLY_CORRECT</code> is set, instead, `<samp><span class="samp">//</span></samp>' is the null match:
this behavior is mandated by <span class="sc">posix</span>, but it would break too many legacy
<br><dt><code>\%</code><var>regexp</var><code>%</code><dd>(The <code>%</code> may be replaced by any other single character.)
<p><a name="index-Slash-character_002c-in-regular-expressions-52"></a>This also matches the regular expression <var>regexp</var>,
but allows one to use a different delimiter than <code>/</code>.
This is particularly useful if the <var>regexp</var> itself contains
a lot of slashes, since it avoids the tedious escaping of every <code>/</code>.
If <var>regexp</var> itself includes any delimiter characters,
each must be escaped by a backslash (<code>\</code>).
<br><dt><code>/</code><var>regexp</var><code>/I</code><dt><code>\%</code><var>regexp</var><code>%I</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-53"></a>The <code>I</code> modifier to regular-expression matching is a <acronym>GNU</acronym>
extension which causes the <var>regexp</var> to be matched in
a case-insensitive manner.
<br><dt><code>/</code><var>regexp</var><code>/M</code><dt><code>\%</code><var>regexp</var><code>%M</code><dd><a name="index-Perl_002dstyle-regular-expressions_002c-multiline-54"></a>The <code>M</code> modifier to regular-expression matching is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
extension which causes <code>^</code> and <code>$</code> to match respectively
(in addition to the normal behavior) the empty string after a newline,
and the empty string before a newline. There are special character
sequences
(<code>\`</code> and <code>\'</code>)
which always match the beginning or the end of the buffer.
<code>M</code> stands for <cite>multi-line</cite>.
</dl>
<p>If no addresses are given, then all lines are matched;
if one address is given, then only lines matching that
address are matched.
<p><a name="index-Range-of-lines-55"></a><a name="index-Several-lines_002c-selecting-56"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007b0_007d-address-57"></a>An address range can be specified by specifying two addresses
separated by a comma (<code>,</code>).
An address range matches lines starting from where the first
address matches, and continues until the second address matches
(inclusively).
If the second address is a <var>regexp</var>, then checking for the
ending match will start with the line <em>following</em> the
line which matched the first address. As a <acronym>GNU</acronym> extension, a
line number of <code>0</code> can be used in an address specification
like `<samp><span class="samp">0,/</span><var>regexp</var><span class="samp">/</span></samp>' so that <var>regexp</var> will be
matched in the first input line too.
<p>If the second address is a <var>number</var> less than (or equal to)
the line matching the first address,
then only the one line is matched.
<p><a name="index-Special-addressing-forms-58"></a><a name="index-Range-with-start-address-of-zero-59"></a><a name="index-Zero_002c-as-range-start-address-60"></a><a name="index-g_t_0040var_007baddr1_007d_002c_002bN-61"></a><a name="index-g_t_0040var_007baddr1_007d_002c_007eN-62"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-two_002daddress-forms-63"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-0_002c_0040var_007baddr2_007d-addressing-64"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_002b_0040var_007bN_007d-addressing-65"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_007e_0040var_007bN_007d-addressing-66"></a><acronym>GNU</acronym> <samp><span class="command">sed</span></samp> also supports some special two-address forms:
<dl>
<dt><code>0,</code><var>addr2</var><dd>Start out in “matched first address” state,
until <var>addr2</var> is found.
This is similar to `<samp><span class="samp">1,</span><var>addr2</var></samp>',
except that if <var>addr2</var> matches the very first line of input
the 0,<var>addr2</var> form will be at the end of its range,
whereas the 1,<var>addr2</var> form will still be at the beginning of its range.
<br><dt><var>addr1</var><code>,+</code><var>N</var><dd>Matches <var>addr1</var> and the <var>N</var> lines following <var>addr1</var>.
<br><dt><var>addr1</var><code>,~</code><var>N</var><dd>Matches <var>addr1</var> and the lines following <var>addr1</var>
until the next line whose input line number is a multiple of <var>N</var>.
</dl>
<p><a name="index-Excluding-lines-67"></a><a name="index-Selecting-non_002dmatching-lines-68"></a>Appending the <code>!</code> character to the end of an address
specification negates the sense of the match.
That is, if the <code>!</code> character follows an address range,
then only lines which do <em>not</em> match the address range
<h3 class="section">3.2 Overview of Regular Expression Syntax</h3>
<p>To know how to use <samp><span class="command">sed</span></samp>, people should understand regular
expressions (<dfn>regexp</dfn> for short). A regular expression
is a pattern that is matched against a
subject string from left to right. Most characters are
<dfn>ordinary</dfn>: they stand for
themselves in a pattern, and match the corresponding characters
in the subject. As a trivial example, the pattern
<pre class="example"> The quick brown fox
</pre>
<p class="noindent">matches a portion of a subject string that is identical to
itself. The power of regular expressions comes from the
ability to include alternatives and repetitions in the pattern.
These are encoded in the pattern by the use of <dfn>special characters</dfn>,
which do not stand for themselves but instead
are interpreted in some special way. Here is a brief description
of regular expression syntax as used in <samp><span class="command">sed</span></samp>.
<dl>
<dt><var>char</var><dd>A single ordinary character matches itself.
<br><dt><code>*</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-69"></a>Matches a sequence of zero or more instances of matches for the
preceding regular expression, which must be an ordinary character, a
special character preceded by <code>\</code>, a <code>.</code>, a grouped regexp
(see below), or a bracket expression. As a <acronym>GNU</acronym> extension, a
postfixed regular expression can also be followed by <code>*</code>; for
example, <code>a**</code> is equivalent to <code>a*</code>. <acronym>POSIX</acronym>
1003.1-2001 says that <code>*</code> stands for itself when it appears at
the start of a regular expression or subexpression, but many
non<acronym>GNU</acronym> implementations do not support this and portable
scripts should instead use <code>\*</code> in these contexts.
<br><dt><code>\+</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-70"></a>As <code>*</code>, but matches one or more. It is a <acronym>GNU</acronym> extension.
<br><dt><code>\?</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-71"></a>As <code>*</code>, but only matches zero or one. It is a <acronym>GNU</acronym> extension.
<br><dt><code>\{</code><var>i</var><code>\}</code><dd>As <code>*</code>, but matches exactly <var>i</var> sequences (<var>i</var> is a
decimal integer; for portability, keep it between 0 and 255
inclusive).
<br><dt><code>\{</code><var>i</var><code>,</code><var>j</var><code>\}</code><dd>Matches between <var>i</var> and <var>j</var>, inclusive, sequences.
<br><dt><code>\{</code><var>i</var><code>,\}</code><dd>Matches more than or equal to <var>i</var> sequences.
<br><dt><code>\(</code><var>regexp</var><code>\)</code><dd>Groups the inner <var>regexp</var> as a whole, this is used to:
<ul>
<li>Apply postfix operators, like <code>\(abcd\)*</code>:
this will search for zero or more whole sequences
of `<samp><span class="samp">abcd</span></samp>', while <code>abcd*</code> would search
for `<samp><span class="samp">abc</span></samp>' followed by zero or more occurrences
of `<samp><span class="samp">d</span></samp>'. Note that support for <code>\(abcd\)*</code> is required by
<acronym>POSIX</acronym> 1003.1-2001, but many non-<acronym>GNU</acronym>
implementations do not support it and hence it is not universally
portable.
<li>Use back references (see below).
</ul>
<br><dt><code>.</code><dd>Matches any single character, including newline.
<br><dt><code>^</code><dd>Matches the null string at beginning of line, i.e. what
appears after the circumflex must appear at the
beginning of line. <code>^#include</code> will match only
lines where `<samp><span class="samp">#include</span></samp>' is the first thing on line—if
there are spaces before, for example, the match fails.
<code>^</code> acts as a special character only at the beginning
of the regular expression or subexpression (that is, after <code>\(</code>
or <code>\|</code>). Portable scripts should avoid <code>^</code> at the
beginning of a subexpression, though, as <acronym>POSIX</acronym> allows
implementations that treat <code>^</code> as an ordinary character in that
context.
<br><dt><code>$</code><dd>It is the same as <code>^</code>, but refers to end of line.
<code>$</code> also acts as a special character only at the end
of the regular expression or subexpression (that is, before <code>\)</code>
or <code>\|</code>), and its use at the end of a subexpression is not
portable.
<br><dt><code>[</code><var>list</var><code>]</code><dt><code>[^</code><var>list</var><code>]</code><dd>Matches any single character in <var>list</var>: for example,
<code>[aeiou]</code> matches all vowels. A list may include
sequences like <var>char1</var><code>-</code><var>char2</var>, which
matches any character between (inclusive) <var>char1</var>
and <var>char2</var>.
<p>A leading <code>^</code> reverses the meaning of <var>list</var>, so that
it matches any single character <em>not</em> in <var>list</var>. To include
<code>]</code> in the list, make it the first character (after
the <code>^</code> if needed), to include <code>-</code> in the list,
make it the first or last; to include <code>^</code> put
it after the first character.
<p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-bracket-expressions-72"></a>The characters <code>$</code>, <code>*</code>, <code>.</code>, <code>[</code>, and <code>\</code>
are normally not special within <var>list</var>. For example, <code>[\*]</code>
matches either `<samp><span class="samp">\</span></samp>' or `<samp><span class="samp">*</span></samp>', because the <code>\</code> is not
special here. However, strings like <code>[.ch.]</code>, <code>[=a=]</code>, and
<code>[:space:]</code> are special within <var>list</var> and represent collating
symbols, equivalence classes, and character classes, respectively, and
<code>[</code> is therefore special within <var>list</var> when it is followed by
<code>.</code>, <code>=</code>, or <code>:</code>. Special escapes like <code>\n</code> and
<code>\t</code> are recognized within <var>list</var>; this will change in a
future version in <code>POSIXLY_CORRECT</code> mode. See <a href="#Escapes">Escapes</a>.
<br><dt><var>regexp1</var><code>\|</code><var>regexp2</var><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-73"></a>Matches either <var>regexp1</var> or <var>regexp2</var>. Use
parentheses to use complex alternative regular expressions.
The matching process tries each alternative in turn, from
left to right, and the first one that succeeds is used.
It is a <acronym>GNU</acronym> extension.
<br><dt><var>regexp1</var><var>regexp2</var><dd>Matches the concatenation of <var>regexp1</var> and <var>regexp2</var>.
Concatenation binds more tightly than <code>\|</code>, <code>^</code>, and
<code>$</code>, but less tightly than the other regular expression
operators.
<br><dt><code>\</code><var>digit</var><dd>Matches the <var>digit</var>-th <code>\(...\)</code> parenthesized
subexpression in the regular expression. This is called a <dfn>back
reference</dfn>. Subexpressions are implicity numbered by counting
occurrences of <code>\(</code> left-to-right.
<br><dt><code>\n</code><dd>Matches the newline character.
<br><dt><code>\</code><var>char</var><dd>Matches <var>char</var>, where <var>char</var> is one of <code>$</code>,
<code>*</code>, <code>.</code>, <code>[</code>, <code>\</code>, or <code>^</code>.
Note that the only C-like
backslash sequences that you can portably assume to be
interpreted are <code>\n</code> and <code>\\</code>; in particular
<code>\t</code> is not portable, and matches a `<samp><span class="samp">t</span></samp>' under most
implementations of <samp><span class="command">sed</span></samp>, rather than a tab character.
</dl>
<p><a name="index-Greedy-regular-expression-matching-74"></a>Note that the regular expression matcher is greedy, i.e., if
two or more matches are detected, it selects the longest; if
there are two or more selected with the same size, it
<br><dt>`<samp><span class="samp">a*b</span></samp>'<dd>Matches zero or more `<samp><span class="samp">a</span></samp>'s followed by a single
`<samp><span class="samp">b</span></samp>'. For example, `<samp><span class="samp">b</span></samp>' or `<samp><span class="samp">aaaaab</span></samp>'.
<br><dt>`<samp><span class="samp">a\?b</span></samp>'<dd>Matches `<samp><span class="samp">b</span></samp>' or `<samp><span class="samp">ab</span></samp>'.
<br><dt>`<samp><span class="samp">a\+b\+</span></samp>'<dd>Matches one or more `<samp><span class="samp">a</span></samp>'s followed by one or more
`<samp><span class="samp">b</span></samp>'s: `<samp><span class="samp">ab</span></samp>' is the shortest possible match, but
other examples are `<samp><span class="samp">aaaab</span></samp>' or `<samp><span class="samp">abbbbb</span></samp>' or
<br><dt>`<samp><span class="samp">.*</span></samp>'<dt>`<samp><span class="samp">.\+</span></samp>'<dd>These two both match all the characters in a string;
however, the first matches every string (including the empty
string), while the second matches only strings containing
at least one character.
<br><dt>`<samp><span class="samp">^main.*(.*)</span></samp>'<dd>This matches a string starting with `<samp><span class="samp">main</span></samp>',
followed by an opening and closing
parenthesis. The `<samp><span class="samp">n</span></samp>', `<samp><span class="samp">(</span></samp>' and `<samp><span class="samp">)</span></samp>' need not
be adjacent.
<br><dt>`<samp><span class="samp">^#</span></samp>'<dd>This matches a string beginning with `<samp><span class="samp">#</span></samp>'.
<br><dt>`<samp><span class="samp">\\$</span></samp>'<dd>This matches a string ending with a single backslash. The
regexp contains two backslashes for escaping.
<br><dt>`<samp><span class="samp">\$</span></samp>'<dd>Instead, this matches a string consisting of a single dollar sign,
because it is escaped.
<br><dt>`<samp><span class="samp">[a-zA-Z0-9]</span></samp>'<dd>In the C locale, this matches any <acronym>ASCII</acronym> letters or digits.
<br><dt>`<samp><span class="samp">[^ </span><kbd>tab</kbd><span class="samp">]\+</span></samp>'<dd>(Here <kbd>tab</kbd> stands for a single tab character.)
This matches a string of one or more
characters, none of which is a space or a tab.
Usually this means a word.
<br><dt>`<samp><span class="samp">^\(.*\)\n\1$</span></samp>'<dd>This matches a string consisting of two equal substrings separated by
a newline.
<br><dt>`<samp><span class="samp">.\{9\}A$</span></samp>'<dd>This matches nine characters followed by an `<samp><span class="samp">A</span></samp>'.
<br><dt>`<samp><span class="samp">^.\{15\}A</span></samp>'<dd>This matches the start of a string that contains 16 characters,
the last of which is an `<samp><span class="samp">A</span></samp>'.
<h3 class="section">3.3 Where <samp><span class="command">sed</span></samp> Buffers Data</h3>
<p><a name="index-Buffer-spaces_002c-pattern-and-hold-75"></a><a name="index-Spaces_002c-pattern-and-hold-76"></a><a name="index-Pattern-space_002c-definition-77"></a><a name="index-Hold-space_002c-definition-78"></a><samp><span class="command">sed</span></samp> maintains two data buffers: the active <em>pattern</em> space,
and the auxiliary <em>hold</em> space.
In “normal” operation, <samp><span class="command">sed</span></samp> reads in one line from the
input stream and places it in the pattern space.
This pattern space is where text manipulations occur.
The hold space is initially empty, but there are commands
for moving data between the pattern and hold spaces.
<!-- XXX FIXME: explain why this is useful/interesting to know. -->
<p>If you use <samp><span class="command">sed</span></samp> at all, you will quite likely want to know
these commands.
<dl>
<dt><code>#</code><dd>[No addresses allowed.]
<p><a name="index-g_t_0023-_0028comments_0029-79"></a><a name="index-Comments_002c-in-scripts-80"></a>The <code>#</code> character begins a comment;
the comment continues until the next newline.
<p><a name="index-Portability_002c-comments-81"></a>If you are concerned about portability, be aware that
some implementations of <samp><span class="command">sed</span></samp> (which are not <span class="sc">posix</span>
conformant) may only support a single one-line comment,
and then only when the very first character of the script is a <code>#</code>.
<p><a name="index-g_t_002dn_002c-forcing-from-within-a-script-82"></a><a name="index-Caveat-_002d_002d_002d-_0023n-on-first-line-83"></a>Warning: if the first two characters of the <samp><span class="command">sed</span></samp> script
are <code>#n</code>, then the <samp><span class="option">-n</span></samp> (no-autoprint) option is forced.
If you want to put a comment in the first line of your script
and that comment begins with the letter `<samp><span class="samp">n</span></samp>'
and you do not want this behavior,
then be sure to either use a capital `<samp><span class="samp">N</span></samp>',
or place at least one space before the `<samp><span class="samp">n</span></samp>'.
<br><dt><code>q [</code><var>exit-code</var><code>]</code><dd>This command only accepts a single address.
<p><a name="index-q-_0028quit_0029-command-84"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-85"></a><a name="index-Quitting-86"></a>Exit <samp><span class="command">sed</span></samp> without processing any more commands or input.
Note that the current pattern space is printed if auto-print is
not disabled with the <samp><span class="option">-n</span></samp> options. The ability to return
an exit code from the <samp><span class="command">sed</span></samp> script is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
<br><dt><code>d</code><dd><a name="index-d-_0028delete_0029-command-87"></a><a name="index-Text_002c-deleting-88"></a>Delete the pattern space;
immediately start next cycle.
<br><dt><code>p</code><dd><a name="index-p-_0028print_0029-command-89"></a><a name="index-Text_002c-printing-90"></a>Print out the pattern space (to the standard output).
This command is usually only used in conjunction with the <samp><span class="option">-n</span></samp>
command-line option.
<p>Note: some implementations of <samp><span class="command">sed</span></samp>, such as this one, will
double-print lines when auto-print is not disabled and the <code>p</code>
command is given.
Other implementations will only print the line once.
<!-- *** CHECK CURRENT STANDARD. I THINK NOT DOUBLE-PRINTING IS HOW IT WORKS. ADR. -->
Both ways conform with the <span class="sc">posix</span> standard, and so neither
way can be considered to be in error.
<p><a name="index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-91"></a><a name="index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-92"></a>Portable <samp><span class="command">sed</span></samp> scripts should thus avoid relying on either behavior;
either use the <samp><span class="option">-n</span></samp> option and explicitly print what you want,
or avoid use of the <code>p</code> command (and also the <code>p</code> flag to the
<code>s</code> command).
<br><dt><code>n</code><dd><a name="index-n-_0028next_002dline_0029-command-93"></a><a name="index-Next-input-line_002c-replace-pattern-space-with-94"></a><a name="index-Read-next-input-line-95"></a>If auto-print is not disabled, print the pattern space,
then, regardless, replace the pattern space with the next line of input.
If there is no more input then <samp><span class="command">sed</span></samp> exits without processing
any more commands.
<br><dt><code>{ </code><var>commands</var><code> }</code><dd><a name="index-g_t_0040_007b_0040_007d-command-grouping-96"></a><a name="index-Grouping-commands-97"></a><a name="index-Command-groups-98"></a>A group of commands may be enclosed between
<code>{</code> and <code>}</code> characters.
This is particularly useful when you want a group of commands
to be triggered by a single address (or address-range) match.
<h3 class="section">3.5 The <code>s</code> Command</h3>
<p>The syntax of the <code>s</code> (as in substitute) command is
`<samp><span class="samp">s/</span><var>regexp</var><span class="samp">/</span><var>replacement</var><span class="samp">/</span><var>flags</var></samp>'. The <code>/</code>
characters may be uniformly replaced by any other single
character within any given <code>s</code> command. The <code>/</code>
character (or whatever other character is used in its stead)
can appear in the <var>regexp</var> or <var>replacement</var>
only if it is preceded by a <code>\</code> character.
<p>The <code>s</code> command is probably the most important in <samp><span class="command">sed</span></samp>
and has a lot of different options. Its basic concept is simple:
the <code>s</code> command attempts to match the pattern
space against the supplied <var>regexp</var>; if the match is
successful, then that portion of the pattern
space which was matched is replaced with <var>replacement</var>.
<p><a name="index-Backreferences_002c-in-regular-expressions-99"></a><a name="index-Parenthesized-substrings-100"></a>The <var>replacement</var> can contain <code>\</code><var>n</var> (<var>n</var> being
a number from 1 to 9, inclusive) references, which refer to
the portion of the match which is contained between the <var>n</var>th
<code>\(</code> and its matching <code>\)</code>.
Also, the <var>replacement</var> can contain unescaped <code>&</code>
characters which reference the whole matched portion
of the pattern space.
<a name="index-g_t_0040value_007bSSEDEXT_007d_002c-case-modifiers-in-_0040code_007bs_007d-commands-101"></a>Finally (this is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension) you can include a
special sequence made of a backslash and one of the letters
<code>L</code>, <code>l</code>, <code>U</code>, <code>u</code>, or <code>E</code>.
The meaning is as follows:
<dl>
<dt><code>\L</code><dd>Turn the replacement
to lowercase until a <code>\U</code> or <code>\E</code> is found,
<br><dt><code>\l</code><dd>Turn the
next character to lowercase,
<br><dt><code>\U</code><dd>Turn the replacement to uppercase
until a <code>\L</code> or <code>\E</code> is found,
<br><dt><code>\u</code><dd>Turn the next character
to uppercase,
<br><dt><code>\E</code><dd>Stop case conversion started by <code>\L</code> or <code>\U</code>.
</dl>
<p>To include a literal <code>\</code>, <code>&</code>, or newline in the final
replacement, be sure to precede the desired <code>\</code>, <code>&</code>,
or newline in the <var>replacement</var> with a <code>\</code>.
<p><a name="index-s-command_002c-option-flags-102"></a><a name="index-Substitution-of-text_002c-options-103"></a>The <code>s</code> command can be followed by zero or more of the
following <var>flags</var>:
<dl>
<dt><code>g</code><dd><a name="index-Global-substitution-104"></a><a name="index-Replacing-all-text-matching-regexp-in-a-line-105"></a>Apply the replacement to <em>all</em> matches to the <var>regexp</var>,
not just the first.
<br><dt><var>number</var><dd><a name="index-Replacing-only-_0040var_007bn_007dth-match-of-regexp-in-a-line-106"></a>Only replace the <var>number</var>th match of the <var>regexp</var>.
<p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifier-interaction-in-_0040code_007bs_007d-command-107"></a><a name="index-Mixing-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifiers-in-the-_0040code_007bs_007d-command-108"></a>Note: the <span class="sc">posix</span> standard does not specify what should happen
when you mix the <code>g</code> and <var>number</var> modifiers,
and currently there is no widely agreed upon meaning
across <samp><span class="command">sed</span></samp> implementations.
For <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>, the interaction is defined to be:
ignore matches before the <var>number</var>th,
and then match and replace all matches from
the <var>number</var>th on.
<br><dt><code>p</code><dd><a name="index-Text_002c-printing-after-substitution-109"></a>If the substitution was made, then print the new pattern space.
<p>Note: when both the <code>p</code> and <code>e</code> options are specified,
the relative ordering of the two produces very different results.
In general, <code>ep</code> (evaluate then print) is what you want,
but operating the other way round can be useful for debugging.
For this reason, the current version of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> interprets
specially the presence of <code>p</code> options both before and after
<code>e</code>, printing the pattern space before and after evaluation,
while in general flags for the <code>s</code> command show their
effect just once. This behavior, although documented, might
change in future versions.
<br><dt><code>w </code><var>file-name</var><dd><a name="index-Text_002c-writing-to-a-file-after-substitution-110"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-111"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-112"></a>If the substitution was made, then write out the result to the named file.
As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, two special values of <var>file-name</var> are
supported: <samp><span class="file">/dev/stderr</span></samp>, which writes the result to the standard
error, and <samp><span class="file">/dev/stdout</span></samp>, which writes to the standard
<br><dt><code>e</code><dd><a name="index-Evaluate-Bourne_002dshell-commands_002c-after-substitution-113"></a><a name="index-Subprocesses-114"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-115"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-116"></a>This command allows one to pipe input from a shell command
into pattern space. If a substitution was made, the command
that is found in pattern space is executed and pattern space
is replaced with its output. A trailing newline is suppressed;
results are undefined if the command to be executed contains
a <span class="sc">nul</span> character. This is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
<br><dt><code>I</code><dt><code>i</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-117"></a><a name="index-Case_002dinsensitive-matching-118"></a>The <code>I</code> modifier to regular-expression matching is a <acronym>GNU</acronym>
extension which makes <samp><span class="command">sed</span></samp> match <var>regexp</var> in a
case-insensitive manner.
<br><dt><code>M</code><dt><code>m</code><dd><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bM_007d-modifier-119"></a>The <code>M</code> modifier to regular-expression matching is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
extension which causes <code>^</code> and <code>$</code> to match respectively
(in addition to the normal behavior) the empty string after a newline,
and the empty string before a newline. There are special character
sequences
(<code>\`</code> and <code>\'</code>)
which always match the beginning or the end of the buffer.
<code>M</code> stands for <cite>multi-line</cite>.
<h3 class="section">3.6 Less Frequently-Used Commands</h3>
<p>Though perhaps less frequently used than those in the previous
section, some very small yet useful <samp><span class="command">sed</span></samp> scripts can be built with
these commands.
<dl>
<dt><code>y/</code><var>source-chars</var><code>/</code><var>dest-chars</var><code>/</code><dd>(The <code>/</code> characters may be uniformly replaced by
any other single character within any given <code>y</code> command.)
<p><a name="index-y-_0028transliterate_0029-command-120"></a><a name="index-Transliteration-121"></a>Transliterate any characters in the pattern space which match
any of the <var>source-chars</var> with the corresponding character
in <var>dest-chars</var>.
<p>Instances of the <code>/</code> (or whatever other character is used in its stead),
<code>\</code>, or newlines can appear in the <var>source-chars</var> or <var>dest-chars</var>
lists, provide that each instance is escaped by a <code>\</code>.
The <var>source-chars</var> and <var>dest-chars</var> lists <em>must</em>
contain the same number of characters (after de-escaping).
<br><dt><code>a\</code><dt><var>text</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-122"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
address.
<p><a name="index-a-_0028append-text-lines_0029-command-123"></a><a name="index-Appending-text-after-a-line-124"></a><a name="index-Text_002c-appending-125"></a>Queue the lines of text which follow this command
(each but the last ending with a <code>\</code>,
which are removed from the output)
to be output at the end of the current cycle,
or when the next input line is read.
<p>As a <acronym>GNU</acronym> extension, if between the <code>a</code> and the newline there is
other than a whitespace-<code>\</code> sequence, then the text of this line,
starting at the first non-whitespace character after the <code>a</code>,
is taken as the first line of the <var>text</var> block.
(This enables a simplification in scripting a one-line add.)
This extension also works with the <code>i</code> and <code>c</code> commands.
<br><dt><code>i\</code><dt><var>text</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-126"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
address.
<p><a name="index-i-_0028insert-text-lines_0029-command-127"></a><a name="index-Inserting-text-before-a-line-128"></a><a name="index-Text_002c-insertion-129"></a>Immediately output the lines of text which follow this command
(each but the last ending with a <code>\</code>,
which are removed from the output).
<br><dt><code>c\</code><dt><var>text</var><dd><a name="index-c-_0028change-to-text-lines_0029-command-130"></a><a name="index-Replacing-selected-lines-with-other-text-131"></a>Delete the lines matching the address or address-range,
and output the lines of text which follow this command
(each but the last ending with a <code>\</code>,
which are removed from the output)
in place of the last line
(or in place of each line, if no addresses were specified).
A new cycle is started after this command is done,
since the pattern space will have been deleted.
<br><dt><code>=</code><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-132"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
address.
<p><a name="index-g_t_003d-_0028print-line-number_0029-command-133"></a><a name="index-Printing-line-number-134"></a><a name="index-Line-number_002c-printing-135"></a>Print out the current input line number (with a trailing newline).
<br><dt><code>l </code><var>n</var><dd><a name="index-l-_0028list-unambiguously_0029-command-136"></a><a name="index-List-pattern-space-137"></a><a name="index-Printing-text-unambiguously-138"></a><a name="index-Line-length_002c-setting-139"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-setting-line-length-140"></a>Print the pattern space in an unambiguous form:
non-printable characters (and the <code>\</code> character)
are printed in C-style escaped form; long lines are split,
with a trailing <code>\</code> character to indicate the split;
the end of each line is marked with a <code>$</code>.
<p><var>n</var> specifies the desired line-wrap length;
a length of 0 (zero) means to never wrap long lines. If omitted,
the default as specified on the command line is used. The <var>n</var>
parameter is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
<br><dt><code>r </code><var>filename</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-141"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
address.
<p><a name="index-r-_0028read-file_0029-command-142"></a><a name="index-Read-text-from-a-file-143"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-144"></a>Queue the contents of <var>filename</var> to be read and
inserted into the output stream at the end of the current cycle,
or when the next input line is read.
Note that if <var>filename</var> cannot be read, it is treated as
if it were an empty file, without any error indication.
<p>As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, the special value <samp><span class="file">/dev/stdin</span></samp>
is supported for the file name, which reads the contents of the
standard input.
<br><dt><code>w </code><var>filename</var><dd><a name="index-w-_0028write-file_0029-command-145"></a><a name="index-Write-to-a-file-146"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-147"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-148"></a>Write the pattern space to <var>filename</var>.
As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, two special values of <var>file-name</var> are
supported: <samp><span class="file">/dev/stderr</span></samp>, which writes the result to the standard
error, and <samp><span class="file">/dev/stdout</span></samp>, which writes to the standard
<p>The file will be created (or truncated) before the
first input line is read; all <code>w</code> commands
(including instances of <code>w</code> flag on successful <code>s</code> commands)
which refer to the same <var>filename</var> are output without
closing and reopening the file.
<br><dt><code>D</code><dd><a name="index-D-_0028delete-first-line_0029-command-149"></a><a name="index-Delete-first-line-from-pattern-space-150"></a>Delete text in the pattern space up to the first newline.
If any text is left, restart cycle with the resultant
pattern space (without reading a new line of input),
otherwise start a normal new cycle.
<br><dt><code>N</code><dd><a name="index-N-_0028append-Next-line_0029-command-151"></a><a name="index-Next-input-line_002c-append-to-pattern-space-152"></a><a name="index-Append-next-input-line-to-pattern-space-153"></a>Add a newline to the pattern space,
then append the next line of input to the pattern space.
If there is no more input then <samp><span class="command">sed</span></samp> exits without processing
any more commands.
<br><dt><code>P</code><dd><a name="index-P-_0028print-first-line_0029-command-154"></a><a name="index-Print-first-line-from-pattern-space-155"></a>Print out the portion of the pattern space up to the first newline.
<br><dt><code>h</code><dd><a name="index-h-_0028hold_0029-command-156"></a><a name="index-Copy-pattern-space-into-hold-space-157"></a><a name="index-Replace-hold-space-with-copy-of-pattern-space-158"></a><a name="index-Hold-space_002c-copying-pattern-space-into-159"></a>Replace the contents of the hold space with the contents of the pattern space.
<br><dt><code>H</code><dd><a name="index-H-_0028append-Hold_0029-command-160"></a><a name="index-Append-pattern-space-to-hold-space-161"></a><a name="index-Hold-space_002c-appending-from-pattern-space-162"></a>Append a newline to the contents of the hold space,
and then append the contents of the pattern space to that of the hold space.
<br><dt><code>g</code><dd><a name="index-g-_0028get_0029-command-163"></a><a name="index-Copy-hold-space-into-pattern-space-164"></a><a name="index-Replace-pattern-space-with-copy-of-hold-space-165"></a><a name="index-Hold-space_002c-copy-into-pattern-space-166"></a>Replace the contents of the pattern space with the contents of the hold space.
<br><dt><code>G</code><dd><a name="index-G-_0028appending-Get_0029-command-167"></a><a name="index-Append-hold-space-to-pattern-space-168"></a><a name="index-Hold-space_002c-appending-to-pattern-space-169"></a>Append a newline to the contents of the pattern space,
and then append the contents of the hold space to that of the pattern space.
<br><dt><code>x</code><dd><a name="index-x-_0028eXchange_0029-command-170"></a><a name="index-Exchange-hold-space-with-pattern-space-171"></a><a name="index-Hold-space_002c-exchange-with-pattern-space-172"></a>Exchange the contents of the hold and pattern spaces.
<p><a name="index-g_t_003a-_0028label_0029-command-174"></a><a name="index-Labels_002c-in-scripts-175"></a>Specify the location of <var>label</var> for branch commands.
In all other respects, a no-op.
<br><dt><code>b </code><var>label</var><dd><a name="index-b-_0028branch_0029-command-176"></a><a name="index-Branch-to-a-label_002c-unconditionally-177"></a><a name="index-Goto_002c-in-scripts-178"></a>Unconditionally branch to <var>label</var>.
The <var>label</var> may be omitted, in which case the next cycle is started.
<br><dt><code>t </code><var>label</var><dd><a name="index-t-_0028test-and-branch-if-successful_0029-command-179"></a><a name="index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-succeeded-180"></a><a name="index-Conditional-branch-181"></a>Branch to <var>label</var> only if there has been a successful <code>s</code>ubstitution
since the last input line was read or conditional branch was taken.
The <var>label</var> may be omitted, in which case the next cycle is started.
<h3 class="section">3.8 Commands Specific to <acronym>GNU</acronym> <samp><span class="command">sed</span></samp></h3>
<p>These commands are specific to <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>, so you
must use them with care and only when you are sure that
hindering portability is not evil. They allow you to check
for <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extensions or to do tasks that are required
quite often, yet are unsupported by standard <samp><span class="command">sed</span></samp>s.
<dl>
<dt><code>e [</code><var>command</var><code>]</code><dd><a name="index-e-_0028evaluate_0029-command-182"></a><a name="index-Evaluate-Bourne_002dshell-commands-183"></a><a name="index-Subprocesses-184"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-185"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-186"></a>This command allows one to pipe input from a shell command
into pattern space. Without parameters, the <code>e</code> command
executes the command that is found in pattern space and
replaces the pattern space with the output; a trailing newline
is suppressed.
<p>If a parameter is specified, instead, the <code>e</code> command
interprets it as a command and sends its output to the output stream
(like <code>r</code> does). The command can run across multiple
lines, all but the last ending with a back-slash.
<p>In both cases, the results are undefined if the command to be
executed contains a <span class="sc">nul</span> character.
<br><dt><code>L </code><var>n</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-187"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
address.
<p><a name="index-L-_0028fLow-paragraphs_0029-command-188"></a><a name="index-Reformat-pattern-space-189"></a><a name="index-Reformatting-paragraphs-190"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-reformatting-paragraphs-191"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bL_007d-command-192"></a>This <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension fills and joins lines in pattern space
to produce output lines of (at most) <var>n</var> characters, like
<code>fmt</code> does; if <var>n</var> is omitted, the default as specified
on the command line is used.
<p>Blank lines, spaces between words, and indentation are
preserved in the output; successive input lines with different
indentation are not joined; tabs are expanded to 8 columns.
<p>If the pattern space contains multiple lines, they are joined, but
since the pattern space usually contains a single line, the behavior
of a simple <code>L;d</code> script is the same as `<samp><span class="samp">fmt -s</span></samp>' (i.e.,
it does not join short lines to form longer ones).
<p><var>n</var> specifies the desired line-wrap length; if omitted,
the default as specified on the command line is used.
<br><dt><code>Q [</code><var>exit-code</var><code>]</code><dd>This command only accepts a single address.
<p><a name="index-Q-_0028silent-Quit_0029-command-193"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-quitting-silently-194"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-195"></a><a name="index-Quitting-196"></a>This command is the same as <code>q</code>, but will not print the
contents of pattern space. Like <code>q</code>, it provides the
ability to return an exit code to the caller.
<p>This command can be useful because the only alternative ways
to accomplish this apparently trivial function are to use
the <samp><span class="option">-n</span></samp> option (which can unnecessarily complicate
your script) or resorting to the following snippet, which
wastes time by reading the whole file without any visible effect:
<pre class="example"> :eat
$d <i>Quit silently on the last line</i>
N <i>Read another line, silently</i>
g <i>Overwrite pattern space each time to save memory</i>
b eat
</pre>
<br><dt><code>R </code><var>filename</var><dd><a name="index-R-_0028read-line_0029-command-197"></a><a name="index-Read-text-from-a-file-198"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-reading-a-file-a-line-at-a-time-199"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bR_007d-command-200"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-201"></a>Queue a line of <var>filename</var> to be read and
inserted into the output stream at the end of the current cycle,
or when the next input line is read.
Note that if <var>filename</var> cannot be read, or if its end is
reached, no line is appended, without any error indication.
<p>As with the <code>r</code> command, the special value <samp><span class="file">/dev/stdin</span></samp>
is supported for the file name, which reads a line from the
standard input.
<br><dt><code>T </code><var>label</var><dd><a name="index-T-_0028test-and-branch-if-failed_0029-command-202"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-branch-if-_0040code_007bs_002f_002f_002f_007d-failed-203"></a><a name="index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-failed-204"></a><a name="index-Conditional-branch-205"></a>Branch to <var>label</var> only if there have been no successful
<code>s</code>ubstitutions since the last input line was read or
conditional branch was taken. The <var>label</var> may be omitted,
in which case the next cycle is started.
<br><dt><code>v </code><var>version</var><dd><a name="index-v-_0028version_0029-command-206"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-checking-for-their-presence-207"></a><a name="index-Requiring-_0040value_007bSSED_007d-208"></a>This command does nothing, but makes <samp><span class="command">sed</span></samp> fail if
<acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extensions are not supported, simply because other
versions of <samp><span class="command">sed</span></samp> do not implement it. In addition, you
can specify the version of <samp><span class="command">sed</span></samp> that your script
requires, such as <code>4.0.5</code>. The default is <code>4.0</code>
because that is the first version that implemented this command.
<p>This commands also enables <acronym>GNU</acronym> extensions unconditionally, even
if <samp><span class="env">POSIXLY_CORRECT</span></samp> is set in the environment.
<br><dt><code>W </code><var>filename</var><dd><a name="index-W-_0028write-first-line_0029-command-209"></a><a name="index-Write-first-line-to-a-file-210"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-writing-first-line-to-a-file-211"></a>Write to the given filename the portion of the pattern space up to
the first newline. Everything said under the <code>w</code> command about
<h3 class="section">3.9 <acronym>GNU</acronym> Extensions for Escapes in Regular Expressions</h3>
<p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-212"></a>Until this chapter, we have only encountered escapes of the form
`<samp><span class="samp">\^</span></samp>', which tell <samp><span class="command">sed</span></samp> not to interpret the circumflex
as a special character, but rather to take it literally. For
example, `<samp><span class="samp">\*</span></samp>' matches a single asterisk rather than zero
or more backslashes.
<p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-escapes-213"></a>This chapter introduces another kind of escape<a rel="footnote" href="#fn-5" name="fnd-5"><sup>5</sup></a>—that
is, escapes that are applied to a character or sequence of characters
that ordinarily are taken literally, and that <samp><span class="command">sed</span></samp> replaces
with a special character. This provides a way
of encoding non-printable characters in patterns in a visible manner.
There is no restriction on the appearance of non-printing characters
in a <samp><span class="command">sed</span></samp> script but when a script is being prepared in the
shell or by text editing, it is usually easier to use one of
the following escape sequences than the binary character it
represents:
<p>The list of these escapes is:
<dl>
<dt><code>\a</code><dd>Produces or matches a <span class="sc">bel</span> character, that is an “alert” (<span class="sc">ascii</span> 7).
<br><dt><code>\f</code><dd>Produces or matches a form feed (<span class="sc">ascii</span> 12).
<br><dt><code>\n</code><dd>Produces or matches a newline (<span class="sc">ascii</span> 10).
<br><dt><code>\r</code><dd>Produces or matches a carriage return (<span class="sc">ascii</span> 13).
<br><dt><code>\t</code><dd>Produces or matches a horizontal tab (<span class="sc">ascii</span> 9).
<br><dt><code>\v</code><dd>Produces or matches a so called “vertical tab” (<span class="sc">ascii</span> 11).
<br><dt><code>\c</code><var>x</var><dd>Produces or matches <span class="sc">Control</span><kbd>-</kbd><var>x</var>, where <var>x</var> is
any character. The precise effect of `<samp><span class="samp">\c</span><var>x</var></samp>' is as follows:
if <var>x</var> is a lower case letter, it is converted to upper case.
Then bit 6 of the character (hex 40) is inverted. Thus `<samp><span class="samp">\cz</span></samp>' becomes
hex 1A, but `<samp><span class="samp">\c{</span></samp>' becomes hex 3B, while `<samp><span class="samp">\c;</span></samp>' becomes hex 7B.
<br><dt><code>\d</code><var>xxx</var><dd>Produces or matches a character whose decimal <span class="sc">ascii</span> value is <var>xxx</var>.
<br><dt><code>\o</code><var>xxx</var><dd>Produces or matches a character whose octal <span class="sc">ascii</span> value is <var>xxx</var>.
<br><dt><code>\x</code><var>xx</var><dd>Produces or matches a character whose hexadecimal <span class="sc">ascii</span> value is <var>xx</var>.
</dl>
<p>`<samp><span class="samp">\b</span></samp>' (backspace) was omitted because of the conflict with
the existing “word boundary” meaning.
<p>Other escapes match a particular character class and are valid only in
regular expressions:
<dl>
<dt><code>\w</code><dd>Matches any “word” character. A “word” character is any
letter or digit or the underscore character.
<br><dt><code>\W</code><dd>Matches any “non-word” character.
<br><dt><code>\b</code><dd>Matches a word boundary; that is it matches if the character
to the left is a “word” character and the character to the
right is a “non-word” character, or vice-versa.
<br><dt><code>\B</code><dd>Matches everywhere but on a word boundary; that is it matches
if the character to the left and the character to the right
are either both “word” characters or both “non-word”
characters.
<br><dt><code>\`</code><dd>Matches only at the start of pattern space. This is different
from <code>^</code> in multi-line mode.
<br><dt><code>\'</code><dd>Matches only at the end of pattern space. This is different
<p>This script is one of a few that demonstrate how to do arithmetic
in <samp><span class="command">sed</span></samp>. This is indeed possible,<a rel="footnote" href="#fn-6" name="fnd-6"><sup>6</sup></a> but must be done manually.
<p>To increment one number you just add 1 to last digit, replacing
it by the following digit. There is one exception: when the digit
is a nine the previous digits must be also incremented until you
don't have a nine.
<p>This solution by Bruno Haible is very clever and smart because
it uses a single buffer; if you don't have this limitation, the
algorithm used in <a href="#cat-_002dn">Numbering lines</a>, is faster.
It works by replacing trailing nines with an underscore, then
using multiple <code>s</code> commands to increment the last digit,
and then again substituting underscores with zeros.
<!-- start- -->
<pre class="example"> #!/usr/bin/sed -f
/[^0-9]/ d
# replace all leading 9s by _ (any other character except digits, could
# be used)
:d
s/9\(_*\)$/_\1/
td
# incr last digit only. The first line adds a most-significant
# digit of 1 if we have to add a digit.
#
# The <code>tn</code> commands are not necessary, but make the thing
<p>This script shows another way to do arithmetic with <samp><span class="command">sed</span></samp>.
In this case we have to add possibly large numbers, so implementing
this by successive increments would not be feasible (and possibly
even more complicated to contrive than this script).
<p>The approach is to map numbers to letters, kind of an abacus
implemented with <samp><span class="command">sed</span></samp>. `<samp><span class="samp">a</span></samp>'s are units, `<samp><span class="samp">b</span></samp>'s are
tenths and so on: we simply add the number of characters
on the current line as units, and then propagate the carry
to tenths, hundredths, and so on.
<p>As usual, running totals are kept in hold space.
<p>On the last line, we convert the abacus form back to decimal.
For the sake of variety, this is done with a loop rather than
with some 80 <code>s</code> commands<a rel="footnote" href="#fn-8" name="fnd-8"><sup>8</sup></a>: first we
convert units, removing `<samp><span class="samp">a</span></samp>'s from the number; then we
rotate letters so that tenths become `<samp><span class="samp">a</span></samp>'s, and so on
until no more letters remain.
<!-- start- -->
<pre class="example"> #!/usr/bin/sed -nf
# Add n+1 a's to hold space (+1 is for the newline)
s/./a/g
H
x
s/\n/a/
# Do the carry. The t's and b's are not necessary,
<h2 class="chapter">5 <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>'s Limitations and Non-limitations</h2>
<p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-unlimited-line-length-214"></a><a name="index-Portability_002c-line-length-limitations-215"></a>For those who want to write portable <samp><span class="command">sed</span></samp> scripts,
be aware that some implementations have been known to
limit line lengths (for the pattern and hold spaces)
to be no more than 4000 bytes.
The <span class="sc">posix</span> standard specifies that conforming <samp><span class="command">sed</span></samp>
implementations shall support at least 8192 byte line lengths.
<acronym>GNU</acronym> <samp><span class="command">sed</span></samp> has no built-in limit on line length;
as long as it can <code>malloc()</code> more (virtual) memory,
you can feed or construct lines as long as you like.
<p>However, recursion is used to handle subpatterns and indefinite
repetition. This means that the available stack space may limit
the size of the buffer that can be processed by certain patterns.
<h2 class="chapter">6 Other Resources for Learning About <samp><span class="command">sed</span></samp></h2>
<p><a name="index-Additional-reading-about-_0040command_007bsed_007d-216"></a>In addition to several books that have been written about <samp><span class="command">sed</span></samp>
(either specifically or as chapters in books which discuss
shell programming), one can find out more about <samp><span class="command">sed</span></samp>
(including suggestions of a few books) from the FAQ
for the <code>sed-users</code> mailing list, available from any of:
<p><a name="index-Bugs_002c-reporting-217"></a>Email bug reports to <a href="mailto:bonzini@gnu.org">bonzini@gnu.org</a>.
Be sure to include the word “sed” somewhere in the <code>Subject:</code> field.
Also, please include the output of `<samp><span class="samp">sed --version</span></samp>' in the body
of your report if at all possible.
<p>Please do not send a bug report like this:
<pre class="example"> <i>while building frobme-1.3.4</i>
$ configure
error--> sed: file sedscr line 1: Unknown option to 's'
</pre>
<p>If <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> doesn't configure your favorite package, take a
few extra minutes to identify the specific problem and make a stand-alone
test case. Unlike other programs such as C compilers, making such test
cases for <samp><span class="command">sed</span></samp> is quite simple.
<p>A stand-alone test case includes all the data necessary to perform the
test, and the specific invocation of <samp><span class="command">sed</span></samp> that causes the problem.
The smaller a stand-alone test case is, the better. A test case should
not involve something as far removed from <samp><span class="command">sed</span></samp> as “try to configure
frobme-1.3.4”. Yes, that is in principle enough information to look
for the bug, but that is not a very practical prospect.
<p>Here are a few commonly reported bugs that are not bugs.
<dl>
<dt>`<samp><span class="samp">sed -n</span></samp>' and `<samp><span class="samp">s/</span><var>regex</var><span class="samp">/`<samp></span><span class="samp">replace</span><span class="samp"></samp>'/p</span></samp>'<dd><a name="index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-218"></a><a name="index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-219"></a>Some versions of <samp><span class="command">sed</span></samp> ignore the <code>p</code> (print) option of an <code>s</code> command
unless the <samp><span class="option">-n</span></samp> command-line option has been specified. Other versions
always honor the <code>p</code> option.
<!-- *** CHECK THE CURRENT STANDARD. ADR. -->
Both approaches are allowed by <span class="sc">posix</span>
and <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
is the
better when you write complex scripts and also more intuitive, but
portable scripts should be written to work correctly with either
behavior.
<br><dt><code>N</code> command on the last line<dd><a name="index-Portability_002c-_0040code_007bN_007d-command-on-the-last-line-220"></a><a name="index-Non_002dbugs_002c-_0040code_007bN_007d-command-on-the-last-line-221"></a>
Most versions of <samp><span class="command">sed</span></samp> exit without printing anything when
the <samp><span class="command">N</span></samp> command is issued on the last line of a file.
<acronym>GNU</acronym> <samp><span class="command">sed</span></samp> prints pattern space before exiting unless of course
the <samp><span class="command">-n</span></samp> command switch has been specified. This choice is
by design.
<p>For example, the behavior of
<pre class="example"> sed N foo bar
</pre>
<p class="noindent">would depend on whether foo has an even or an odd number of
lines<a rel="footnote" href="#fn-9" name="fnd-9"><sup>9</sup></a>. Or, when writing a script to read the
next few lines following a pattern match, traditional
implementations of <code>sed</code> would force you to write
<p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-_0040code_007bN_007d-command-222"></a>In any case, the simplest workaround is to use <code>$d;N</code> in
scripts that rely on the traditional behavior, or to set
the <code>POSIXLY_CORRECT</code> variable to a non-empty value.
<br><dt>Regex syntax clashes<dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-223"></a><a name="index-Non_002dbugs_002c-regex-syntax-clashes-224"></a><samp><span class="command">sed</span></samp> uses the <span class="sc">posix</span> basic regular expression syntax. According to
the standard, the meaning of some escape sequences is undefined in
this syntax; notable in the case of <samp><span class="command">sed</span></samp> are <code>\|</code>,
<p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-225"></a>In addition, this version of <samp><span class="command">sed</span></samp> supports several escape characters
(some of which are multi-character) to insert non-printable characters
in scripts (<code>\a</code>, <code>\c</code>, <code>\d</code>, <code>\o</code>, <code>\r</code>,
<code>\t</code>, <code>\v</code>, <code>\x</code>). These can cause similar problems
with scripts written for other <samp><span class="command">sed</span></samp>s.
<li><a href="#index-Addresses_002c-in-_0040command_007bsed_007d-scripts-37">Addresses, in <samp><span class="command">sed</span></samp> scripts</a>: <a href="#Addresses">Addresses</a></li>
<li><a href="#index-Append-hold-space-to-pattern-space-168">Append hold space to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Append-next-input-line-to-pattern-space-153">Append next input line to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Append-pattern-space-to-hold-space-161">Append pattern space to hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Appending-text-after-a-line-124">Appending text after a line</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Backreferences_002c-in-regular-expressions-99">Backreferences, in regular expressions</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-failed-204">Branch to a label, if <code>s///</code> failed</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-succeeded-180">Branch to a label, if <code>s///</code> succeeded</a>: <a href="#Programming-Commands">Programming Commands</a></li>
<li><a href="#index-Branch-to-a-label_002c-unconditionally-177">Branch to a label, unconditionally</a>: <a href="#Programming-Commands">Programming Commands</a></li>
<li><a href="#index-Buffer-spaces_002c-pattern-and-hold-75">Buffer spaces, pattern and hold</a>: <a href="#Data-Spaces">Data Spaces</a></li>
<li><a href="#index-Copy-hold-space-into-pattern-space-164">Copy hold space into pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Copy-pattern-space-into-hold-space-157">Copy pattern space into hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Delete-first-line-from-pattern-space-150">Delete first line from pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Disabling-autoprint_002c-from-command-line-11">Disabling autoprint, from command line</a>: <a href="#Invoking-sed">Invoking sed</a></li>
<li><a href="#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution-113">Evaluate Bourne-shell commands, after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Exchange-hold-space-with-pattern-space-171">Exchange hold space with pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-branch-if-_0040code_007bs_002f_002f_002f_007d-failed-203"><acronym>GNU</acronym> extensions, branch if <code>s///</code> failed</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-case-modifiers-in-_0040code_007bs_007d-commands-101"><acronym>GNU</acronym> extensions, case modifiers in <code>s</code> commands</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-checking-for-their-presence-207"><acronym>GNU</acronym> extensions, checking for their presence</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-reading-a-file-a-line-at-a-time-199"><acronym>GNU</acronym> extensions, reading a file a line at a time</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-223"><acronym>GNU</acronym> extensions, to basic regular expressions</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
<li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-69"><acronym>GNU</acronym> extensions, to basic regular expressions</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
<li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-unlimited-line-length-214"><acronym>GNU</acronym> extensions, unlimited line length</a>: <a href="#Limitations">Limitations</a></li>
<li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-writing-first-line-to-a-file-211"><acronym>GNU</acronym> extensions, writing first line to a file</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-Goto_002c-in-scripts-178">Goto, in scripts</a>: <a href="#Programming-Commands">Programming Commands</a></li>
<li><a href="#index-Mixing-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifiers-in-the-_0040code_007bs_007d-command-108">Mixing <code>g</code> and <var>number</var> modifiers in the <code>s</code> command</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Non_002dbugs_002c-_0040code_007bN_007d-command-on-the-last-line-221">Non-bugs, <code>N</code> command on the last line</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
<li><a href="#index-Portability_002c-line-length-limitations-215">Portability, line length limitations</a>: <a href="#Limitations">Limitations</a></li>
<li><a href="#index-Portability_002c-_0040code_007bN_007d-command-on-the-last-line-220">Portability, <code>N</code> command on the last line</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
<li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-187"><code>POSIXLY_CORRECT</code> behavior, two addresses</a>: <a href="#Extended-Commands">Extended Commands</a></li>
<li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-122"><code>POSIXLY_CORRECT</code> behavior, two addresses</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Print-first-line-from-pattern-space-155">Print first line from pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Printing-line-number-134">Printing line number</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Printing-text-unambiguously-138">Printing text unambiguously</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Replace-hold-space-with-copy-of-pattern-space-158">Replace hold space with copy of pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Replace-pattern-space-with-copy-of-hold-space-165">Replace pattern space with copy of hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Replacing-all-text-matching-regexp-in-a-line-105">Replacing all text matching regexp in a line</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Replacing-only-_0040var_007bn_007dth-match-of-regexp-in-a-line-106">Replacing only <var>n</var>th match of regexp in a line</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Replacing-selected-lines-with-other-text-131">Replacing selected lines with other text</a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-Text_002c-printing-after-substitution-109">Text, printing after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-Text_002c-writing-to-a-file-after-substitution-110">Text, writing to a file after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
<li><a href="#index-g_t_002dn_002c-forcing-from-within-a-script-82"><code>-n, forcing from within a script</code></a>: <a href="#Common-Commands">Common Commands</a></li>
<li><a href="#index-c-_0028change-to-text-lines_0029-command-130"><code>c (change to text lines) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a href="#index-D-_0028delete-first-line_0029-command-149"><code>D (delete first line) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
<li><a name="toc_Command-and-Option-Index" href="#Command-and-Option-Index">Command and Option Index</a>
</li></ul>
</div>
<div class="footnote">
<hr>
<a name="texinfo-footnotes-in-document"></a><h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> This applies to commands such as <code>=</code>,
<code>a</code>, <code>c</code>, <code>i</code>, <code>l</code>, <code>p</code>. You can
still write to the standard output by using the <code>w</code>
<a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-230"></a>or <code>W</code> commands together with the <samp><span class="file">/dev/stdout</span></samp>
special file</p>
<p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> Note that <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
creates the backup file whether
or not any output is actually changed.</p>
<p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> This is equivalent to <code>p</code> unless the <samp><span class="option">-i</span></samp>
option is being used.</p>
<p class="footnote"><small>[<a name="fn-4" href="#fnd-4">4</a>]</small> This is equivalent to <code>p</code> unless the <samp><span class="option">-i</span></samp>
option is being used.</p>
<p class="footnote"><small>[<a name="fn-5" href="#fnd-5">5</a>]</small> All
the escapes introduced here are <acronym>GNU</acronym>
extensions, with the exception of <code>\n</code>.</p>
<p class="footnote"><small>[<a name="fn-6" href="#fnd-6">6</a>]</small> <samp><span class="command">sed</span></samp> guru Greg
Ubben wrote an implementation of the <samp><span class="command">dc</span></samp> <span class="sc">rpn</span> calculator!
It is distributed together with sed.</p>
<p class="footnote"><small>[<a name="fn-7" href="#fnd-7">7</a>]</small> This requires
another script to pad the output of banner; for example
<pre class="example"> #! /bin/sh
banner -w $1 $2 $3 $4 |
sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
~/sedscripts/reverseline.sed
</pre>
<p></p>
<p class="footnote"><small>[<a name="fn-8" href="#fnd-8">8</a>]</small> Some implementations
have a limit of 199 commands per script</p>
<p class="footnote"><small>[<a name="fn-9" href="#fnd-9">9</a>]</small> which is the actual “bug” that prompted the