esy-bash
Version:
Cross-platform bash utilities - primed for Reason/OCaml
1,091 lines (1,008 loc) • 354 kB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents version 4.4 of
GNU sed, a stream editor.
Copyright (C) 1998-2017 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no
Back-Cover Texts. A copy of the license is included in the
section entitled "GNU Free Documentation License". -->
<!-- Created by GNU Texinfo 6.3, http://www.gnu.org/software/texinfo/ -->
<head>
<title>sed, a stream editor</title>
<meta name="description" content="sed, a stream editor">
<meta name="keywords" content="sed, a stream editor">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="#Top" rel="start" title="Top">
<link href="#Concept-Index" rel="index" title="Concept Index">
<link href="#SEC_Contents" rel="contents" title="Table of Contents">
<link href="dir.html#Top" rel="up" title="(dir)">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<h1 class="settitle" align="center">sed, a stream editor</h1>
<a name="SEC_Contents"></a>
<h2 class="contents-heading">Table of Contents</h2>
<div class="contents">
<ul class="no-bullet">
<li><a name="toc-Introduction-1" href="#Introduction">1 Introduction</a></li>
<li><a name="toc-Running-sed" href="#Invoking-sed">2 Running sed</a>
<ul class="no-bullet">
<li><a name="toc-Overview-1" href="#Overview">2.1 Overview</a></li>
<li><a name="toc-Command_002dLine-Options-1" href="#Command_002dLine-Options">2.2 Command-Line Options</a></li>
<li><a name="toc-Exit-status-1" href="#Exit-status">2.3 Exit status</a></li>
</ul></li>
<li><a name="toc-sed-scripts-1" href="#sed-scripts">3 <code>sed</code> scripts</a>
<ul class="no-bullet">
<li><a name="toc-sed-script-overview-1" href="#sed-script-overview">3.1 <code>sed</code> script overview</a></li>
<li><a name="toc-sed-commands-summary" href="#sed-commands-list">3.2 <code>sed</code> commands summary</a></li>
<li><a name="toc-The-s-Command" href="#The-_0022s_0022-Command">3.3 The <code>s</code> Command</a></li>
<li><a name="toc-Often_002dUsed-Commands" href="#Common-Commands">3.4 Often-Used Commands</a></li>
<li><a name="toc-Less-Frequently_002dUsed-Commands" href="#Other-Commands">3.5 Less Frequently-Used Commands</a></li>
<li><a name="toc-Commands-for-sed-gurus" href="#Programming-Commands">3.6 Commands for <code>sed</code> gurus</a></li>
<li><a name="toc-Commands-Specific-to-GNU-sed" href="#Extended-Commands">3.7 Commands Specific to <acronym>GNU</acronym> <code>sed</code></a></li>
<li><a name="toc-Multiple-commands-syntax-1" href="#Multiple-commands-syntax">3.8 Multiple commands syntax</a>
<ul class="no-bullet">
<li><a name="toc-Commands-Requiring-a-newline" href="#Commands-Requiring-a-newline">3.8.1 Commands Requiring a newline</a></li>
</ul></li>
</ul></li>
<li><a name="toc-Addresses_003a-selecting-lines" href="#sed-addresses">4 Addresses: selecting lines</a>
<ul class="no-bullet">
<li><a name="toc-Addresses-overview-1" href="#Addresses-overview">4.1 Addresses overview</a></li>
<li><a name="toc-Selecting-lines-by-numbers" href="#Numeric-Addresses">4.2 Selecting lines by numbers</a></li>
<li><a name="toc-selecting-lines-by-text-matching" href="#Regexp-Addresses">4.3 selecting lines by text matching</a></li>
<li><a name="toc-Range-Addresses-1" href="#Range-Addresses">4.4 Range Addresses</a></li>
</ul></li>
<li><a name="toc-Regular-Expressions_003a-selecting-text" href="#sed-regular-expressions">5 Regular Expressions: selecting text</a>
<ul class="no-bullet">
<li><a name="toc-Overview-of-regular-expression-in-sed" href="#Regular-Expressions-Overview">5.1 Overview of regular expression in <code>sed</code></a></li>
<li><a name="toc-Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression" href="#BRE-vs-ERE">5.2 Basic (BRE) and extended (ERE) regular expression</a></li>
<li><a name="toc-Overview-of-basic-regular-expression-syntax" href="#BRE-syntax">5.3 Overview of basic regular expression syntax</a></li>
<li><a name="toc-Overview-of-extended-regular-expression-syntax" href="#ERE-syntax">5.4 Overview of extended regular expression syntax</a></li>
<li><a name="toc-Character-Classes-and-Bracket-Expressions-1" href="#Character-Classes-and-Bracket-Expressions">5.5 Character Classes and Bracket Expressions</a></li>
<li><a name="toc-regular-expression-extensions" href="#regexp-extensions">5.6 regular expression extensions</a></li>
<li><a name="toc-Back_002dreferences-and-Subexpressions-1" href="#Back_002dreferences-and-Subexpressions">5.7 Back-references and Subexpressions</a></li>
<li><a name="toc-Escape-Sequences-_002d-specifying-special-characters" href="#Escapes">5.8 Escape Sequences - specifying special characters</a></li>
<li><a name="toc-Locale-Considerations-1" href="#Locale-Considerations">5.9 Locale Considerations</a></li>
</ul></li>
<li><a name="toc-Advanced-sed_003a-cycles-and-buffers" href="#advanced-sed">6 Advanced <code>sed</code>: cycles and buffers</a>
<ul class="no-bullet">
<li><a name="toc-How-sed-Works" href="#Execution-Cycle">6.1 How <code>sed</code> Works</a></li>
<li><a name="toc-Hold-and-Pattern-Buffers-1" href="#Hold-and-Pattern-Buffers">6.2 Hold and Pattern Buffers</a></li>
<li><a name="toc-Multiline-techniques-_002d-using-D_002cG_002cH_002cN_002cP-to-process-multiple-lines" href="#Multiline-techniques">6.3 Multiline techniques - using D,G,H,N,P to process multiple lines</a></li>
<li><a name="toc-Branching-and-Flow-Control" href="#Branching-and-flow-control">6.4 Branching and Flow Control</a>
<ul class="no-bullet">
<li><a name="toc-Branching-and-Cycles" href="#Branching-and-Cycles">6.4.1 Branching and Cycles</a></li>
<li><a name="toc-Branching-example_003a-joining-lines" href="#Branching-example_003a-joining-lines">6.4.2 Branching example: joining lines</a></li>
</ul></li>
</ul></li>
<li><a name="toc-Some-Sample-Scripts" href="#Examples">7 Some Sample Scripts</a>
<ul class="no-bullet">
<li><a name="toc-Joining-lines-1" href="#Joining-lines">7.1 Joining lines</a></li>
<li><a name="toc-Centering-Lines" href="#Centering-lines">7.2 Centering Lines</a></li>
<li><a name="toc-Increment-a-Number" href="#Increment-a-number">7.3 Increment a Number</a></li>
<li><a name="toc-Rename-Files-to-Lower-Case" href="#Rename-files-to-lower-case">7.4 Rename Files to Lower Case</a></li>
<li><a name="toc-Print-bash-Environment" href="#Print-bash-environment">7.5 Print <code>bash</code> Environment</a></li>
<li><a name="toc-Reverse-Characters-of-Lines" href="#Reverse-chars-of-lines">7.6 Reverse Characters of Lines</a></li>
<li><a name="toc-Text-search-across-multiple-lines-1" href="#Text-search-across-multiple-lines">7.7 Text search across multiple lines</a></li>
<li><a name="toc-Line-length-adjustment-1" href="#Line-length-adjustment">7.8 Line length adjustment</a></li>
<li><a name="toc-Reverse-Lines-of-Files" href="#tac">7.9 Reverse Lines of Files</a></li>
<li><a name="toc-Numbering-Lines" href="#cat-_002dn">7.10 Numbering Lines</a></li>
<li><a name="toc-Numbering-Non_002dblank-Lines" href="#cat-_002db">7.11 Numbering Non-blank Lines</a></li>
<li><a name="toc-Counting-Characters" href="#wc-_002dc">7.12 Counting Characters</a></li>
<li><a name="toc-Counting-Words" href="#wc-_002dw">7.13 Counting Words</a></li>
<li><a name="toc-Counting-Lines" href="#wc-_002dl">7.14 Counting Lines</a></li>
<li><a name="toc-Printing-the-First-Lines" href="#head">7.15 Printing the First Lines</a></li>
<li><a name="toc-Printing-the-Last-Lines" href="#tail">7.16 Printing the Last Lines</a></li>
<li><a name="toc-Make-Duplicate-Lines-Unique" href="#uniq">7.17 Make Duplicate Lines Unique</a></li>
<li><a name="toc-Print-Duplicated-Lines-of-Input" href="#uniq-_002dd">7.18 Print Duplicated Lines of Input</a></li>
<li><a name="toc-Remove-All-Duplicated-Lines" href="#uniq-_002du">7.19 Remove All Duplicated Lines</a></li>
<li><a name="toc-Squeezing-Blank-Lines" href="#cat-_002ds">7.20 Squeezing Blank Lines</a></li>
</ul></li>
<li><a name="toc-GNU-sed_0027s-Limitations-and-Non_002dlimitations" href="#Limitations">8 <acronym>GNU</acronym> <code>sed</code>’s Limitations and Non-limitations</a></li>
<li><a name="toc-Other-Resources-for-Learning-About-sed" href="#Other-Resources">9 Other Resources for Learning About <code>sed</code></a></li>
<li><a name="toc-Reporting-Bugs-1" href="#Reporting-Bugs">10 Reporting Bugs</a></li>
<li><a name="toc-GNU-Free-Documentation-License-1" href="#GNU-Free-Documentation-License">Appendix A GNU Free Documentation License</a></li>
<li><a name="toc-Concept-Index-1" href="#Concept-Index">Concept Index</a></li>
<li><a name="toc-Command-and-Option-Index-1" href="#Command-and-Option-Index">Command and Option Index</a></li>
</ul>
</div>
<a name="Top"></a>
<div class="header">
<p>
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a>, Up: <a href="dir.html#Top" accesskey="u" rel="up">(dir)</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="GNU-sed"></a>
<h1 class="top"><acronym>GNU</acronym> <code>sed</code></h1>
<p>This file documents version 4.4 of
<acronym>GNU</acronym> <code>sed</code>, a stream editor.
</p>
<p>Copyright © 1998-2017 Free Software Foundation, Inc.
</p>
<blockquote>
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no
Back-Cover Texts. A copy of the license is included in the
section entitled “GNU Free Documentation License”.
</p></blockquote>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Introduction" accesskey="1">Introduction</a>:</td><td> </td><td align="left" valign="top">Introduction
</td></tr>
<tr><td align="left" valign="top">• <a href="#Invoking-sed" accesskey="2">Invoking sed</a>:</td><td> </td><td align="left" valign="top">Invocation
</td></tr>
<tr><td align="left" valign="top">• <a href="#sed-scripts" accesskey="3">sed scripts</a>:</td><td> </td><td align="left" valign="top"><code>sed</code> scripts
</td></tr>
<tr><td align="left" valign="top">• <a href="#sed-addresses" accesskey="4">sed addresses</a>:</td><td> </td><td align="left" valign="top">Addresses: selecting lines
</td></tr>
<tr><td align="left" valign="top">• <a href="#sed-regular-expressions" accesskey="5">sed regular expressions</a>:</td><td> </td><td align="left" valign="top">Regular expressions: selecting text
</td></tr>
<tr><td align="left" valign="top">• <a href="#advanced-sed" accesskey="6">advanced sed</a>:</td><td> </td><td align="left" valign="top">Advanced <code>sed</code>: cycles and buffers
</td></tr>
<tr><td align="left" valign="top">• <a href="#Examples" accesskey="7">Examples</a>:</td><td> </td><td align="left" valign="top">Some sample scripts
</td></tr>
<tr><td align="left" valign="top">• <a href="#Limitations" accesskey="8">Limitations</a>:</td><td> </td><td align="left" valign="top">Limitations and (non-)limitations of <acronym>GNU</acronym> <code>sed</code>
</td></tr>
<tr><td align="left" valign="top">• <a href="#Other-Resources" accesskey="9">Other Resources</a>:</td><td> </td><td align="left" valign="top">Other resources for learning about <code>sed</code>
</td></tr>
<tr><td align="left" valign="top">• <a href="#Reporting-Bugs">Reporting Bugs</a>:</td><td> </td><td align="left" valign="top">Reporting bugs
</td></tr>
<tr><td align="left" valign="top">• <a href="#GNU-Free-Documentation-License">GNU Free Documentation License</a>:</td><td> </td><td align="left" valign="top">Copying and sharing this manual
</td></tr>
<tr><td align="left" valign="top">• <a href="#Concept-Index">Concept Index</a>:</td><td> </td><td align="left" valign="top">A menu with all the topics in this manual.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Command-and-Option-Index">Command and Option Index</a>:</td><td> </td><td align="left" valign="top">A menu with all <code>sed</code> commands and
command-line options.
</td></tr>
</table>
<hr>
<a name="Introduction"></a>
<div class="header">
<p>
Next: <a href="#Invoking-sed" accesskey="n" rel="next">Invoking sed</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Introduction-1"></a>
<h2 class="chapter">1 Introduction</h2>
<a name="index-Stream-editor"></a>
<p><code>sed</code> is a stream editor.
A stream editor is used to perform basic text
transformations on an input stream
(a file or input from a pipeline).
While in some ways similar to an editor which
permits scripted edits (such as <code>ed</code>),
<code>sed</code> works by making only one pass over the
input(s), and is consequently more efficient.
But it is <code>sed</code>’s ability to filter text in a pipeline
which particularly distinguishes it from other types of
editors.
</p>
<hr>
<a name="Invoking-sed"></a>
<div class="header">
<p>
Next: <a href="#sed-scripts" accesskey="n" rel="next">sed scripts</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Running-sed"></a>
<h2 class="chapter">2 Running sed</h2>
<p>This chapter covers how to run <code>sed</code>. Details of <code>sed</code>
scripts and individual <code>sed</code> commands are discussed in the
next chapter.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Overview" accesskey="1">Overview</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Command_002dLine-Options" accesskey="2">Command-Line Options</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">• <a href="#Exit-status" accesskey="3">Exit status</a>:</td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Overview"></a>
<div class="header">
<p>
Next: <a href="#Command_002dLine-Options" accesskey="n" rel="next">Command-Line Options</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Overview-1"></a>
<h3 class="section">2.1 Overview</h3>
<p>Normally <code>sed</code> is invoked like this:
</p>
<div class="example">
<pre class="example">sed SCRIPT INPUTFILE...
</pre></div>
<p>For example, to replace all occurrences of ‘<samp>hello</samp>’ to ‘<samp>world</samp>’
in the file <samp>input.txt</samp>:
</p>
<div class="example">
<pre class="example">sed 's/hello/world/' input.txt > output.txt
</pre></div>
<a name="index-stdin"></a>
<a name="index-standard-input"></a>
<p>If you do not specify <var>INPUTFILE</var>, or if <var>INPUTFILE</var> is <samp>-</samp>,
<code>sed</code> filters the contents of the standard input. The following
commands are equivalent:
</p>
<div class="example">
<pre class="example">sed 's/hello/world/' input.txt > output.txt
sed 's/hello/world/' < input.txt > output.txt
cat input.txt | sed 's/hello/world/' - > output.txt
</pre></div>
<a name="index-stdout"></a>
<a name="index-output"></a>
<a name="index-standard-output"></a>
<a name="index-_002di_002c-example"></a>
<p><code>sed</code> writes output to standard output. Use <samp>-i</samp> to edit
files in-place instead of printing to standard output.
See also the <code>W</code> and <code>s///w</code> commands for writing output to
other files. The following command modifies <samp>file.txt</samp> and
does not produce any output:
</p>
<div class="example">
<pre class="example">sed -i 's/hello/world' file.txt
</pre></div>
<a name="index-_002dn_002c-example"></a>
<a name="index-p_002c-example"></a>
<a name="index-suppressing-output"></a>
<a name="index-output_002c-suppressing"></a>
<p>By default <code>sed</code> prints all processed input (except input
that has been modified/deleted by commands such as <code>d</code>).
Use <samp>-n</samp> to suppress output, and the <code>p</code> command
to print specific lines. The following command prints only line 45
of the input file:
</p>
<div class="example">
<pre class="example">sed -n '45p' file.txt
</pre></div>
<a name="index-multiple-files"></a>
<a name="index-_002ds_002c-example"></a>
<p><code>sed</code> treats multiple input files as one long stream.
The following example prints the first line of the first file
(<samp>one.txt</samp>) and the last line of the last file (<samp>three.txt</samp>).
Use <samp>-s</samp> to reverse this behavior.
</p>
<div class="example">
<pre class="example">sed -n '1p ; $p' one.txt two.txt three.txt
</pre></div>
<a name="index-_002de_002c-example"></a>
<a name="index-_002d_002dexpression_002c-example"></a>
<a name="index-_002df_002c-example"></a>
<a name="index-_002d_002dfile_002c-example"></a>
<a name="index-script-parameter"></a>
<a name="index-parameters_002c-script"></a>
<p>Without <samp>-e</samp> or <samp>-f</samp> options, <code>sed</code> uses
the first non-option parameter as the <var>script</var>, and the following
non-option parameters as input files.
If <samp>-e</samp> or <samp>-f</samp> options are used to specify a <var>script</var>,
all non-option parameters are taken as input files.
Options <samp>-e</samp> and <samp>-f</samp> can be combined, and can appear
multiple times (in which case the final effective <var>script</var> will be
concatenation of all the individual <var>script</var>s).
</p>
<p>The following examples are equivalent:
</p>
<div class="example">
<pre class="example">sed 's/hello/world/' input.txt > output.txt
sed -e 's/hello/world/' input.txt > output.txt
sed --expression='s/hello/world/' input.txt > output.txt
echo 's/hello/world/' > myscript.sed
sed -f myscript.sed input.txt > output.txt
sed --file=myscript.sed input.txt > output.txt
</pre></div>
<hr>
<a name="Command_002dLine-Options"></a>
<div class="header">
<p>
Next: <a href="#Exit-status" accesskey="n" rel="next">Exit status</a>, Previous: <a href="#Overview" accesskey="p" rel="prev">Overview</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Command_002dLine-Options-1"></a>
<h3 class="section">2.2 Command-Line Options</h3>
<p>The full format for invoking <code>sed</code> is:
</p>
<div class="example">
<pre class="example">sed OPTIONS... [SCRIPT] [INPUTFILE...]
</pre></div>
<p><code>sed</code> may be invoked with the following command-line options:
</p>
<dl compact="compact">
<dt><code>--version</code></dt>
<dd><a name="index-_002d_002dversion"></a>
<a name="index-Version_002c-printing"></a>
<p>Print out the version of <code>sed</code> that is being run and a copyright notice,
then exit.
</p>
</dd>
<dt><code>--help</code></dt>
<dd><a name="index-_002d_002dhelp"></a>
<a name="index-Usage-summary_002c-printing"></a>
<p>Print a usage message briefly summarizing these command-line options
and the bug-reporting address,
then exit.
</p>
</dd>
<dt><code>-n</code></dt>
<dt><code>--quiet</code></dt>
<dt><code>--silent</code></dt>
<dd><a name="index-_002dn"></a>
<a name="index-_002d_002dquiet"></a>
<a name="index-_002d_002dsilent"></a>
<a name="index-Disabling-autoprint_002c-from-command-line"></a>
<p>By default, <code>sed</code> prints out the pattern space
at the end of each cycle through the script (see <a href="#Execution-Cycle">How <code>sed</code> works</a>).
These options disable this automatic printing,
and <code>sed</code> only produces output when explicitly told to
via the <code>p</code> command.
</p>
</dd>
<dt><code>-e <var>script</var></code></dt>
<dt><code>--expression=<var>script</var></code></dt>
<dd><a name="index-_002de"></a>
<a name="index-_002d_002dexpression"></a>
<a name="index-Script_002c-from-command-line"></a>
<p>Add the commands in <var>script</var> to the set of commands to be
run while processing the input.
</p>
</dd>
<dt><code>-f <var>script-file</var></code></dt>
<dt><code>--file=<var>script-file</var></code></dt>
<dd><a name="index-_002df"></a>
<a name="index-_002d_002dfile"></a>
<a name="index-Script_002c-from-a-file"></a>
<p>Add the commands contained in the file <var>script-file</var>
to the set of commands to be run while processing the input.
</p>
</dd>
<dt><code>-i[<var>SUFFIX</var>]</code></dt>
<dt><code>--in-place[=<var>SUFFIX</var>]</code></dt>
<dd><a name="index-_002di"></a>
<a name="index-_002d_002din_002dplace"></a>
<a name="index-In_002dplace-editing_002c-activating"></a>
<a name="index-GNU-extensions_002c-in_002dplace-editing"></a>
<p>This option specifies that files are to be edited in-place.
<acronym>GNU</acronym> <code>sed</code> does this by creating a temporary file and
sending output to this file rather than to the standard
output.<a name="DOCF1" href="#FOOT1"><sup>1</sup></a>.
</p>
<p>This option implies <samp>-s</samp>.
</p>
<p>When the end of the file is reached, the temporary file is
renamed to the output file’s original name. The extension,
if supplied, is used to modify the name of the old file
before renaming the temporary file, thereby making a backup
copy<a name="DOCF2" href="#FOOT2"><sup>2</sup></a>).
</p>
<a name="index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names"></a>
<p>This rule is followed: if the extension doesn’t contain a <code>*</code>,
then it is appended to the end of the current filename as a
suffix; if the extension does contain one or more <code>*</code>
characters, then <em>each</em> asterisk is replaced with the
current filename. This allows you to add a prefix to the
backup file, instead of (or in addition to) a suffix, or
even to place backup copies of the original files into another
directory (provided the directory already exists).
</p>
<p>If no extension is supplied, the original file is
overwritten without making a backup.
</p>
</dd>
<dt><code>-l <var>N</var></code></dt>
<dt><code>--line-length=<var>N</var></code></dt>
<dd><a name="index-_002dl"></a>
<a name="index-_002d_002dline_002dlength"></a>
<a name="index-Line-length_002c-setting"></a>
<p>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.
</p>
</dd>
<dt><code>--posix</code></dt>
<dd><a name="index-_002d_002dposix"></a>
<a name="index-GNU-extensions_002c-disabling"></a>
<p><acronym>GNU</acronym> <code>sed</code> includes several extensions to <acronym>POSIX</acronym>
sed. In order to simplify writing portable scripts, this
option disables all the extensions that this manual documents,
including additional commands.
<a name="index-POSIXLY_005fCORRECT-behavior_002c-enabling"></a>
Most of the extensions accept <code>sed</code> programs that
are outside the syntax mandated by <acronym>POSIX</acronym>, but some
of them (such as the behavior of the <code>N</code> command
described in <a href="#Reporting-Bugs">Reporting Bugs</a>) actually violate the
standard. If you want to disable only the latter kind of
extension, you can set the <code>POSIXLY_CORRECT</code> variable
to a non-empty value.
</p>
</dd>
<dt><code>-b</code></dt>
<dt><code>--binary</code></dt>
<dd><a name="index-_002db"></a>
<a name="index-_002d_002dbinary"></a>
<p>This option is available on every platform, but is only effective where the
operating system makes a distinction between text files and binary files.
When such a distinction is made—as is the case for MS-DOS, Windows,
Cygwin—text files are composed of lines separated by a carriage return
<em>and</em> a line feed character, and <code>sed</code> does not see the
ending CR. When this option is specified, <code>sed</code> will open
input files in binary mode, thus not requesting this special processing
and considering lines to end at a line feed.
</p>
</dd>
<dt><code>--follow-symlinks</code></dt>
<dd><a name="index-_002d_002dfollow_002dsymlinks"></a>
<p>This option is available only on platforms that support
symbolic links and has an effect only if option <samp>-i</samp>
is specified. In this case, if the file that is specified
on the command line is a symbolic link, <code>sed</code> will
follow the link and edit the ultimate destination of the
link. The default behavior is to break the symbolic link,
so that the link destination will not be modified.
</p>
</dd>
<dt><code>-E</code></dt>
<dt><code>-r</code></dt>
<dt><code>--regexp-extended</code></dt>
<dd><a name="index-_002dE"></a>
<a name="index-_002dr"></a>
<a name="index-_002d_002dregexp_002dextended"></a>
<a name="index-Extended-regular-expressions_002c-choosing"></a>
<a name="index-GNU-extensions_002c-extended-regular-expressions"></a>
<p>Use extended regular expressions rather than basic
regular expressions. Extended regexps are those that
<code>egrep</code> accepts; they can be clearer because they
usually have fewer backslashes.
Historically this was a <acronym>GNU</acronym> extension,
but the <samp>-E</samp>
extension has since been added to the POSIX standard
(http://austingroupbugs.net/view.php?id=528),
so use <samp>-E</samp> for portability.
GNU sed has accepted <samp>-E</samp> as an undocumented option for years,
and *BSD seds have accepted <samp>-E</samp> for years as well,
but scripts that use <samp>-E</samp> might not port to other older systems.
See <a href="#ERE-syntax">Extended regular expressions</a>.
</p>
</dd>
<dt><code>-s</code></dt>
<dt><code>--separate</code></dt>
<dd><a name="index-_002ds"></a>
<a name="index-_002d_002dseparate"></a>
<a name="index-Working-on-separate-files"></a>
<p>By default, <code>sed</code> will consider the files specified on the
command line as a single continuous long stream. This <acronym>GNU</acronym> <code>sed</code>
extension allows the user to consider them as separate files:
range addresses (such as ‘<samp>/abc/,/def/</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.
</p>
</dd>
<dt><code>--sandbox</code></dt>
<dd><a name="index-_002d_002dsandbox"></a>
<a name="index-Sandbox-mode"></a>
<p>In sandbox mode, <code>e/w/r</code> commands are rejected - programs containing
them will be aborted without being run. Sandbox mode ensures <code>sed</code>
operates only on the input files designated on the command line, and
cannot run external programs.
</p>
</dd>
<dt><code>-u</code></dt>
<dt><code>--unbuffered</code></dt>
<dd><a name="index-_002du"></a>
<a name="index-_002d_002dunbuffered"></a>
<a name="index-Unbuffered-I_002fO_002c-choosing"></a>
<p>Buffer both input and output as minimally as practical.
(This is particularly useful if the input is coming from
the likes of ‘<samp>tail -f</samp>’, and you wish to see the transformed
output as soon as possible.)
</p>
</dd>
<dt><code>-z</code></dt>
<dt><code>--null-data</code></dt>
<dt><code>--zero-terminated</code></dt>
<dd><a name="index-_002dz"></a>
<a name="index-_002d_002dnull_002ddata"></a>
<a name="index-_002d_002dzero_002dterminated"></a>
<p>Treat the input as a set of lines, each terminated by a zero byte
(the ASCII ‘<samp>NUL</samp>’ character) instead of a newline. This option can
be used with commands like ‘<samp>sort -z</samp>’ and ‘<samp>find -print0</samp>’
to process arbitrary file names.
</p></dd>
</dl>
<p>If no <samp>-e</samp>, <samp>-f</samp>, <samp>--expression</samp>, or <samp>--file</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"></a>
<p>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"></a>
A file name of ‘<samp>-</samp>’ refers to the standard input stream.
The standard input will be processed if no file names are specified.
</p>
<hr>
<a name="Exit-status"></a>
<div class="header">
<p>
Previous: <a href="#Command_002dLine-Options" accesskey="p" rel="prev">Command-Line Options</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Exit-status-1"></a>
<h3 class="section">2.3 Exit status</h3>
<a name="index-exit-status"></a>
<p>An exit status of zero indicates success, and a nonzero value
indicates failure. <acronym>GNU</acronym> <code>sed</code> returns the following exit status
error values:
</p>
<dl compact="compact">
<dt>0</dt>
<dd><p>Successful completion.
</p>
</dd>
<dt>1</dt>
<dd><p>Invalid command, invalid syntax, invalid regular expression or a
<acronym>GNU</acronym> <code>sed</code> extension command used with <samp>--posix</samp>.
</p>
</dd>
<dt>2</dt>
<dd><p>One or more of the input file specified on the command line could not be
opened (e.g. if a file is not found, or read permission is denied).
Processing continued with other files.
</p>
</dd>
<dt>4</dt>
<dd><p>An I/O error, or a serious processing error during runtime,
<acronym>GNU</acronym> <code>sed</code> aborted immediately.
</p></dd>
</dl>
<a name="index-Q_002c-example"></a>
<a name="index-exit-status_002c-example"></a>
<p>Additionally, the commands <code>q</code> and <code>Q</code> can be used to terminate
<code>sed</code> with a custom exit code value (this is a <acronym>GNU</acronym> <code>sed</code> extension):
</p>
<div class="example">
<pre class="example">$ echo | sed 'Q42' ; echo $?
42
</pre></div>
<hr>
<a name="sed-scripts"></a>
<div class="header">
<p>
Next: <a href="#sed-addresses" accesskey="n" rel="next">sed addresses</a>, Previous: <a href="#Invoking-sed" accesskey="p" rel="prev">Invoking sed</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="sed-scripts-1"></a>
<h2 class="chapter">3 <code>sed</code> scripts</h2>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#sed-script-overview" accesskey="1">sed script overview</a>:</td><td> </td><td align="left" valign="top"><code>sed</code> script overview
</td></tr>
<tr><td align="left" valign="top">• <a href="#sed-commands-list" accesskey="2">sed commands list</a>:</td><td> </td><td align="left" valign="top"><code>sed</code> commands summary
</td></tr>
<tr><td align="left" valign="top">• <a href="#The-_0022s_0022-Command" accesskey="3">The "s" Command</a>:</td><td> </td><td align="left" valign="top"><code>sed</code>’s Swiss Army Knife
</td></tr>
<tr><td align="left" valign="top">• <a href="#Common-Commands" accesskey="4">Common Commands</a>:</td><td> </td><td align="left" valign="top">Often used commands
</td></tr>
<tr><td align="left" valign="top">• <a href="#Other-Commands" accesskey="5">Other Commands</a>:</td><td> </td><td align="left" valign="top">Less frequently used commands
</td></tr>
<tr><td align="left" valign="top">• <a href="#Programming-Commands" accesskey="6">Programming Commands</a>:</td><td> </td><td align="left" valign="top">Commands for <code>sed</code> gurus
</td></tr>
<tr><td align="left" valign="top">• <a href="#Extended-Commands" accesskey="7">Extended Commands</a>:</td><td> </td><td align="left" valign="top">Commands specific of <acronym>GNU</acronym> <code>sed</code>
</td></tr>
<tr><td align="left" valign="top">• <a href="#Multiple-commands-syntax" accesskey="8">Multiple commands syntax</a>:</td><td> </td><td align="left" valign="top">Extension for easier scripting
</td></tr>
</table>
<hr>
<a name="sed-script-overview"></a>
<div class="header">
<p>
Next: <a href="#sed-commands-list" accesskey="n" rel="next">sed commands list</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="sed-script-overview-1"></a>
<h3 class="section">3.1 <code>sed</code> script overview</h3>
<a name="index-sed-script-structure"></a>
<a name="index-Script-structure"></a>
<p>A <code>sed</code> program consists of one or more <code>sed</code> commands,
passed in by one or more of the
<samp>-e</samp>, <samp>-f</samp>, <samp>--expression</samp>, and <samp>--file</samp>
options, or the first non-option argument if zero of these
options are used.
This document will refer to “the” <code>sed</code> script;
this is understood to mean the in-order concatenation
of all of the <var>script</var>s and <var>script-file</var>s passed in.
See <a href="#Overview">Overview</a>.
</p>
<a name="index-sed-commands-syntax"></a>
<a name="index-syntax_002c-sed-commands"></a>
<a name="index-addresses_002c-syntax"></a>
<a name="index-syntax_002c-addresses"></a>
<p><code>sed</code> commands follow this syntax:
</p>
<div class="example">
<pre class="example">[addr]<var>X</var>[options]
</pre></div>
<p><var>X</var> is a single-letter <code>sed</code> command.
<code>[addr]</code> is an optional line address. If <code>[addr]</code> is specified,
the command <var>X</var> will be executed only on the matched lines.
<code>[addr]</code> can be a single line number, a regular expression,
or a range of lines (see <a href="#sed-addresses">sed addresses</a>).
Additional <code>[options]</code> are used for some <code>sed</code> commands.
</p>
<a name="index-d_002c-example"></a>
<a name="index-address-range_002c-example"></a>
<a name="index-example_002c-address-range"></a>
<p>The following example deletes lines 30 to 35 in the input.
<code>30,35</code> is an address range. <code>d</code> is the delete command:
</p>
<div class="example">
<pre class="example">sed '30,35d' input.txt > output.txt
</pre></div>
<a name="index-q_002c-example"></a>
<a name="index-regular-expression_002c-example"></a>
<a name="index-example_002c-regular-expression"></a>
<p>The following example prints all input until a line
starting with the word ‘<samp>foo</samp>’ is found. If such line is found,
<code>sed</code> will terminate with exit status 42.
If such line was not found (and no other error occurred), <code>sed</code>
will exit with status 0.
<code>/^foo/</code> is a regular-expression address.
<code>q</code> is the quit command. <code>42</code> is the command option.
</p>
<div class="example">
<pre class="example">sed '/^foo/q42' input.txt > output.txt
</pre></div>
<a name="index-multiple-sed-commands"></a>
<a name="index-sed-commands_002c-multiple"></a>
<a name="index-newline_002c-command-separator"></a>
<a name="index-semicolons_002c-command-separator"></a>
<a name="index-_003b_002c-command-separator"></a>
<a name="index-_002de_002c-example-1"></a>
<a name="index-_002df_002c-example-1"></a>
<p>Commands within a <var>script</var> or <var>script-file</var> can be
separated by semicolons (<code>;</code>) or newlines (ASCII 10).
Multiple scripts can be specified with <samp>-e</samp> or <samp>-f</samp>
options.
</p>
<p>The following examples are all equivalent. They perform two <code>sed</code>
operations: deleting any lines matching the regular expression <code>/^foo/</code>,
and replacing all occurrences of the string ‘<samp>hello</samp>’ with ‘<samp>world</samp>’:
</p>
<div class="example">
<pre class="example">sed '/^foo/d ; s/hello/world/' input.txt > output.txt
sed -e '/^foo/d' -e 's/hello/world/' input.txt > output.txt
echo '/^foo/d' > script.sed
echo 's/hello/world/' >> script.sed
sed -f script.sed input.txt > output.txt
echo 's/hello/world/' > script2.sed
sed -e '/^foo/d' -f script2.sed input.txt > output.txt
</pre></div>
<a name="index-a_002c-and-semicolons"></a>
<a name="index-c_002c-and-semicolons"></a>
<a name="index-i_002c-and-semicolons"></a>
<p>Commands <code>a</code>, <code>c</code>, <code>i</code>, due to their syntax,
cannot be followed by semicolons working as command separators and
thus should be terminated
with newlines or be placed at the end of a <var>script</var> or <var>script-file</var>.
Commands can also be preceded with optional non-significant
whitespace characters.
See <a href="#Multiple-commands-syntax">Multiple commands syntax</a>.
</p>
<hr>
<a name="sed-commands-list"></a>
<div class="header">
<p>
Next: <a href="#The-_0022s_0022-Command" accesskey="n" rel="next">The "s" Command</a>, Previous: <a href="#sed-script-overview" accesskey="p" rel="prev">sed script overview</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="sed-commands-summary"></a>
<h3 class="section">3.2 <code>sed</code> commands summary</h3>
<p>The following commands are supported in <acronym>GNU</acronym> <code>sed</code>.
Some are standard POSIX commands, while other are <acronym>GNU</acronym> extensions.
Details and examples for each command are in the following sections.
(Mnemonics) are shown in parentheses.
</p>
<dl compact="compact">
<dt><code>a\</code></dt>
<dt><code><var>text</var></code></dt>
<dd><p>Append <var>text</var> after a line.
</p>
</dd>
<dt><code>a <var>text</var></code></dt>
<dd><p>Append <var>text</var> after a line (alternative syntax).
</p>
</dd>
<dt><code>b <var>label</var></code></dt>
<dd><p>Branch unconditionally to <var>label</var>.
The <var>label</var> may be omitted, in which case the next cycle is started.
</p>
</dd>
<dt><code>c\</code></dt>
<dt><code><var>text</var></code></dt>
<dd><p>Replace (change) lines with <var>text</var>.
</p>
</dd>
<dt><code>c <var>text</var></code></dt>
<dd><p>Replace (change) lines with <var>text</var> (alternative syntax).
</p>
</dd>
<dt><code>d</code></dt>
<dd><p>Delete the pattern space;
immediately start next cycle.
</p>
</dd>
<dt><code>D</code></dt>
<dd><p>If pattern space contains newlines, delete text in the pattern
space up to the first newline, and restart cycle with the resultant
pattern space, without reading a new line of input.
</p>
<p>If pattern space contains no newline, start a normal new cycle as if
the <code>d</code> command was issued.
</p>
</dd>
<dt><code>e</code></dt>
<dd><p>Executes the command that is found in pattern space and
replaces the pattern space with the output; a trailing newline
is suppressed.
</p>
</dd>
<dt><code>e <var>command</var></code></dt>
<dd><p>Executes <var>command</var> and sends its output to the output stream.
The command can run across multiple lines, all but the last ending with
a back-slash.
</p>
</dd>
<dt><code>F</code></dt>
<dd><p>(filename) Print the file name of the current input file (with a trailing
newline).
</p>
</dd>
<dt><code>g</code></dt>
<dd><p>Replace the contents of the pattern space with the contents of the hold space.
</p>
</dd>
<dt><code>G</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>h</code></dt>
<dd><p>(hold) Replace the contents of the hold space with the contents of the
pattern space.
</p>
</dd>
<dt><code>H</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>i\</code></dt>
<dt><code><var>text</var></code></dt>
<dd><p>insert <var>text</var> before a line.
</p>
</dd>
<dt><code>i <var>text</var></code></dt>
<dd><p>insert <var>text</var> before a line (alternative syntax).
</p>
</dd>
<dt><code>l</code></dt>
<dd><p>Print the pattern space in an unambiguous form.
</p>
</dd>
<dt><code>n</code></dt>
<dd><p>(next) 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 <code>sed</code> exits without processing
any more commands.
</p>
</dd>
<dt><code>N</code></dt>
<dd><p>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 <code>sed</code> exits without processing
any more commands.
</p>
</dd>
<dt><code>p</code></dt>
<dd><p>Print the pattern space.
</p>
</dd>
<dt><code>P</code></dt>
<dd><p>Print the pattern space, up to the first <newline>.
</p>
</dd>
<dt><code>q<var>[exit-code]</var></code></dt>
<dd><p>(quit) Exit <code>sed</code> without processing any more commands or input.
</p>
</dd>
<dt><code>Q<var>[exit-code]</var></code></dt>
<dd><p>(quit) 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>
</dd>
<dt><code>r filename</code></dt>
<dd><p>Reads text file a file. Example:
</p>
</dd>
<dt><code>R filename</code></dt>
<dd><p>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.
</p>
</dd>
<dt><code>s<var>/regexp/replacement/[flags]</var></code></dt>
<dd><p>(substitute) Match the regular-expression against the content of the
pattern space. If found, replace matched string with
<var>replacement</var>.
</p>
</dd>
<dt><code>t <var>label</var></code></dt>
<dd><p>(test) 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.
</p>
</dd>
<dt><code>T <var>label</var></code></dt>
<dd><p>(test) 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.
</p>
</dd>
<dt><code>v <var>[version]</var></code></dt>
<dd><p>(version) This command does nothing, but makes <code>sed</code> fail if
<acronym>GNU</acronym> <code>sed</code> extensions are not supported, or if the requested version
is not available.
</p>
</dd>
<dt><code>w filename</code></dt>
<dd><p>Write the pattern space to <var>filename</var>.
</p>
</dd>
<dt><code>W filename</code></dt>
<dd><p>Write to the given filename the portion of the pattern space up to
the first newline
</p>
</dd>
<dt><code>x</code></dt>
<dd><p>Exchange the contents of the hold and pattern spaces.
</p>
</dd>
<dt><code>y/src/dst/</code></dt>
<dd><p>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>
</dd>
<dt><code>z</code></dt>
<dd><p>(zap) This command empties the content of pattern space.
</p>
</dd>
<dt><code>#</code></dt>
<dd><p>A comment, until the next newline.
</p>
</dd>
<dt><code>{ <var>cmd ; cmd ...</var> }</code></dt>
<dd><p>Group several commands together.
</p>
</dd>
<dt><code>=</code></dt>
<dd><p>Print the current input line number (with a trailing newline).
</p>
</dd>
<dt><code>: <var>label</var></code></dt>
<dd><p>Specify the location of <var>label</var> for branch commands (<code>b</code>,
<code>t</code>, <code>T</code>).
</p>
</dd>
</dl>
<hr>
<a name="The-_0022s_0022-Command"></a>
<div class="header">
<p>
Next: <a href="#Common-Commands" accesskey="n" rel="next">Common Commands</a>, Previous: <a href="#sed-commands-list" accesskey="p" rel="prev">sed commands list</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-s-Command"></a>
<h3 class="section">3.3 The <code>s</code> Command</h3>
<p>The <code>s</code> command (as in substitute) is probably the most important
in <code>sed</code> and has a lot of different options. The syntax of
the <code>s</code> command is
‘<samp>s/<var>regexp</var>/<var>replacement</var>/<var>flags</var></samp>’.
</p>
<p>Its basic concept is simple: the <code>s</code> command attempts to match
the pattern space against the supplied regular expression <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>
<p>For details about <var>regexp</var> syntax see <a href="#Regexp-Addresses">Regular
Expression Addresses</a>.
</p>
<a name="index-Backreferences_002c-in-regular-expressions"></a>
<a name="index-Parenthesized-substrings"></a>
<p>The <var>replacement</var> can contain <code>\<var>n</var></code> (<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.
</p>
<p>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>
<a name="index-GNU-extensions_002c-case-modifiers-in-s-commands"></a>
<p>Finally, as a <acronym>GNU</acronym> <code>sed</code> 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:
</p>
<dl compact="compact">
<dt><code>\L</code></dt>
<dd><p>Turn the replacement
to lowercase until a <code>\U</code> or <code>\E</code> is found,
</p>
</dd>
<dt><code>\l</code></dt>
<dd><p>Turn the
next character to lowercase,
</p>
</dd>
<dt><code>\U</code></dt>
<dd><p>Turn the replacement to uppercase
until a <code>\L</code> or <code>\E</code> is found,
</p>
</dd>
<dt><code>\u</code></dt>
<dd><p>Turn the next character
to uppercase,
</p>
</dd>
<dt><code>\E</code></dt>
<dd><p>Stop case conversion started by <code>\L</code> or <code>\U</code>.
</p></dd>
</dl>
<p>When the <code>g</code> flag is being used, case conversion does not
propagate from one occurrence of the regular expression to
another. For example, when the following command is executed
with ‘<samp>a-b-</samp>’ in pattern space:
</p><div class="example">
<pre class="example">s/\(b\?\)-/x\u\1/g
</pre></div>
<p>the output is ‘<samp>axxB</samp>’. When replacing the first ‘<samp>-</samp>’,
the ‘<samp>\u</samp>’ sequence only affects the empty replacement of
‘<samp>\1</samp>’. It does not affect the <code>x</code> character that is
added to pattern space when replacing <code>b-</code> with <code>xB</code>.
</p>
<p>On the other hand, <code>\l</code> and <code>\u</code> do affect the remainder
of the replacement text if they are followed by an empty substitution