403Webshell
Server IP : 127.0.0.1  /  Your IP : 216.73.216.109
Web Server : Apache/2.4.54 (Win64) OpenSSL/1.1.1q PHP/8.1.10
System : Windows NT DESKTOP-E5T4RUN 10.0 build 19045 (Windows 10) AMD64
User : SERVERWEB ( 0)
PHP Version : 8.1.10
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : OFF |  Perl : OFF |  Python : OFF |  Sudo : OFF |  Pkexec : OFF
Directory :  C:/cygwin64/usr/share/doc/sed/html/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : C:/cygwin64/usr/share/doc/sed/html/sed.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.8, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents version 4.9 of
GNU sed, a stream editor.

Copyright (C) 1998-2022 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". -->
<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 name="viewport" content="width=device-width,initial-scale=1">

<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="#Introduction" rel="next" title="Introduction">
<style type="text/css">
<!--
a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {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}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
span:hover a.copiable-anchor {visibility: visible}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en">
<h1 class="settitle" align="center">sed, a stream editor</h1>


















<div class="top" id="Top">
<div class="header">
<p>
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="GNU-sed"></span><h1 class="top">GNU <code>sed</code></h1>

<p>This file documents version 4.9 of
GNU <code>sed</code>, a stream editor.
</p>
<p>Copyright &copy; 1998&ndash;2022 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 &ldquo;GNU Free Documentation License&rdquo;.
</p></blockquote>



<div class="Contents_element" id="SEC_Contents">
<h2 class="contents-heading">Table of Contents</h2>

<div class="contents">

<ul class="no-bullet">
  <li><a id="toc-Introduction-1" href="#Introduction">1 Introduction</a></li>
  <li><a id="toc-Running-sed" href="#Invoking-sed">2 Running sed</a>
  <ul class="no-bullet">
    <li><a id="toc-Overview-1" href="#Overview">2.1 Overview</a></li>
    <li><a id="toc-Command_002dLine-Options-1" href="#Command_002dLine-Options">2.2 Command-Line Options</a></li>
    <li><a id="toc-Exit-status-1" href="#Exit-status">2.3 Exit status</a></li>
  </ul></li>
  <li><a id="toc-sed-scripts-1" href="#sed-scripts">3 <code>sed</code> scripts</a>
  <ul class="no-bullet">
    <li><a id="toc-sed-script-overview-1" href="#sed-script-overview">3.1 <code>sed</code> script overview</a></li>
    <li><a id="toc-sed-commands-summary" href="#sed-commands-list">3.2 <code>sed</code> commands summary</a></li>
    <li><a id="toc-The-s-Command" href="#The-_0022s_0022-Command">3.3 The <code>s</code> Command</a></li>
    <li><a id="toc-Often_002dUsed-Commands" href="#Common-Commands">3.4 Often-Used Commands</a></li>
    <li><a id="toc-Less-Frequently_002dUsed-Commands" href="#Other-Commands">3.5 Less Frequently-Used Commands</a></li>
    <li><a id="toc-Commands-for-sed-gurus" href="#Programming-Commands">3.6 Commands for <code>sed</code> gurus</a></li>
    <li><a id="toc-Commands-Specific-to-GNU-sed" href="#Extended-Commands">3.7 Commands Specific to GNU <code>sed</code></a></li>
    <li><a id="toc-Multiple-commands-syntax-1" href="#Multiple-commands-syntax">3.8 Multiple commands syntax</a>
    <ul class="no-bullet">
      <li><a id="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 id="toc-Addresses_003a-selecting-lines" href="#sed-addresses">4 Addresses: selecting lines</a>
  <ul class="no-bullet">
    <li><a id="toc-Addresses-overview-1" href="#Addresses-overview">4.1 Addresses overview</a></li>
    <li><a id="toc-Selecting-lines-by-numbers" href="#Numeric-Addresses">4.2 Selecting lines by numbers</a></li>
    <li><a id="toc-selecting-lines-by-text-matching" href="#Regexp-Addresses">4.3 selecting lines by text matching</a></li>
    <li><a id="toc-Range-Addresses-1" href="#Range-Addresses">4.4 Range Addresses</a></li>
    <li><a id="toc-Zero-Address-1" href="#Zero-Address">4.5 Zero Address</a></li>
  </ul></li>
  <li><a id="toc-Regular-Expressions_003a-selecting-text" href="#sed-regular-expressions">5 Regular Expressions: selecting text</a>
  <ul class="no-bullet">
    <li><a id="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 id="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 id="toc-Overview-of-basic-regular-expression-syntax" href="#BRE-syntax">5.3 Overview of basic regular expression syntax</a></li>
    <li><a id="toc-Overview-of-extended-regular-expression-syntax" href="#ERE-syntax">5.4 Overview of extended regular expression syntax</a></li>
    <li><a id="toc-Character-Classes-and-Bracket-Expressions-1" href="#Character-Classes-and-Bracket-Expressions">5.5 Character Classes and Bracket Expressions</a></li>
    <li><a id="toc-regular-expression-extensions" href="#regexp-extensions">5.6 regular expression extensions</a></li>
    <li><a id="toc-Back_002dreferences-and-Subexpressions-1" href="#Back_002dreferences-and-Subexpressions">5.7 Back-references and Subexpressions</a></li>
    <li><a id="toc-Escape-Sequences-_002d-specifying-special-characters" href="#Escapes">5.8 Escape Sequences - specifying special characters</a>
    <ul class="no-bullet">
      <li><a id="toc-Escaping-Precedence" href="#Escaping-Precedence">5.8.1 Escaping Precedence</a></li>
    </ul></li>
    <li><a id="toc-Multibyte-characters-and-Locale-Considerations" href="#Locale-Considerations">5.9 Multibyte characters and Locale Considerations</a>
    <ul class="no-bullet">
      <li><a id="toc-Invalid-multibyte-characters" href="#Invalid-multibyte-characters">5.9.1 Invalid multibyte characters</a></li>
      <li><a id="toc-Upper_002fLower-case-conversion" href="#Upper_002fLower-case-conversion">5.9.2 Upper/Lower case conversion</a></li>
      <li><a id="toc-Multibyte-regexp-character-classes" href="#Multibyte-regexp-character-classes">5.9.3 Multibyte regexp character classes</a></li>
    </ul></li>
  </ul></li>
  <li><a id="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 id="toc-How-sed-Works" href="#Execution-Cycle">6.1 How <code>sed</code> Works</a></li>
    <li><a id="toc-Hold-and-Pattern-Buffers-1" href="#Hold-and-Pattern-Buffers">6.2 Hold and Pattern Buffers</a></li>
    <li><a id="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 id="toc-Branching-and-Flow-Control" href="#Branching-and-flow-control">6.4 Branching and Flow Control</a>
    <ul class="no-bullet">
      <li><a id="toc-Branching-and-Cycles" href="#Branching-and-Cycles">6.4.1 Branching and Cycles</a></li>
      <li><a id="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 id="toc-Some-Sample-Scripts" href="#Examples">7 Some Sample Scripts</a>
  <ul class="no-bullet">
    <li><a id="toc-Joining-lines-1" href="#Joining-lines">7.1 Joining lines</a></li>
    <li><a id="toc-Centering-Lines" href="#Centering-lines">7.2 Centering Lines</a></li>
    <li><a id="toc-Increment-a-Number" href="#Increment-a-number">7.3 Increment a Number</a></li>
    <li><a id="toc-Rename-Files-to-Lower-Case" href="#Rename-files-to-lower-case">7.4 Rename Files to Lower Case</a></li>
    <li><a id="toc-Print-bash-Environment" href="#Print-bash-environment">7.5 Print <code>bash</code> Environment</a></li>
    <li><a id="toc-Reverse-Characters-of-Lines" href="#Reverse-chars-of-lines">7.6 Reverse Characters of Lines</a></li>
    <li><a id="toc-Text-search-across-multiple-lines-1" href="#Text-search-across-multiple-lines">7.7 Text search across multiple lines</a></li>
    <li><a id="toc-Line-length-adjustment-1" href="#Line-length-adjustment">7.8 Line length adjustment</a></li>
    <li><a id="toc-Adding-a-header-to-multiple-files-1" href="#Adding-a-header-to-multiple-files">7.9 Adding a header to multiple files</a></li>
    <li><a id="toc-Reverse-Lines-of-Files" href="#tac">7.10 Reverse Lines of Files</a></li>
    <li><a id="toc-Numbering-Lines" href="#cat-_002dn">7.11 Numbering Lines</a></li>
    <li><a id="toc-Numbering-Non_002dblank-Lines" href="#cat-_002db">7.12 Numbering Non-blank Lines</a></li>
    <li><a id="toc-Counting-Characters" href="#wc-_002dc">7.13 Counting Characters</a></li>
    <li><a id="toc-Counting-Words" href="#wc-_002dw">7.14 Counting Words</a></li>
    <li><a id="toc-Counting-Lines" href="#wc-_002dl">7.15 Counting Lines</a></li>
    <li><a id="toc-Printing-the-First-Lines" href="#head">7.16 Printing the First Lines</a></li>
    <li><a id="toc-Printing-the-Last-Lines" href="#tail">7.17 Printing the Last Lines</a></li>
    <li><a id="toc-Make-Duplicate-Lines-Unique" href="#uniq">7.18 Make Duplicate Lines Unique</a></li>
    <li><a id="toc-Print-Duplicated-Lines-of-Input" href="#uniq-_002dd">7.19 Print Duplicated Lines of Input</a></li>
    <li><a id="toc-Remove-All-Duplicated-Lines" href="#uniq-_002du">7.20 Remove All Duplicated Lines</a></li>
    <li><a id="toc-Squeezing-Blank-Lines" href="#cat-_002ds">7.21 Squeezing Blank Lines</a></li>
  </ul></li>
  <li><a id="toc-GNU-sed_0027s-Limitations-and-Non_002dlimitations" href="#Limitations">8 GNU <code>sed</code>&rsquo;s Limitations and Non-limitations</a></li>
  <li><a id="toc-Other-Resources-for-Learning-About-sed" href="#Other-Resources">9 Other Resources for Learning About <code>sed</code></a></li>
  <li><a id="toc-Reporting-Bugs-1" href="#Reporting-Bugs">10 Reporting Bugs</a></li>
  <li><a id="toc-GNU-Free-Documentation-License-1" href="#GNU-Free-Documentation-License">Appendix A GNU Free Documentation License</a></li>
  <li><a id="toc-Concept-Index-1" href="#Concept-Index" rel="index">Concept Index</a></li>
  <li><a id="toc-Command-and-Option-Index-1" href="#Command-and-Option-Index" rel="index">Command and Option Index</a></li>
</ul>
</div>
</div>
<hr>
<div class="chapter" id="Introduction">
<div class="header">
<p>
Next: <a href="#Invoking-sed" accesskey="n" rel="next">Running sed</a>, Previous: <a href="#Top" accesskey="p" rel="prev">GNU <code>sed</code></a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Introduction-1"></span><h2 class="chapter">1 Introduction</h2>

<span id="index-Stream-editor"></span>
<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>&rsquo;s ability to filter text in a pipeline
which particularly distinguishes it from other types of
editors.
</p>

<hr>
</div>
<div class="chapter" id="Invoking-sed">
<div class="header">
<p>
Next: <a href="#sed-scripts" accesskey="n" rel="next"><code>sed</code> scripts</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Running-sed"></span><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>


<ul class="section-toc">
<li><a href="#Overview" accesskey="1">Overview</a></li>
<li><a href="#Command_002dLine-Options" accesskey="2">Command-Line Options</a></li>
<li><a href="#Exit-status" accesskey="3">Exit status</a></li>
</ul>
<hr>
<div class="section" id="Overview">
<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">Running sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Overview-1"></span><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 change every &lsquo;<samp>hello</samp>&rsquo; to &lsquo;<samp>world</samp>&rsquo;
in the file <samp>input.txt</samp>:
</p>
<div class="example">
<pre class="example">sed 's/hello/world/g' input.txt &gt; output.txt
</pre></div>

<p>Without the &lsquo;<samp>g</samp>&rsquo; (global) modifier, <code>sed</code> affects
only the first instance per line.
</p>
<span id="index-stdin"></span>
<span id="index-standard-input"></span>
<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/g' input.txt &gt; output.txt
sed 's/hello/world/g' &lt; input.txt &gt; output.txt
cat input.txt | sed 's/hello/world/g' - &gt; output.txt
</pre></div>

<span id="index-stdout"></span>
<span id="index-output"></span>
<span id="index-standard-output"></span>
<span id="index-_002di_002c-example"></span>
<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>

<span id="index-_002dn_002c-example"></span>
<span id="index-p_002c-example"></span>
<span id="index-suppressing-output"></span>
<span id="index-output_002c-suppressing"></span>
<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>



<span id="index-multiple-files"></span>
<span id="index-_002ds_002c-example"></span>
<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>


<span id="index-_002de_002c-example"></span>
<span id="index-_002d_002dexpression_002c-example"></span>
<span id="index-_002df_002c-example"></span>
<span id="index-_002d_002dfile_002c-example"></span>
<span id="index-script-parameter"></span>
<span id="index-parameters_002c-script"></span>
<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 &gt; output.txt

sed -e 's/hello/world/' input.txt &gt; output.txt
sed --expression='s/hello/world/' input.txt &gt; output.txt

echo 's/hello/world/' &gt; myscript.sed
sed -f myscript.sed input.txt &gt; output.txt
sed --file=myscript.sed input.txt &gt; output.txt
</pre></div>


<hr>
</div>
<div class="section" id="Command_002dLine-Options">
<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">Running sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Command_002dLine-Options-1"></span><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 id='index-_002d_002dversion'><span><code>--version</code><a href='#index-_002d_002dversion' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Version_002c-printing"></span>
<p>Print out the version of <code>sed</code> that is being run and a copyright notice,
then exit.
</p>
</dd>
<dt id='index-_002d_002dhelp'><span><code>--help</code><a href='#index-_002d_002dhelp' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Usage-summary_002c-printing"></span>
<p>Print a usage message briefly summarizing these command-line options
and the bug-reporting address,
then exit.
</p>
</dd>
<dt id='index-_002dn'><span><code>-n</code><a href='#index-_002dn' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--quiet</code></span></dt>
<dt><span><code>--silent</code></span></dt>
<dd><span id="index-_002d_002dquiet"></span>
<span id="index-_002d_002dsilent"></span>
<span id="index-Disabling-autoprint_002c-from-command-line"></span>
<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 id='index-_002d_002ddebug'><span><code>--debug</code><a href='#index-_002d_002ddebug' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-debug"></span>
<p>Print the input sed program in canonical form,
and annotate program execution.
</p><div class="example">
<pre class="example">$ echo 1 | sed '\%1%s21232'
3

$ echo 1 | sed --debug '\%1%s21232'
SED PROGRAM:
  /1/ s/1/3/
INPUT:   'STDIN' line 1
PATTERN: 1
COMMAND: /1/ s/1/3/
PATTERN: 3
END-OF-CYCLE:
3
</pre></div>


</dd>
<dt id='index-_002de'><span><code>-e <var>script</var></code><a href='#index-_002de' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--expression=<var>script</var></code></span></dt>
<dd><span id="index-_002d_002dexpression"></span>
<span id="index-Script_002c-from-command-line"></span>
<p>Add the commands in <var>script</var> to the set of commands to be
run while processing the input.
</p>
</dd>
<dt id='index-_002df'><span><code>-f <var>script-file</var></code><a href='#index-_002df' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--file=<var>script-file</var></code></span></dt>
<dd><span id="index-_002d_002dfile"></span>
<span id="index-Script_002c-from-a-file"></span>
<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 id='index-_002di'><span><code>-i[<var>SUFFIX</var>]</code><a href='#index-_002di' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--in-place[=<var>SUFFIX</var>]</code></span></dt>
<dd><span id="index-_002d_002din_002dplace"></span>
<span id="index-In_002dplace-editing_002c-activating"></span>
<span id="index-GNU-extensions_002c-in_002dplace-editing"></span>
<p>This option specifies that files are to be edited in-place.
GNU <code>sed</code> does this by creating a temporary file and
sending output to this file rather than to the standard
output.<a id="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&rsquo;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 id="DOCF2" href="#FOOT2"><sup>2</sup></a>).
</p>
<span id="index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names"></span>
<p>This rule is followed: if the extension doesn&rsquo;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>
<p>Because <samp>-i</samp> takes an optional argument, it should
not be followed by other short options:
</p><dl compact="compact">
<dt><span><code>sed -Ei '...' FILE</code></span></dt>
<dd><p>Same as <samp>-E -i</samp> with no backup suffix - <samp>FILE</samp> will be
edited in-place without creating a backup.
</p>
</dd>
<dt><span><code>sed -iE '...' FILE</code></span></dt>
<dd><p>This is equivalent to <samp>--in-place=E</samp>, creating <samp>FILEE</samp> as backup
of <samp>FILE</samp>
</p></dd>
</dl>

<p>Be cautious of using <samp>-n</samp> with <samp>-i</samp>: the former disables
automatic printing of lines and the latter changes the file in-place
without a backup. Used carelessly (and without an explicit <code>p</code> command),
the output file will be empty:
</p><div class="example">
<pre class="example"># WRONG USAGE: 'FILE' will be truncated.
sed -ni 's/foo/bar/' FILE
</pre></div>

</dd>
<dt id='index-_002dl'><span><code>-l <var>N</var></code><a href='#index-_002dl' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--line-length=<var>N</var></code></span></dt>
<dd><span id="index-_002d_002dline_002dlength"></span>
<span id="index-Line-length_002c-setting"></span>
<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 id='index-_002d_002dposix'><span><code>--posix</code><a href='#index-_002d_002dposix' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-disabling"></span>
<p>GNU <code>sed</code> includes several extensions to POSIX
sed.  In order to simplify writing portable scripts, this
option disables all the extensions that this manual documents,
including additional commands.
<span id="index-POSIXLY_005fCORRECT-behavior_002c-enabling"></span>
Most of the extensions accept <code>sed</code> programs that
are outside the syntax mandated by POSIX, 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 id='index-_002db'><span><code>-b</code><a href='#index-_002db' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--binary</code></span></dt>
<dd><span id="index-_002d_002dbinary"></span>
<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&mdash;as is the case for MS-DOS, Windows,
Cygwin&mdash;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 id='index-_002d_002dfollow_002dsymlinks'><span><code>--follow-symlinks</code><a href='#index-_002d_002dfollow_002dsymlinks' class='copiable-anchor'> &para;</a></span></dt>
<dd><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 id='index-_002dE'><span><code>-E</code><a href='#index-_002dE' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>-r</code></span></dt>
<dt><span><code>--regexp-extended</code></span></dt>
<dd><span id="index-_002dr"></span>
<span id="index-_002d_002dregexp_002dextended"></span>
<span id="index-Extended-regular-expressions_002c-choosing"></span>
<span id="index-GNU-extensions_002c-extended-regular-expressions"></span>
<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 GNU 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 id='index-_002ds'><span><code>-s</code><a href='#index-_002ds' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--separate</code></span></dt>
<dd><span id="index-_002d_002dseparate"></span>
<span id="index-Working-on-separate-files"></span>
<p>By default, <code>sed</code> will consider the files specified on the
command line as a single continuous long stream.  This GNU <code>sed</code>
extension allows the user to consider them as separate files:
range addresses (such as &lsquo;<samp>/abc/,/def/</samp>&rsquo;) 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 id='index-_002d_002dsandbox'><span><code>--sandbox</code><a href='#index-_002d_002dsandbox' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Sandbox-mode"></span>
<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 id='index-_002du'><span><code>-u</code><a href='#index-_002du' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--unbuffered</code></span></dt>
<dd><span id="index-_002d_002dunbuffered"></span>
<span id="index-Unbuffered-I_002fO_002c-choosing"></span>
<p>Buffer both input and output as minimally as practical.
(This is particularly useful if the input is coming from
the likes of &lsquo;<samp>tail -f</samp>&rsquo;, and you wish to see the transformed
output as soon as possible.)
</p>
</dd>
<dt id='index-_002dz'><span><code>-z</code><a href='#index-_002dz' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>--null-data</code></span></dt>
<dt><span><code>--zero-terminated</code></span></dt>
<dd><span id="index-_002d_002dnull_002ddata"></span>
<span id="index-_002d_002dzero_002dterminated"></span>
<p>Treat the input as a set of lines, each terminated by a zero byte
(the ASCII &lsquo;<samp>NUL</samp>&rsquo; character) instead of a newline.  This option can
be used with commands like &lsquo;<samp>sort -z</samp>&rsquo; and &lsquo;<samp>find -print0</samp>&rsquo;
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>
<span id="index-Files-to-be-processed-as-input"></span>
<p>If any command-line parameters remain after processing the above,
these parameters are interpreted as the names of input files to
be processed.
<span id="index-Standard-input_002c-processing-as-input"></span>
A file name of &lsquo;<samp>-</samp>&rsquo; refers to the standard input stream.
The standard input will be processed if no file names are specified.
</p>
<hr>
</div>
<div class="section" id="Exit-status">
<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">Running sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Exit-status-1"></span><h3 class="section">2.3 Exit status</h3>
<span id="index-exit-status"></span>
<p>An exit status of zero indicates success, and a nonzero value
indicates failure. GNU <code>sed</code> returns the following exit status
error values:
</p>
<dl compact="compact">
<dt><span>0</span></dt>
<dd><p>Successful completion.
</p>
</dd>
<dt><span>1</span></dt>
<dd><p>Invalid command, invalid syntax, invalid regular expression or a
GNU <code>sed</code> extension command used with <samp>--posix</samp>.
</p>
</dd>
<dt><span>2</span></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><span>4</span></dt>
<dd><p>An I/O error, or a serious processing error during runtime,
GNU <code>sed</code> aborted immediately.
</p></dd>
</dl>

<span id="index-Q_002c-example"></span>
<span id="index-exit-status_002c-example"></span>
<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 GNU <code>sed</code> extension):
</p>
<div class="example">
<pre class="example">$ echo | sed 'Q42' ; echo $?
42
</pre></div>


<hr>
</div>
</div>
<div class="chapter" id="sed-scripts">
<div class="header">
<p>
Next: <a href="#sed-addresses" accesskey="n" rel="next">Addresses: selecting lines</a>, Previous: <a href="#Invoking-sed" accesskey="p" rel="prev">Running sed</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="sed-scripts-1"></span><h2 class="chapter">3 <code>sed</code> scripts</h2>



<ul class="section-toc">
<li><a href="#sed-script-overview" accesskey="1"><code>sed</code> script overview</a></li>
<li><a href="#sed-commands-list" accesskey="2"><code>sed</code> commands summary</a></li>
<li><a href="#The-_0022s_0022-Command" accesskey="3">The <code>s</code> Command</a></li>
<li><a href="#Common-Commands" accesskey="4">Often-Used Commands</a></li>
<li><a href="#Other-Commands" accesskey="5">Less Frequently-Used Commands</a></li>
<li><a href="#Programming-Commands" accesskey="6">Commands for <code>sed</code> gurus</a></li>
<li><a href="#Extended-Commands" accesskey="7">Commands Specific to GNU <code>sed</code></a></li>
<li><a href="#Multiple-commands-syntax" accesskey="8">Multiple commands syntax</a></li>
</ul>
<hr>
<div class="section" id="sed-script-overview">
<div class="header">
<p>
Next: <a href="#sed-commands-list" accesskey="n" rel="next"><code>sed</code> commands summary</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="sed-script-overview-1"></span><h3 class="section">3.1 <code>sed</code> script overview</h3>

<span id="index-sed-script-structure"></span>
<span id="index-Script-structure"></span>

<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 &ldquo;the&rdquo; <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>

<span id="index-sed-commands-syntax"></span>
<span id="index-syntax_002c-sed-commands"></span>
<span id="index-addresses_002c-syntax"></span>
<span id="index-syntax_002c-addresses"></span>
<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">Addresses: selecting lines</a>).
Additional <code>[options]</code> are used for some <code>sed</code> commands.
</p>
<span id="index-d_002c-example"></span>
<span id="index-address-range_002c-example"></span>
<span id="index-example_002c-address-range"></span>
<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 &gt; output.txt
</pre></div>

<span id="index-q_002c-example"></span>
<span id="index-regular-expression_002c-example"></span>
<span id="index-example_002c-regular-expression"></span>
<p>The following example prints all input until a line
starting with the string &lsquo;<samp>foo</samp>&rsquo; 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 &gt; output.txt
</pre></div>


<span id="index-multiple-sed-commands"></span>
<span id="index-sed-commands_002c-multiple"></span>
<span id="index-newline_002c-command-separator"></span>
<span id="index-semicolons_002c-command-separator"></span>
<span id="index-_003b_002c-command-separator"></span>
<span id="index-_002de_002c-example-1"></span>
<span id="index-_002df_002c-example-1"></span>
<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 &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed '/^foo/d ; s/hello/world/g' input.txt &gt; output.txt

sed -e '/^foo/d' -e 's/hello/world/g' input.txt &gt; output.txt

echo '/^foo/d' &gt; script.sed
echo 's/hello/world/g' &gt;&gt; script.sed
sed -f script.sed input.txt &gt; output.txt

echo 's/hello/world/g' &gt; script2.sed
sed -e '/^foo/d' -f script2.sed input.txt &gt; output.txt
</pre></div>


<span id="index-a_002c-and-semicolons"></span>
<span id="index-c_002c-and-semicolons"></span>
<span id="index-i_002c-and-semicolons"></span>
<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>
</div>
<div class="section" id="sed-commands-list">
<div class="header">
<p>
Next: <a href="#The-_0022s_0022-Command" accesskey="n" rel="next">The <code>s</code> Command</a>, Previous: <a href="#sed-script-overview" accesskey="p" rel="prev"><code>sed</code> script overview</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="sed-commands-summary"></span><h3 class="section">3.2 <code>sed</code> commands summary</h3>

<p>The following commands are supported in GNU <code>sed</code>.
Some are standard POSIX commands, while other are GNU extensions.
Details and examples for each command are in the following sections.
(Mnemonics) are shown in parentheses.
</p>
<dl compact="compact">
<dt><span><code>a\</code></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><p>Append <var>text</var> after a line.
</p>
</dd>
<dt><span><code>a <var>text</var></code></span></dt>
<dd><p>Append <var>text</var> after a line (alternative syntax).
</p>
</dd>
<dt><span><code>b <var>label</var></code></span></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><span><code>c\</code></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><p>Replace (change) lines with <var>text</var>.
</p>
</dd>
<dt><span><code>c <var>text</var></code></span></dt>
<dd><p>Replace (change) lines with <var>text</var> (alternative syntax).
</p>
</dd>
<dt><span><code>d</code></span></dt>
<dd><p>Delete the pattern space;
immediately start next cycle.
</p>
</dd>
<dt><span><code>D</code></span></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><span><code>e</code></span></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><span><code>e <var>command</var></code></span></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><span><code>F</code></span></dt>
<dd><p>(filename) Print the file name of the current input file (with a trailing
newline).
</p>
</dd>
<dt><span><code>g</code></span></dt>
<dd><p>Replace the contents of the pattern space with the contents of the hold space.
</p>
</dd>
<dt><span><code>G</code></span></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><span><code>h</code></span></dt>
<dd><p>(hold) Replace the contents of the hold space with the contents of the
pattern space.
</p>
</dd>
<dt><span><code>H</code></span></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><span><code>i\</code></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><p>insert <var>text</var> before a line.
</p>
</dd>
<dt><span><code>i <var>text</var></code></span></dt>
<dd><p>insert <var>text</var> before a line (alternative syntax).
</p>
</dd>
<dt><span><code>l</code></span></dt>
<dd><p>Print the pattern space in an unambiguous form.
</p>
</dd>
<dt><span><code>n</code></span></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><span><code>N</code></span></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><span><code>p</code></span></dt>
<dd><p>Print the pattern space.
</p>
</dd>
<dt><span><code>P</code></span></dt>
<dd><p>Print the pattern space, up to the first &lt;newline&gt;.
</p>
</dd>
<dt><span><code>q<var>[exit-code]</var></code></span></dt>
<dd><p>(quit) Exit <code>sed</code> without processing any more commands or input.
</p>
</dd>
<dt><span><code>Q<var>[exit-code]</var></code></span></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><span><code>r filename</code></span></dt>
<dd><p>Reads file <var>filename</var>.
</p>
</dd>
<dt><span><code>R filename</code></span></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><span><code>s<var>/regexp/replacement/[flags]</var></code></span></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><span><code>t <var>label</var></code></span></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><span><code>T <var>label</var></code></span></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><span><code>v <var>[version]</var></code></span></dt>
<dd><p>(version) This command does nothing, but makes <code>sed</code> fail if
GNU <code>sed</code> extensions are not supported, or if the requested version
is not available.
</p>
</dd>
<dt><span><code>w filename</code></span></dt>
<dd><p>Write the pattern space to <var>filename</var>.
</p>
</dd>
<dt><span><code>W filename</code></span></dt>
<dd><p>Write to the given filename the portion of the pattern space up to
the first newline
</p>
</dd>
<dt><span><code>x</code></span></dt>
<dd><p>Exchange the contents of the hold and pattern spaces.
</p>

</dd>
<dt><span><code>y/src/dst/</code></span></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><span><code>z</code></span></dt>
<dd><p>(zap) This command empties the content of pattern space.
</p>
</dd>
<dt><span><code>#</code></span></dt>
<dd><p>A comment, until  the next newline.
</p>

</dd>
<dt><span><code>{ <var>cmd ; cmd ...</var> }</code></span></dt>
<dd><p>Group several commands together.
</p>
</dd>
<dt><span><code>=</code></span></dt>
<dd><p>Print the current input line number (with a trailing newline).
</p>
</dd>
<dt><span><code>: <var>label</var></code></span></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>
</div>
<div class="section" id="The-_0022s_0022-Command">
<div class="header">
<p>
Next: <a href="#Common-Commands" accesskey="n" rel="next">Often-Used Commands</a>, Previous: <a href="#sed-commands-list" accesskey="p" rel="prev"><code>sed</code> commands summary</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="The-s-Command"></span><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
&lsquo;<samp>s/<var>regexp</var>/<var>replacement</var>/<var>flags</var></samp>&rsquo;.
</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>
<span id="index-Backreferences_002c-in-regular-expressions"></span>
<span id="index-Parenthesized-substrings"></span>
<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>&amp;</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>


<span id="index-GNU-extensions_002c-case-modifiers-in-s-commands"></span>
<p>Finally, as a GNU <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><span><code>\L</code></span></dt>
<dd><p>Turn the replacement
to lowercase until a <code>\U</code> or <code>\E</code> is found,
</p>
</dd>
<dt><span><code>\l</code></span></dt>
<dd><p>Turn the
next character to lowercase,
</p>
</dd>
<dt><span><code>\U</code></span></dt>
<dd><p>Turn the replacement to uppercase
until a <code>\L</code> or <code>\E</code> is found,
</p>
</dd>
<dt><span><code>\u</code></span></dt>
<dd><p>Turn the next character
to uppercase,
</p>
</dd>
<dt><span><code>\E</code></span></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 &lsquo;<samp>a-b-</samp>&rsquo; in pattern space:
</p><div class="example">
<pre class="example">s/\(b\?\)-/x\u\1/g
</pre></div>

<p>the output is &lsquo;<samp>axxB</samp>&rsquo;.  When replacing the first &lsquo;<samp>-</samp>&rsquo;,
the &lsquo;<samp>\u</samp>&rsquo; sequence only affects the empty replacement of
&lsquo;<samp>\1</samp>&rsquo;.  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.
With &lsquo;<samp>a-b-</samp>&rsquo; in pattern space, the following command:
</p><div class="example">
<pre class="example">s/\(b\?\)-/\u\1x/g
</pre></div>

<p>will replace &lsquo;<samp>-</samp>&rsquo; with &lsquo;<samp>X</samp>&rsquo; (uppercase) and &lsquo;<samp>b-</samp>&rsquo; with
&lsquo;<samp>Bx</samp>&rsquo;.  If this behavior is undesirable, you can prevent it by
adding a &lsquo;<samp>\E</samp>&rsquo; sequence&mdash;after &lsquo;<samp>\1</samp>&rsquo; in this case.
</p>
<p>To include a literal <code>\</code>, <code>&amp;</code>, or newline in the final
replacement, be sure to precede the desired <code>\</code>, <code>&amp;</code>,
or newline in the <var>replacement</var> with a <code>\</code>.
</p>
<span id="index-s-command_002c-option-flags"></span>
<span id="index-Substitution-of-text_002c-options"></span>
<p>The <code>s</code> command can be followed by zero or more of the
following <var>flags</var>:
</p>
<dl compact="compact">
<dt id='index-Global-substitution'><span><code>g</code><a href='#index-Global-substitution' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Replacing-all-text-matching-regexp-in-a-line"></span>
<p>Apply the replacement to <em>all</em> matches to the <var>regexp</var>,
not just the first.
</p>
</dd>
<dt id='index-Replacing-only-nth-match-of-regexp-in-a-line'><span><code><var>number</var></code><a href='#index-Replacing-only-nth-match-of-regexp-in-a-line' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>Only replace the <var>number</var>th match of the <var>regexp</var>.
</p>
<span id="index-GNU-extensions_002c-g-and-number-modifier"></span>
<p>interaction in <code>s</code> command
<span id="index-Mixing-g-and-number-modifiers-in-the-s-command"></span>
Note: the <small>POSIX</small> 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 <code>sed</code> implementations.
For GNU <code>sed</code>, 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.
</p>
</dd>
<dt id='index-Text_002c-printing-after-substitution'><span><code>p</code><a href='#index-Text_002c-printing-after-substitution' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>If the substitution was made, then print the new pattern space.
</p>
<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 GNU <code>sed</code> 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.
</p>
</dd>
<dt id='index-Text_002c-writing-to-a-file-after-substitution'><span><code>w <var>filename</var></code><a href='#index-Text_002c-writing-to-a-file-after-substitution' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-_002fdev_002fstdout-file-1"></span>
<span id="index-GNU-extensions_002c-_002fdev_002fstderr-file"></span>
<p>If the substitution was made, then write out the result to the named file.
As a GNU <code>sed</code> extension, two special values of <var>filename</var> are
supported: <samp>/dev/stderr</samp>, which writes the result to the standard
error, and <samp>/dev/stdout</samp>, which writes to the standard
output.<a id="DOCF3" href="#FOOT3"><sup>3</sup></a>
</p>
</dd>
<dt id='index-Evaluate-Bourne_002dshell-commands_002c-after-substitution'><span><code>e</code><a href='#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Subprocesses"></span>
<span id="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands"></span>
<span id="index-GNU-extensions_002c-subprocesses"></span>
<p>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 <small>NUL</small> character.  This is a GNU <code>sed</code> extension.
</p>
</dd>
<dt id='index-GNU-extensions_002c-I-modifier'><span><code>I</code><a href='#index-GNU-extensions_002c-I-modifier' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>i</code></span></dt>
<dd><span id="index-Case_002dinsensitive-matching"></span>
<p>The <code>I</code> modifier to regular-expression matching is a GNU
extension which makes <code>sed</code> match <var>regexp</var> in a
case-insensitive manner.
</p>
</dd>
<dt id='index-GNU-extensions_002c-M-modifier'><span><code>M</code><a href='#index-GNU-extensions_002c-M-modifier' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>m</code></span></dt>
<dd><p>The <code>M</code> modifier to regular-expression matching is a GNU <code>sed</code>
extension which directs GNU <code>sed</code> to match the regular expression
in <cite>multi-line</cite> mode.  The modifier 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.
In addition,
the period character does not match a new-line character in
multi-line mode.
</p>

</dd>
</dl>

<hr>
</div>
<div class="section" id="Common-Commands">
<div class="header">
<p>
Next: <a href="#Other-Commands" accesskey="n" rel="next">Less Frequently-Used Commands</a>, Previous: <a href="#The-_0022s_0022-Command" accesskey="p" rel="prev">The <code>s</code> Command</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Often_002dUsed-Commands"></span><h3 class="section">3.4 Often-Used Commands</h3>

<p>If you use <code>sed</code> at all, you will quite likely want to know
these commands.
</p>
<dl compact="compact">
<dt><span><code>#</code></span></dt>
<dd><p>[No addresses allowed.]
</p>
<span id="index-_0023-_0028comments_0029"></span>
<span id="index-Comments_002c-in-scripts"></span>
<p>The <code>#</code> character begins a comment;
the comment continues until the next newline.
</p>
<span id="index-Portability_002c-comments"></span>
<p>If you are concerned about portability, be aware that
some implementations of <code>sed</code> (which are not <small>POSIX</small>
conforming) may only support a single one-line comment,
and then only when the very first character of the script is a <code>#</code>.
</p>
<span id="index-_002dn_002c-forcing-from-within-a-script"></span>
<span id="index-Caveat-_002d_002d_002d-_0023n-on-first-line"></span>
<p>Warning: if the first two characters of the <code>sed</code> script
are <code>#n</code>, then the <samp>-n</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 &lsquo;<samp>n</samp>&rsquo;
and you do not want this behavior,
then be sure to either use a capital &lsquo;<samp>N</samp>&rsquo;,
or place at least one space before the &lsquo;<samp>n</samp>&rsquo;.
</p>
</dd>
<dt id='index-q-_0028quit_0029-command'><span><code>q [<var>exit-code</var>]</code><a href='#index-q-_0028quit_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-returning-an-exit-code"></span>
<span id="index-Quitting"></span>
<p>Exit <code>sed</code> without processing any more commands or input.
</p>
<p>Example: stop after printing the second line:
</p><div class="example">
<pre class="example">$ seq 3 | sed 2q
1
2
</pre></div>

<p>This command accepts only one address.
Note that the current pattern space is printed if auto-print is
not disabled with the <samp>-n</samp> options.  The ability to return
an exit code from the <code>sed</code> script is a GNU <code>sed</code> extension.
</p>
<p>See also the GNU <code>sed</code> extension <code>Q</code> command which quits silently
without printing the current pattern space.
</p>
</dd>
<dt id='index-d-_0028delete_0029-command'><span><code>d</code><a href='#index-d-_0028delete_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Text_002c-deleting"></span>
<p>Delete the pattern space;
immediately start next cycle.
</p>
<p>Example: delete the second input line:
</p><div class="example">
<pre class="example">$ seq 3 | sed 2d
1
3
</pre></div>

</dd>
<dt id='index-p-_0028print_0029-command'><span><code>p</code><a href='#index-p-_0028print_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Text_002c-printing"></span>
<p>Print out the pattern space (to the standard output).
This command is usually only used in conjunction with the <samp>-n</samp>
command-line option.
</p>
<p>Example: print only the second input line:
</p><div class="example">
<pre class="example">$ seq 3 | sed -n 2p
2
</pre></div>

</dd>
<dt id='index-n-_0028next_002dline_0029-command'><span><code>n</code><a href='#index-n-_0028next_002dline_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Next-input-line_002c-replace-pattern-space-with"></span>
<span id="index-Read-next-input-line"></span>
<p>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>
<p>This command is useful to skip lines (e.g. process every Nth line).
</p>
<p>Example: perform substitution on every 3rd line (i.e. two <code>n</code> commands
skip two lines):
</p><div class="example">
<pre class="example">$ seq 6 | sed 'n;n;s/./x/'
1
2
x
4
5
x
</pre></div>

<p>GNU <code>sed</code> provides an extension address syntax of <var>first</var>~<var>step</var>
to achieve the same result:
</p>
<div class="example">
<pre class="example">$ seq 6 | sed '0~3s/./x/'
1
2
x
4
5
x
</pre></div>



</dd>
<dt id='index-_007b_007d-command-grouping'><span><code>{ <var>commands</var> }</code><a href='#index-_007b_007d-command-grouping' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Grouping-commands"></span>
<span id="index-Command-groups"></span>
<p>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.
</p>
<p>Example: perform substitution then print the second input line:
</p><div class="example">
<pre class="example">$ seq 3 | sed -n '2{s/2/X/ ; p}'
X
</pre></div>

</dd>
</dl>


<hr>
</div>
<div class="section" id="Other-Commands">
<div class="header">
<p>
Next: <a href="#Programming-Commands" accesskey="n" rel="next">Commands for <code>sed</code> gurus</a>, Previous: <a href="#Common-Commands" accesskey="p" rel="prev">Often-Used Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Less-Frequently_002dUsed-Commands"></span><h3 class="section">3.5 Less Frequently-Used Commands</h3>

<p>Though perhaps less frequently used than those in the previous
section, some very small yet useful <code>sed</code> scripts can be built with
these commands.
</p>
<dl compact="compact">
<dt id='index-y-_0028transliterate_0029-command'><span><code>y/<var>source-chars</var>/<var>dest-chars</var>/</code><a href='#index-y-_0028transliterate_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Transliteration"></span>
<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>
<p>Example: transliterate &lsquo;<samp>a-j</samp>&rsquo; into &lsquo;<samp>0-9</samp>&rsquo;:
</p><div class="example">
<pre class="example">$ echo hello world | sed 'y/abcdefghij/0123456789/'
74llo worl3
</pre></div>

<p>(The <code>/</code> characters may be uniformly replaced by
any other single character within any given <code>y</code> command.)
</p>
<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).
</p>
<p>See the <code>tr</code> command from GNU coreutils for similar functionality.
</p>
</dd>
<dt><span><code>a <var>text</var></code></span></dt>
<dd><p>Appending <var>text</var> after a line. This is a GNU extension
to the standard <code>a</code> command - see below for details.
</p>
<p>Example: Add &lsquo;<samp>hello</samp>&rsquo; after the second line:
</p><div class="example">
<pre class="example">$ seq 3 | sed '2a hello'
1
2
hello
3
</pre></div>

<p>Leading whitespace after the <code>a</code> command is ignored.
The text to add is read until the end of the line.
</p>

</dd>
<dt id='index-a-_0028append-text-lines_0029-command'><span><code>a\</code><a href='#index-a-_0028append-text-lines_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><span id="index-Appending-text-after-a-line"></span>
<span id="index-Text_002c-appending"></span>
<p>Appending <var>text</var> after a line.
</p>
<p>Example: Add &lsquo;<samp>hello</samp>&rsquo; after the second line
(-| indicates printed output lines):
</p><div class="example">
<pre class="example">$ seq 3 | sed '2a\
hello'
-|1
-|2
-|hello
-|3
</pre></div>

<p>The <code>a</code> command queues 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>
<span id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands"></span>
<p>As a GNU extension, this command accepts two addresses.
</p>
<p>Escape sequences in <var>text</var> are processed, so you should
use <code>\\</code> in <var>text</var> to print a single backslash.
</p>
<p>The commands resume after the last line without a backslash (<code>\</code>) -
&lsquo;<samp>world</samp>&rsquo; in the following example:
</p><div class="example">
<pre class="example">$ seq 3 | sed '2a\
hello\
world
3s/./X/'
-|1
-|2
-|hello
-|world
-|X
</pre></div>

<p>As a GNU extension, the <code>a</code> command and <var>text</var> can be
separated into two <code>-e</code> parameters, enabling easier scripting:
</p><div class="example">
<pre class="example">$ seq 3 | sed -e '2a\' -e hello
1
2
hello
3

$ sed -e '2a\' -e &quot;$VAR&quot;
</pre></div>

</dd>
<dt><span><code>i <var>text</var></code></span></dt>
<dd><p>insert <var>text</var> before a line. This is a GNU extension
to the standard <code>i</code> command - see below for details.
</p>
<p>Example: Insert &lsquo;<samp>hello</samp>&rsquo; before the second line:
</p><div class="example">
<pre class="example">$ seq 3 | sed '2i hello'
1
hello
2
3
</pre></div>

<p>Leading whitespace after the <code>i</code> command is ignored.
The text to add is read until the end of the line.
</p>
<span id="insert-command"></span></dd>
<dt id='index-i-_0028insert-text-lines_0029-command'><span><code>i\</code><a href='#index-i-_0028insert-text-lines_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><span id="index-Inserting-text-before-a-line"></span>
<span id="index-Text_002c-insertion"></span>
<p>Immediately output the lines of text which follow this command.
</p>
<p>Example: Insert &lsquo;<samp>hello</samp>&rsquo; before the second line
(-| indicates printed output lines):
</p><div class="example">
<pre class="example">$ seq 3 | sed '2i\
hello'
-|1
-|hello
-|2
-|3
</pre></div>

<span id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-1"></span>
<p>As a GNU extension, this command accepts two addresses.
</p>
<p>Escape sequences in <var>text</var> are processed, so you should
use <code>\\</code> in <var>text</var> to print a single backslash.
</p>
<p>The commands resume after the last line without a backslash (<code>\</code>) -
&lsquo;<samp>world</samp>&rsquo; in the following example:
</p><div class="example">
<pre class="example">$ seq 3 | sed '2i\
hello\
world
s/./X/'
-|X
-|hello
-|world
-|X
-|X
</pre></div>

<p>As a GNU extension, the <code>i</code> command and <var>text</var> can be
separated into two <code>-e</code> parameters, enabling easier scripting:
</p><div class="example">
<pre class="example">$ seq 3 | sed -e '2i\' -e hello
1
hello
2
3

$ sed -e '2i\' -e &quot;$VAR&quot;
</pre></div>

</dd>
<dt><span><code>c <var>text</var></code></span></dt>
<dd><p>Replaces the line(s) with <var>text</var>. This is a GNU extension
to the standard <code>c</code> command - see below for details.
</p>
<p>Example: Replace the 2nd to 9th lines with the word &lsquo;<samp>hello</samp>&rsquo;:
</p><div class="example">
<pre class="example">$ seq 10 | sed '2,9c hello'
1
hello
10
</pre></div>

<p>Leading whitespace after the <code>c</code> command is ignored.
The text to add is read until the end of the line.
</p>
</dd>
<dt id='index-c-_0028change-to-text-lines_0029-command'><span><code>c\</code><a href='#index-c-_0028change-to-text-lines_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code><var>text</var></code></span></dt>
<dd><span id="index-Replacing-selected-lines-with-other-text"></span>
<p>Delete the lines matching the address or address-range,
and output the lines of text which follow this command.
</p>
<p>Example: Replace 2nd to 4th lines with the words &lsquo;<samp>hello</samp>&rsquo; and
&lsquo;<samp>world</samp>&rsquo; (-| indicates printed output lines):
</p><div class="example">
<pre class="example">$ seq 5 | sed '2,4c\
hello\
world'
-|1
-|hello
-|world
-|5
</pre></div>

<p>If no addresses are given, each line is replaced.
</p>
<p>A new cycle is started after this command is done,
since the pattern space will have been deleted.
In the following example, the <code>c</code> starts a
new cycle and the substitution command is not performed
on the replaced text:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed '2c\
hello
s/./X/'
-|X
-|hello
-|X
</pre></div>

<p>As a GNU extension, the <code>c</code> command and <var>text</var> can be
separated into two <code>-e</code> parameters, enabling easier scripting:
</p><div class="example">
<pre class="example">$ seq 3 | sed -e '2c\' -e hello
1
hello
3

$ sed -e '2c\' -e &quot;$VAR&quot;
</pre></div>


</dd>
<dt id='index-_003d-_0028print-line-number_0029-command'><span><code>=</code><a href='#index-_003d-_0028print-line-number_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Printing-line-number"></span>
<span id="index-Line-number_002c-printing"></span>
<p>Print out the current input line number (with a trailing newline).
</p>
<div class="example">
<pre class="example">$ printf '%s\n' aaa bbb ccc | sed =
1
aaa
2
bbb
3
ccc
</pre></div>

<span id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-2"></span>
<p>As a GNU extension, this command accepts two addresses.
</p>



</dd>
<dt id='index-l-_0028list-unambiguously_0029-command'><span><code>l <var>n</var></code><a href='#index-l-_0028list-unambiguously_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-List-pattern-space"></span>
<span id="index-Printing-text-unambiguously"></span>
<span id="index-Line-length_002c-setting-1"></span>
<span id="index-GNU-extensions_002c-setting-line-length"></span>
<p>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>
<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 GNU <code>sed</code> extension.
</p>
</dd>
<dt><span><code>r <var>filename</var></code></span></dt>
<dd>
<span id="index-r-_0028read-file_0029-command"></span>
<span id="index-Read-text-from-a-file"></span>
<p>Reads file <var>filename</var>. Example:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed '2r/etc/hostname'
1
2
fencepost.gnu.org
3
</pre></div>

<span id="index-GNU-extensions_002c-_002fdev_002fstdin-file"></span>
<p>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>
<p>As a GNU <code>sed</code> extension, the special value <samp>/dev/stdin</samp>
is supported for the file name, which reads the contents of the
standard input.
</p>
<span id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-3"></span>
<p>As a GNU extension, this command accepts two addresses. The
file will then be reread and inserted on each of the addressed lines.
</p>
<p>As a GNU <code>sed</code> extension, the <code>r</code> command accepts a zero address,
inserting a file <em>before</em> the first line of the input
see <a href="#Adding-a-header-to-multiple-files">Adding a header to multiple files</a>.
</p>
</dd>
<dt id='index-w-_0028write-file_0029-command'><span><code>w <var>filename</var></code><a href='#index-w-_0028write-file_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Write-to-a-file"></span>
<span id="index-GNU-extensions_002c-_002fdev_002fstdout-file-2"></span>
<span id="index-GNU-extensions_002c-_002fdev_002fstderr-file-1"></span>
<p>Write the pattern space to <var>filename</var>.
As a GNU <code>sed</code> extension, two special values of <var>filename</var> are
supported: <samp>/dev/stderr</samp>, which writes the result to the standard
error, and <samp>/dev/stdout</samp>, which writes to the standard
output.<a id="DOCF4" href="#FOOT4"><sup>4</sup></a>
</p>
<p>The file will be created (or truncated) before the first input line is
read; all <code>w</code> commands (including instances of the <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.
</p>
</dd>
<dt id='index-D-_0028delete-first-line_0029-command'><span><code>D</code><a href='#index-D-_0028delete-first-line_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Delete-first-line-from-pattern-space"></span>
<p>If pattern space contains no newline, start a normal new cycle as if
the <code>d</code> command was issued.  Otherwise, 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>
</dd>
<dt id='index-N-_0028append-Next-line_0029-command'><span><code>N</code><a href='#index-N-_0028append-Next-line_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Next-input-line_002c-append-to-pattern-space"></span>
<span id="index-Append-next-input-line-to-pattern-space"></span>
<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>
<p>When <samp>-z</samp> is used, a zero byte (the ascii &lsquo;<samp>NUL</samp>&rsquo; character) is
added between the lines (instead of a new line).
</p>
<p>By default <code>sed</code> does not terminate if there is no &rsquo;next&rsquo; input line.
This is a GNU extension which can be disabled with <samp>--posix</samp>.
See <a href="#N_005fcommand_005flast_005fline">N command on the last line</a>.
</p>

</dd>
<dt id='index-P-_0028print-first-line_0029-command'><span><code>P</code><a href='#index-P-_0028print-first-line_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Print-first-line-from-pattern-space"></span>
<p>Print out the portion of the pattern space up to the first newline.
</p>
</dd>
<dt id='index-h-_0028hold_0029-command'><span><code>h</code><a href='#index-h-_0028hold_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Copy-pattern-space-into-hold-space"></span>
<span id="index-Replace-hold-space-with-copy-of-pattern-space"></span>
<span id="index-Hold-space_002c-copying-pattern-space-into"></span>
<p>Replace the contents of the hold space with the contents of the pattern space.
</p>
</dd>
<dt id='index-H-_0028append-Hold_0029-command'><span><code>H</code><a href='#index-H-_0028append-Hold_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Append-pattern-space-to-hold-space"></span>
<span id="index-Hold-space_002c-appending-from-pattern-space"></span>
<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 id='index-g-_0028get_0029-command'><span><code>g</code><a href='#index-g-_0028get_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Copy-hold-space-into-pattern-space"></span>
<span id="index-Replace-pattern-space-with-copy-of-hold-space"></span>
<span id="index-Hold-space_002c-copy-into-pattern-space"></span>
<p>Replace the contents of the pattern space with the contents of the hold space.
</p>
</dd>
<dt id='index-G-_0028appending-Get_0029-command'><span><code>G</code><a href='#index-G-_0028appending-Get_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Append-hold-space-to-pattern-space"></span>
<span id="index-Hold-space_002c-appending-to-pattern-space"></span>
<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 id='index-x-_0028eXchange_0029-command'><span><code>x</code><a href='#index-x-_0028eXchange_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Exchange-hold-space-with-pattern-space"></span>
<span id="index-Hold-space_002c-exchange-with-pattern-space"></span>
<p>Exchange the contents of the hold and pattern spaces.
</p>
</dd>
</dl>


<hr>
</div>
<div class="section" id="Programming-Commands">
<div class="header">
<p>
Next: <a href="#Extended-Commands" accesskey="n" rel="next">Commands Specific to GNU <code>sed</code></a>, Previous: <a href="#Other-Commands" accesskey="p" rel="prev">Less Frequently-Used Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Commands-for-sed-gurus"></span><h3 class="section">3.6 Commands for <code>sed</code> gurus</h3>

<p>In most cases, use of these commands indicates that you are
probably better off programming in something like <code>awk</code>
or Perl.  But occasionally one is committed to sticking
with <code>sed</code>, and these commands can enable one to write
quite convoluted scripts.
</p>
<span id="index-Flow-of-control-in-scripts"></span>
<dl compact="compact">
<dt><span><code>: <var>label</var></code></span></dt>
<dd><p>[No addresses allowed.]
</p>
<span id="index-_003a-_0028label_0029-command"></span>
<span id="index-Labels_002c-in-scripts"></span>
<p>Specify the location of <var>label</var> for branch commands.
In all other respects, a no-op.
</p>
</dd>
<dt id='index-b-_0028branch_0029-command'><span><code>b <var>label</var></code><a href='#index-b-_0028branch_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Branch-to-a-label_002c-unconditionally"></span>
<span id="index-Goto_002c-in-scripts"></span>
<p>Unconditionally branch to <var>label</var>.
The <var>label</var> may be omitted, in which case the next cycle is started.
</p>
</dd>
<dt id='index-t-_0028test-and-branch-if-successful_0029-command'><span><code>t <var>label</var></code><a href='#index-t-_0028test-and-branch-if-successful_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Branch-to-a-label_002c-if-s_002f_002f_002f-succeeded"></span>
<span id="index-Conditional-branch"></span>
<p>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>
</dl>

<hr>
</div>
<div class="section" id="Extended-Commands">
<div class="header">
<p>
Next: <a href="#Multiple-commands-syntax" accesskey="n" rel="next">Multiple commands syntax</a>, Previous: <a href="#Programming-Commands" accesskey="p" rel="prev">Commands for <code>sed</code> gurus</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Commands-Specific-to-GNU-sed"></span><h3 class="section">3.7 Commands Specific to GNU <code>sed</code></h3>

<p>These commands are specific to GNU <code>sed</code>, 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 GNU <code>sed</code> extensions or to do tasks that are required
quite often, yet are unsupported by standard <code>sed</code>s.
</p>
<dl compact="compact">
<dt id='index-e-_0028evaluate_0029-command'><span><code>e [<var>command</var>]</code><a href='#index-e-_0028evaluate_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Evaluate-Bourne_002dshell-commands"></span>
<span id="index-Subprocesses-1"></span>
<span id="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands-1"></span>
<span id="index-GNU-extensions_002c-subprocesses-1"></span>
<p>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>
<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.
The command can run across multiple lines, all but the last ending with
a back-slash.
</p>
<p>In both cases, the results are undefined if the command to be
executed contains a <small>NUL</small> character.
</p>
<p>Note that, unlike the <code>r</code> command, the output of the command will
be printed immediately; the <code>r</code> command instead delays the output
to the end of the current cycle.
</p>
</dd>
<dt id='index-F-_0028File-name_0029-command'><span><code>F</code><a href='#index-F-_0028File-name_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Printing-file-name"></span>
<span id="index-File-name_002c-printing"></span>
<p>Print out the file name of the current input file (with a trailing
newline).
</p>
</dd>
<dt><span><code>Q [<var>exit-code</var>]</code></span></dt>
<dd><p>This command accepts only one address.
</p>
<span id="index-Q-_0028silent-Quit_0029-command"></span>
<span id="index-GNU-extensions_002c-quitting-silently"></span>
<span id="index-GNU-extensions_002c-returning-an-exit-code-1"></span>
<span id="index-Quitting-1"></span>
<p>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>
<p>This command can be useful because the only alternative ways
to accomplish this apparently trivial function are to use
the <samp>-n</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:
</p>
<div class="example">
<pre class="example">:eat
$d       <i><span class="roman">Quit silently on the last line</span></i>
N        <i><span class="roman">Read another line, silently</span></i>
g        <i><span class="roman">Overwrite pattern space each time to save memory</span></i>
b eat
</pre></div>

</dd>
<dt id='index-R-_0028read-line_0029-command'><span><code>R <var>filename</var></code><a href='#index-R-_0028read-line_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Read-text-from-a-file-1"></span>
<span id="index-GNU-extensions_002c-reading-a-file-a-line-at-a-time"></span>
<span id="index-GNU-extensions_002c-R-command"></span>
<span id="index-GNU-extensions_002c-_002fdev_002fstdin-file-1"></span>
<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.
Note that if <var>filename</var> cannot be read, or if its end is
reached, no line is appended, without any error indication.
</p>
<p>As with the <code>r</code> command, the special value <samp>/dev/stdin</samp>
is supported for the file name, which reads a line from the
standard input.
</p>
</dd>
<dt id='index-T-_0028test-and-branch-if-failed_0029-command'><span><code>T <var>label</var></code><a href='#index-T-_0028test-and-branch-if-failed_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-branch-if-s_002f_002f_002f-failed"></span>
<span id="index-Branch-to-a-label_002c-if-s_002f_002f_002f-failed"></span>
<span id="index-Conditional-branch-1"></span>
<p>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 id='index-v-_0028version_0029-command'><span><code>v <var>version</var></code><a href='#index-v-_0028version_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-checking-for-their-presence"></span>
<span id="index-Requiring-GNU-sed"></span>
<p>This command does nothing, but makes <code>sed</code> fail if
GNU <code>sed</code> extensions are not supported, simply because other
versions of <code>sed</code> do not implement it.  In addition, you
can specify the version of <code>sed</code> 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>
<p>This command enables all GNU extensions even if
<code>POSIXLY_CORRECT</code> is set in the environment.
</p>
</dd>
<dt id='index-W-_0028write-first-line_0029-command'><span><code>W <var>filename</var></code><a href='#index-W-_0028write-first-line_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Write-first-line-to-a-file"></span>
<span id="index-GNU-extensions_002c-writing-first-line-to-a-file"></span>
<p>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
file handling holds here too.
</p>
</dd>
<dt id='index-z-_0028Zap_0029-command'><span><code>z</code><a href='#index-z-_0028Zap_0029-command' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-emptying-pattern-space"></span>
<span id="index-Emptying-pattern-space"></span>
<p>This command empties the content of pattern space.  It is
usually the same as &lsquo;<samp>s/.*//</samp>&rsquo;, but is more efficient
and works in the presence of invalid multibyte sequences
in the input stream.  <small>POSIX</small> mandates that such sequences
are <em>not</em> matched by &lsquo;<samp>.</samp>&rsquo;, so that there is no portable
way to clear <code>sed</code>&rsquo;s buffers in the middle of the
script in most multibyte locales (including UTF-8 locales).
</p></dd>
</dl>


<hr>
</div>
<div class="section" id="Multiple-commands-syntax">
<div class="header">
<p>
Previous: <a href="#Extended-Commands" accesskey="p" rel="prev">Commands Specific to GNU <code>sed</code></a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code>sed</code> scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Multiple-commands-syntax-1"></span><h3 class="section">3.8 Multiple commands syntax</h3>


<p>There are several methods to specify multiple commands in a <code>sed</code>
program.
</p>
<p>Using newlines is most natural when running a sed script from a file
(using the <samp>-f</samp> option).
</p>
<p>On the command line, all <code>sed</code> commands may be separated by newlines.
Alternatively, you may specify each command as an argument to an <samp>-e</samp>
option:
</p>
<div class="example">
<pre class="example">$ seq 6 | sed '1d
3d
5d'
2
4
6

$ seq 6 | sed -e 1d -e 3d -e 5d
2
4
6
</pre></div>

<p>A semicolon (&lsquo;<samp>;</samp>&rsquo;) may be used to separate most simple commands:
</p>
<div class="example">
<pre class="example">$ seq 6 | sed '1d;3d;5d'
2
4
6
</pre></div>

<p>The <code>{</code>,<code>}</code>,<code>b</code>,<code>t</code>,<code>T</code>,<code>:</code> commands can
be separated with a semicolon (this is a non-portable GNU <code>sed</code> extension).
</p>
<div class="example">
<pre class="example">$ seq 4 | sed '{1d;3d}'
2
4

$ seq 6 | sed '{1d;3d};5d'
2
4
6
</pre></div>

<p>Labels used in <code>b</code>,<code>t</code>,<code>T</code>,<code>:</code> commands are read
until a semicolon.  Leading and trailing whitespace is ignored.  In
the examples below the label is &lsquo;<samp>x</samp>&rsquo;.  The first example works
with GNU <code>sed</code>.  The second is a portable equivalent.  For more
information about branching and labels see <a href="#Branching-and-flow-control">Branching and Flow Control</a>.
</p>
<div class="example">
<pre class="example">$ seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d'
1
=2

$ seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d'
1
=2
</pre></div>



<ul class="section-toc">
<li><a href="#Commands-Requiring-a-newline" accesskey="1">Commands Requiring a newline</a></li>
</ul>
<div class="subsection" id="Commands-Requiring-a-newline">
<h4 class="subsection">3.8.1 Commands Requiring a newline</h4>

<p>The following commands cannot be separated by a semicolon and
require a newline:
</p>
<dl compact="compact">
<dt><span><code>a</code>,<code>c</code>,<code>i</code> (append/change/insert)</span></dt>
<dd>
<p>All characters following <code>a</code>,<code>c</code>,<code>i</code> commands are taken
as the text to append/change/insert.  Using a semicolon leads to
undesirable results:
</p>
<div class="example">
<pre class="example">$ seq 2 | sed '1aHello ; 2d'
1
Hello ; 2d
2
</pre></div>

<p>Separate the commands using <samp>-e</samp> or a newline:
</p>
<div class="example">
<pre class="example">$ seq 2 | sed -e 1aHello -e 2d
1
Hello

$ seq 2 | sed '1aHello
2d'
1
Hello
</pre></div>

<p>Note that specifying the text to add (&lsquo;<samp>Hello</samp>&rsquo;) immediately
after <code>a</code>,<code>c</code>,<code>i</code> is itself a GNU <code>sed</code> extension.
A portable, POSIX-compliant alternative is:
</p>
<div class="example">
<pre class="example">$ seq 2 | sed '1a\
Hello
2d'
1
Hello
</pre></div>

</dd>
<dt><span><code>#</code> (comment)</span></dt>
<dd>
<p>All characters following &lsquo;<samp>#</samp>&rsquo; until the next newline are ignored.
</p>
<div class="example">
<pre class="example">$ seq 3 | sed '# this is a comment ; 2d'
1
2
3


$ seq 3 | sed '# this is a comment
2d'
1
3
</pre></div>

</dd>
<dt><span><code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> (reading and writing files)</span></dt>
<dd>
<p>The <code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> commands parse the filename
until end of the line.  If whitespace, comments or semicolons are found,
they will be included in the filename, leading to unexpected results:
</p>
<div class="example">
<pre class="example">$ seq 2 | sed '1w hello.txt ; 2d'
1
2

$ ls -log
total 4
-rw-rw-r-- 1 2 Jan 23 23:03 hello.txt ; 2d

$ cat 'hello.txt ; 2d'
1
</pre></div>

<p>Note that <code>sed</code> silently ignores read/write errors in
<code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> commands (such as missing files).
In the following example, <code>sed</code> tries to read a file named
&lsquo;<samp><samp>hello.txt ; N</samp></samp>&rsquo;. The file is missing, and the error is silently
ignored:
</p>
<div class="example">
<pre class="example">$ echo x | sed '1rhello.txt ; N'
x
</pre></div>

</dd>
<dt><span><code>e</code> (command execution)</span></dt>
<dd>
<p>Any characters following the <code>e</code> command until the end of the line
will be sent to the shell.  If whitespace, comments or semicolons are found,
they will be included in the shell command, leading to unexpected results:
</p>
<div class="example">
<pre class="example">$ echo a | sed '1e touch foo#bar'
a

$ ls -1
foo#bar

$ echo a | sed '1e touch foo ; s/a/b/'
sh: 1: s/a/b/: not found
a
</pre></div>


</dd>
<dt><span><code>s///[we]</code> (substitute with <code>e</code> or <code>w</code> flags)</span></dt>
<dd>
<p>In a substitution command, the <code>w</code> flag writes the substitution
result to a file, and the <code>e</code> flag executes the substitution result
as a shell command.  As with the <code>r/R/w/W/e</code> commands, these
must be terminated with a newline.  If whitespace, comments or semicolons
are found, they will be included in the shell command or filename, leading to
unexpected results:
</p>
<div class="example">
<pre class="example">$ echo a | sed 's/a/b/w1.txt#foo'
b

$ ls -1
1.txt#foo
</pre></div>

</dd>
</dl>


<hr>
</div>
</div>
</div>
<div class="chapter" id="sed-addresses">
<div class="header">
<p>
Next: <a href="#sed-regular-expressions" accesskey="n" rel="next">Regular Expressions: selecting text</a>, Previous: <a href="#sed-scripts" accesskey="p" rel="prev"><code>sed</code> scripts</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Addresses_003a-selecting-lines"></span><h2 class="chapter">4 Addresses: selecting lines</h2>


<ul class="section-toc">
<li><a href="#Addresses-overview" accesskey="1">Addresses overview</a></li>
<li><a href="#Numeric-Addresses" accesskey="2">Selecting lines by numbers</a></li>
<li><a href="#Regexp-Addresses" accesskey="3">selecting lines by text matching</a></li>
<li><a href="#Range-Addresses" accesskey="4">Range Addresses</a></li>
<li><a href="#Zero-Address" accesskey="5">Zero Address</a></li>
</ul>
<hr>
<div class="section" id="Addresses-overview">
<div class="header">
<p>
Next: <a href="#Numeric-Addresses" accesskey="n" rel="next">Selecting lines by numbers</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">Addresses: selecting lines</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Addresses-overview-1"></span><h3 class="section">4.1 Addresses overview</h3>

<span id="index-addresses_002c-numeric"></span>
<span id="index-numeric-addresses"></span>
<p>Addresses determine on which line(s) the <code>sed</code> command will be
executed. The following command replaces any first occurrence of &lsquo;<samp>hello</samp>&rsquo;
with &lsquo;<samp>world</samp>&rsquo; only on line 144:
</p>
<div class="example">
<pre class="example">sed '144s/hello/world/' input.txt &gt; output.txt
</pre></div>



<p>If no address is specified, the command is performed on all lines.
The following command replaces &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;,
targeting every line of the input file.
However, note that it modifies only the first instance of &lsquo;<samp>hello</samp>&rsquo;
on each line.
Use the &lsquo;<samp>g</samp>&rsquo; modifier to affect every instance on each affected line.
</p>
<div class="example">
<pre class="example">sed 's/hello/world/' input.txt &gt; output.txt
</pre></div>



<span id="index-addresses_002c-regular-expression"></span>
<span id="index-regular-expression-addresses"></span>
<p>Addresses can contain regular expressions to match lines based
on content instead of line numbers. The following command replaces
&lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo; only on lines
containing the string &lsquo;<samp>apple</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed '/apple/s/hello/world/' input.txt &gt; output.txt
</pre></div>



<span id="index-addresses_002c-range"></span>
<span id="index-range-addresses"></span>
<p>An address range is specified with two addresses separated by a comma
(<code>,</code>). Addresses can be numeric, regular expressions, or a mix of
both.
The following command replaces &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;
only on lines 4 to 17 (inclusive):
</p>
<div class="example">
<pre class="example">sed '4,17s/hello/world/' input.txt &gt; output.txt
</pre></div>



<span id="index-Excluding-lines"></span>
<span id="index-Selecting-non_002dmatching-lines"></span>
<span id="index-addresses_002c-negating"></span>
<span id="index-addresses_002c-excluding"></span>
<p>Appending the <code>!</code> character to the end of an address
specification (before the command letter) negates the sense of the
match.  That is, if the <code>!</code> character follows an address or an
address range, then only lines which do <em>not</em> match the addresses
will be selected. The following command replaces &lsquo;<samp>hello</samp>&rsquo;
with &lsquo;<samp>world</samp>&rsquo; only on lines <em>not</em> containing the string
&lsquo;<samp>apple</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed '/apple/!s/hello/world/' input.txt &gt; output.txt
</pre></div>

<p>The following command replaces &lsquo;<samp>hello</samp>&rsquo; with
&lsquo;<samp>world</samp>&rsquo; only on lines 1 to 3 and from line 18 to the last line of the
input file (i.e. excluding lines 4 to 17):
</p>
<div class="example">
<pre class="example">sed '4,17!s/hello/world/' input.txt &gt; output.txt
</pre></div>





<hr>
</div>
<div class="section" id="Numeric-Addresses">
<div class="header">
<p>
Next: <a href="#Regexp-Addresses" accesskey="n" rel="next">selecting lines by text matching</a>, Previous: <a href="#Addresses-overview" accesskey="p" rel="prev">Addresses overview</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">Addresses: selecting lines</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Selecting-lines-by-numbers"></span><h3 class="section">4.2 Selecting lines by numbers</h3>
<span id="index-Addresses_002c-in-sed-scripts"></span>
<span id="index-Line-selection"></span>
<span id="index-Selecting-lines-to-process"></span>

<p>Addresses in a <code>sed</code> script can be in any of the following forms:
</p><dl compact="compact">
<dt id='index-Address_002c-numeric'><span><code><var>number</var></code><a href='#index-Address_002c-numeric' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Line_002c-selecting-by-number"></span>
<p>Specifying a line number will match only that line in the input.
(Note that <code>sed</code> counts lines continuously across all input files
unless <samp>-i</samp> or <samp>-s</samp> options are specified.)
</p>
</dd>
<dt id='index-Address_002c-last-line'><span><code>$</code><a href='#index-Address_002c-last-line' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Last-line_002c-selecting"></span>
<span id="index-Line_002c-selecting-last"></span>
<p>This address matches the last line of the last file of input, or
the last line of each file when the <samp>-i</samp> or <samp>-s</samp> options
are specified.
</p>

</dd>
<dt id='index-GNU-extensions_002c-n_007em-addresses'><span><code><var>first</var>~<var>step</var></code><a href='#index-GNU-extensions_002c-n_007em-addresses' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>This GNU 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
<var>first</var> + (<var>n</var> * <var>step</var>).
Thus, one would use <code>1~2</code> to select the odd-numbered lines and
<code>0~2</code> for even-numbered lines;
to pick every third line starting with the second, &lsquo;<samp>2~3</samp>&rsquo; would be used;
to pick every fifth line starting with the tenth, use &lsquo;<samp>10~5</samp>&rsquo;;
and &lsquo;<samp>50~0</samp>&rsquo; is just an obscure way of saying <code>50</code>.
</p>
<p>The following commands demonstrate the step address usage:
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '0~4p'
4
8

$ seq 10 | sed -n '1~3p'
1
4
7
10
</pre></div>


</dd>
</dl>



<hr>
</div>
<div class="section" id="Regexp-Addresses">
<div class="header">
<p>
Next: <a href="#Range-Addresses" accesskey="n" rel="next">Range Addresses</a>, Previous: <a href="#Numeric-Addresses" accesskey="p" rel="prev">Selecting lines by numbers</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">Addresses: selecting lines</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="selecting-lines-by-text-matching"></span><h3 class="section">4.3 selecting lines by text matching</h3>

<p>GNU <code>sed</code> supports the following regular expression addresses.
The default regular expression is
<a href="#BRE-syntax">Basic Regular Expression (BRE)</a>.
If <samp>-E</samp> or <samp>-r</samp> options are used, The regular expression should be
in <a href="#ERE-syntax">Extended Regular Expression (ERE)</a> syntax.
See <a href="#BRE-vs-ERE">Basic (BRE) and extended (ERE) regular expression</a>.
</p>
<dl compact="compact">
<dt id='index-Address_002c-as-a-regular-expression'><span><code>/<var>regexp</var>/</code><a href='#index-Address_002c-as-a-regular-expression' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Line_002c-selecting-by-regular-expression-match"></span>
<p>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>
<p>The following command prints lines in <samp>/etc/passwd</samp>
which end with &lsquo;<samp>bash</samp>&rsquo;<a id="DOCF5" href="#FOOT5"><sup>5</sup></a>:
</p>
<div class="example">
<pre class="example">sed -n '/bash$/p' /etc/passwd
</pre></div>

<span id="index-empty-regular-expression"></span>
<span id="index-GNU-extensions_002c-modifiers-and-the-empty-regular-expression"></span>
<p>The empty regular expression &lsquo;<samp>//</samp>&rsquo; 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 invalid to
specify them together with the empty regular expression.
</p>
</dd>
<dt><span><code>\%<var>regexp</var>%</code></span></dt>
<dd><p>(The <code>%</code> may be replaced by any other single character.)
</p>
<span id="index-Slash-character_002c-in-regular-expressions"></span>
<p>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>).
</p>
<p>The following commands are equivalent. They print lines
which start with &lsquo;<samp>/home/alice/documents/</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed -n '/^\/home\/alice\/documents\//p'
sed -n '\%^/home/alice/documents/%p'
sed -n '\;^/home/alice/documents/;p'
</pre></div>


</dd>
<dt id='index-GNU-extensions_002c-I-modifier-1'><span><code>/<var>regexp</var>/I</code><a href='#index-GNU-extensions_002c-I-modifier-1' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>\%<var>regexp</var>%I</code></span></dt>
<dd><span id="index-case-insensitive_002c-regular-expression"></span>
<p>The <code>I</code> modifier to regular-expression matching is a GNU
extension which causes the <var>regexp</var> to be matched in
a case-insensitive manner.
</p>
<p>In many other programming languages, a lower case <code>i</code> is used
for case-insensitive regular expression matching. However, in <code>sed</code>
the <code>i</code> is used for the insert command (see <a href="#insert-command">insert command</a>).
</p>
<p>Observe the difference between the following examples.
</p>
<p>In this example, <code>/b/I</code> is the address: regular expression with <code>I</code>
modifier. <code>d</code> is the delete command:
</p>
<div class="example">
<pre class="example">$ printf &quot;%s\n&quot; a b c | sed '/b/Id'
a
c
</pre></div>

<p>Here, <code>/b/</code> is the address: a regular expression.
<code>i</code> is the insert command.
<code>d</code> is the value to insert.
A line with &lsquo;<samp>d</samp>&rsquo; is then inserted above the matched line:
</p>
<div class="example">
<pre class="example">$ printf &quot;%s\n&quot; a b c | sed '/b/id'
a
d
b
c
</pre></div>

</dd>
<dt id='index-GNU-extensions_002c-M-modifier-1'><span><code>/<var>regexp</var>/M</code><a href='#index-GNU-extensions_002c-M-modifier-1' class='copiable-anchor'> &para;</a></span></dt>
<dt><span><code>\%<var>regexp</var>%M</code></span></dt>
<dd><p>The <code>M</code> modifier to regular-expression matching is a GNU <code>sed</code>
extension which directs GNU <code>sed</code> to match the regular expression
in <cite>multi-line</cite> mode.  The modifier 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.
In addition,
the period character does not match a new-line character in
multi-line mode.
</p></dd>
</dl>


<span id="index-regex-addresses-and-pattern-space"></span>
<span id="index-regex-addresses-and-input-lines"></span>
<p>Regex addresses operate on the content of the current
pattern space. If the pattern space is changed (for example with <code>s///</code>
command) the regular expression matching will operate on the changed text.
</p>
<p>In the following example, automatic printing is disabled with
<samp>-n</samp>.  The <code>s/2/X/</code> command changes lines containing
&lsquo;<samp>2</samp>&rsquo; to &lsquo;<samp>X</samp>&rsquo;. The command <code>/[0-9]/p</code> matches
lines with digits and prints them.
Because the second line is changed before the <code>/[0-9]/</code> regex,
it will not match and will not be printed:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed -n 's/2/X/ ; /[0-9]/p'
1
3
</pre></div>


<hr>
</div>
<div class="section" id="Range-Addresses">
<div class="header">
<p>
Next: <a href="#Zero-Address" accesskey="n" rel="next">Zero Address</a>, Previous: <a href="#Regexp-Addresses" accesskey="p" rel="prev">selecting lines by text matching</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">Addresses: selecting lines</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Range-Addresses-1"></span><h3 class="section">4.4 Range Addresses</h3>

<span id="index-Range-of-lines"></span>
<span id="index-Several-lines_002c-selecting"></span>
<p>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):
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '4,6p'
4
5
6
</pre></div>

<p>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: a range will always
span at least two lines (except of course if the input stream
ends).
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '4,/[0-9]/p'
4
5
</pre></div>

<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>
<div class="example">
<pre class="example">$ seq 10 | sed -n '4,1p'
4
</pre></div>

<span id="Zero-Address-Regex-Range"></span><span id="index-Special-addressing-forms"></span>
<span id="index-Range-with-start-address-of-zero"></span>
<span id="index-Zero_002c-as-range-start-address"></span>
<span id="index-addr1_002c_002bN"></span>
<span id="index-addr1_002c_007eN"></span>
<span id="index-GNU-extensions_002c-special-two_002daddress-forms"></span>
<span id="index-GNU-extensions_002c-0-address"></span>
<span id="index-GNU-extensions_002c-0_002caddr2-addressing"></span>
<span id="index-GNU-extensions_002c-addr1_002c_002bN-addressing"></span>
<span id="index-GNU-extensions_002c-addr1_002c_007eN-addressing"></span>
<p>GNU <code>sed</code> also supports some special two-address forms; all these
are GNU extensions:
</p><dl compact="compact">
<dt><span><code>0,/<var>regexp</var>/</code></span></dt>
<dd><p>A line number of <code>0</code> can be used in an address specification like
<code>0,/<var>regexp</var>/</code> so that <code>sed</code> will try to match
<var>regexp</var> in the first input line too.  In other words,
<code>0,/<var>regexp</var>/</code> is similar to <code>1,/<var>regexp</var>/</code>,
except that if <var>addr2</var> matches the very first line of input the
<code>0,/<var>regexp</var>/</code> form will consider it to end the range, whereas
the <code>1,/<var>regexp</var>/</code> form will match the beginning of its range and
hence make the range span up to the <em>second</em> occurrence of the
regular expression.
</p>
<p>The following examples demonstrate the difference between starting
with address 1 and 0:
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '1,/[0-9]/p'
1
2

$ seq 10 | sed -n '0,/[0-9]/p'
1
</pre></div>


</dd>
<dt><span><code><var>addr1</var>,+<var>N</var></code></span></dt>
<dd><p>Matches <var>addr1</var> and the <var>N</var> lines following <var>addr1</var>.
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '6,+2p'
6
7
8
</pre></div>

<p><var>addr1</var> can be a line number or a regular expression.
</p>
</dd>
<dt><span><code><var>addr1</var>,~<var>N</var></code></span></dt>
<dd><p>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>.
The following command prints starting at line 6, until the next line which
is a multiple of 4 (i.e. line 8):
</p>
<div class="example">
<pre class="example">$ seq 10 | sed -n '6,~4p'
6
7
8
</pre></div>

<p><var>addr1</var> can be a line number or a regular expression.
</p>
</dd>
</dl>



<hr>
</div>
<div class="section" id="Zero-Address">
<div class="header">
<p>
Previous: <a href="#Range-Addresses" accesskey="p" rel="prev">Range Addresses</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">Addresses: selecting lines</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Zero-Address-1"></span><h3 class="section">4.5 Zero Address</h3>
<span id="index-Zero-Address"></span>
<p>As a GNU <code>sed</code> extension, <code>0</code> address can be used in two cases:
</p><ol>
<li> In a regex range addresses as <code>0,/<var>regexp</var>/</code>
(see <a href="#Zero-Address-Regex-Range">Zero Address Regex Range</a>).
</li><li> With the <code>r</code> command, inserting a file before the first line
(see <a href="#Adding-a-header-to-multiple-files">Adding a header to multiple files</a>).
</li></ol>

<p>Note that these are the only places where the <code>0</code> address makes
sense; Commands which are given the <code>0</code> address in any
other way will give an error.
</p>


<hr>
</div>
</div>
<div class="chapter" id="sed-regular-expressions">
<div class="header">
<p>
Next: <a href="#advanced-sed" accesskey="n" rel="next">Advanced <code>sed</code>: cycles and buffers</a>, Previous: <a href="#sed-addresses" accesskey="p" rel="prev">Addresses: selecting lines</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Regular-Expressions_003a-selecting-text"></span><h2 class="chapter">5 Regular Expressions: selecting text</h2>


<ul class="section-toc">
<li><a href="#Regular-Expressions-Overview" accesskey="1">Overview of regular expression in <code>sed</code></a></li>
<li><a href="#BRE-vs-ERE" accesskey="2">Basic (BRE) and extended (ERE) regular expression</a></li>
<li><a href="#BRE-syntax" accesskey="3">Overview of basic regular expression syntax</a></li>
<li><a href="#ERE-syntax" accesskey="4">Overview of extended regular expression syntax</a></li>
<li><a href="#Character-Classes-and-Bracket-Expressions" accesskey="5">Character Classes and Bracket Expressions</a></li>
<li><a href="#regexp-extensions" accesskey="6">regular expression extensions</a></li>
<li><a href="#Back_002dreferences-and-Subexpressions" accesskey="7">Back-references and Subexpressions</a></li>
<li><a href="#Escapes" accesskey="8">Escape Sequences - specifying special characters</a></li>
<li><a href="#Locale-Considerations" accesskey="9">Multibyte characters and Locale Considerations</a></li>
</ul>
<hr>
<div class="section" id="Regular-Expressions-Overview">
<div class="header">
<p>
Next: <a href="#BRE-vs-ERE" accesskey="n" rel="next">Basic (BRE) and extended (ERE) regular expression</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Overview-of-regular-expression-in-sed"></span><h3 class="section">5.1 Overview of regular expression in <code>sed</code></h3>



<p>To know how to use <code>sed</code>, people should understand regular
expressions (<em>regexp</em> for short).  A regular expression
is a pattern that is matched against a
subject string from left to right.  Most characters are
<em>ordinary</em>: they stand for
themselves in a pattern, and match the corresponding characters.
Regular expressions in <code>sed</code> are specified between two
slashes.
</p>
<p>The following command prints lines containing the string &lsquo;<samp>hello</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed -n '/hello/p'
</pre></div>

<p>The above example is equivalent to this <code>grep</code> command:
</p>
<div class="example">
<pre class="example">grep 'hello'
</pre></div>

<p>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 <em>special characters</em>, which do not stand for
themselves but instead are interpreted in some special way.
</p>
<p>The character <code>^</code> (caret) in a regular expression matches the
beginning of the line. The character <code>.</code> (dot) matches any single
character. The following <code>sed</code> command matches and prints
lines which start with the letter &lsquo;<samp>b</samp>&rsquo;, followed by any single character,
followed by the letter &lsquo;<samp>d</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">$ printf &quot;%s\n&quot; abode bad bed bit bid byte body | sed -n '/^b.d/p'
bad
bed
bid
body
</pre></div>

<p>The following sections explain the meaning and usage of special
characters in regular expressions.
</p>
<hr>
</div>
<div class="section" id="BRE-vs-ERE">
<div class="header">
<p>
Next: <a href="#BRE-syntax" accesskey="n" rel="next">Overview of basic regular expression syntax</a>, Previous: <a href="#Regular-Expressions-Overview" accesskey="p" rel="prev">Overview of regular expression in <code>sed</code></a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression"></span><h3 class="section">5.2 Basic (BRE) and extended (ERE) regular expression</h3>

<p>Basic and extended regular expressions are two variations on the
syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the
default in <code>sed</code> (and similarly in <code>grep</code>).
Use the POSIX-specified <samp>-E</samp> option (<samp>-r</samp>,
<samp>--regexp-extended</samp>) to enable Extended Regular Expression (ERE) syntax.
</p>
<p>In GNU <code>sed</code>, the only difference between basic and extended regular
expressions is in the behavior of a few special characters: &lsquo;<samp>?</samp>&rsquo;,
&lsquo;<samp>+</samp>&rsquo;, parentheses, braces (&lsquo;<samp>{}</samp>&rsquo;), and &lsquo;<samp>|</samp>&rsquo;.
</p>
<p>With basic (BRE) syntax, these characters do not have special meaning
unless prefixed with a backslash (&lsquo;<samp>\</samp>&rsquo;); While with extended (ERE) syntax
it is reversed: these characters are special unless they are prefixed
with backslash (&lsquo;<samp>\</samp>&rsquo;).
</p>
<table>
<thead><tr><th width="28%">Desired pattern</th><th width="36%">Basic (BRE) Syntax</th><th width="35%">Extended (ERE) Syntax</th></tr></thead>
<tr><td width="28%">literal &lsquo;<samp>+</samp>&rsquo; (plus sign)</td><td width="36%"><div class="example">
<pre class="example">$ echo 'a+b=c' &gt; foo
$ sed -n '/a+b/p' foo
a+b=c
</pre></div></td><td width="35%"><div class="example">
<pre class="example">$ echo 'a+b=c' &gt; foo
$ sed -E -n '/a\+b/p' foo
a+b=c
</pre></div></td></tr>
<tr><td width="28%">One or more &lsquo;<samp>a</samp>&rsquo; characters followed by &lsquo;<samp>b</samp>&rsquo;
(plus sign as special meta-character)</td><td width="36%"><div class="example">
<pre class="example">$ echo aab &gt; foo
$ sed -n '/a\+b/p' foo
aab
</pre></div></td><td width="35%"><div class="example">
<pre class="example">$ echo aab &gt; foo
$ sed -E -n '/a+b/p' foo
aab
</pre></div></td></tr>
</table>




<hr>
</div>
<div class="section" id="BRE-syntax">
<div class="header">
<p>
Next: <a href="#ERE-syntax" accesskey="n" rel="next">Overview of extended regular expression syntax</a>, Previous: <a href="#BRE-vs-ERE" accesskey="p" rel="prev">Basic (BRE) and extended (ERE) regular expression</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Overview-of-basic-regular-expression-syntax"></span><h3 class="section">5.3 Overview of basic regular expression syntax</h3>

<p>Here is a brief description
of regular expression syntax as used in <code>sed</code>.
</p>
<dl compact="compact">
<dt><span><code><var>char</var></code></span></dt>
<dd><p>A single ordinary character matches itself.
</p>
</dd>
<dt id='index-GNU-extensions_002c-to-basic-regular-expressions'><span><code>*</code><a href='#index-GNU-extensions_002c-to-basic-regular-expressions' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>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 GNU extension, a
postfixed regular expression can also be followed by <code>*</code>; for
example, <code>a**</code> is equivalent to <code>a*</code>.  POSIX
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-GNU implementations do not support this and portable
scripts should instead use <code>\*</code> in these contexts.
</p></dd>
<dt><span><code>.</code></span></dt>
<dd><p>Matches any character, including newline.
</p>
</dd>
<dt><span><code>^</code></span></dt>
<dd><p>Matches the null string at beginning of the pattern space, i.e. what
appears after the circumflex must appear at the beginning of the
pattern space.
</p>
<p>In most scripts, pattern space is initialized to the content of each
line (see <a href="#Execution-Cycle">How <code>sed</code> works</a>).  So, it is a
useful simplification to think of <code>^#include</code> as matching only
lines where &lsquo;<samp>#include</samp>&rsquo; is the first thing on the line&mdash;if there is
any preceding space, for example, the match fails.  This simplification is
valid as long as the original content of pattern space is not modified,
for example with an <code>s</code> command.
</p>
<p><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 POSIX allows implementations that
treat <code>^</code> as an ordinary character in that context.
</p>
</dd>
<dt><span><code>$</code></span></dt>
<dd><p>It is the same as <code>^</code>, but refers to end of pattern space.
<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.
</p>

</dd>
<dt><span><code>[<var>list</var>]</code></span></dt>
<dt><span><code>[^<var>list</var>]</code></span></dt>
<dd><p>Matches any single character in <var>list</var>: for example,
<code>[aeiou]</code> matches all vowels.  A list may include
sequences like <code><var>char1</var>-<var>char2</var></code>, which
matches any character between (inclusive) <var>char1</var>
and <var>char2</var>.
See <a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a>.
</p>
</dd>
<dt id='index-GNU-extensions_002c-to-basic-regular-expressions-1'><span><code>\+</code><a href='#index-GNU-extensions_002c-to-basic-regular-expressions-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>As <code>*</code>, but matches one or more.  It is a GNU extension.
</p>
</dd>
<dt id='index-GNU-extensions_002c-to-basic-regular-expressions-2'><span><code>\?</code><a href='#index-GNU-extensions_002c-to-basic-regular-expressions-2' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>As <code>*</code>, but only matches zero or one.  It is a GNU extension.
</p>
</dd>
<dt><span><code>\{<var>i</var>\}</code></span></dt>
<dd><p>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).
</p>
</dd>
<dt><span><code>\{<var>i</var>,<var>j</var>\}</code></span></dt>
<dd><p>Matches between <var>i</var> and <var>j</var>, inclusive, sequences.
</p>
</dd>
<dt><span><code>\{<var>i</var>,\}</code></span></dt>
<dd><p>Matches more than or equal to <var>i</var> sequences.
</p>
</dd>
<dt><span><code>\(<var>regexp</var>\)</code></span></dt>
<dd><p>Groups the inner <var>regexp</var> as a whole, this is used to:
</p>
<ul>
<li> <span id="index-GNU-extensions_002c-to-basic-regular-expressions-3"></span>
Apply postfix operators, like <code>\(abcd\)*</code>:
this will search for zero or more whole sequences
of &lsquo;<samp>abcd</samp>&rsquo;, while <code>abcd*</code> would search
for &lsquo;<samp>abc</samp>&rsquo; followed by zero or more occurrences
of &lsquo;<samp>d</samp>&rsquo;.  Note that support for <code>\(abcd\)*</code> is
required by POSIX 1003.1-2001, but many non-GNU
implementations do not support it and hence it is not universally
portable.

</li><li> Use back references (see below).
</li></ul>


</dd>
<dt id='index-GNU-extensions_002c-to-basic-regular-expressions-4'><span><code><var>regexp1</var>\|<var>regexp2</var></code><a href='#index-GNU-extensions_002c-to-basic-regular-expressions-4' class='copiable-anchor'> &para;</a></span></dt>
<dd><p>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 GNU extension.
</p>
</dd>
<dt><span><code><var>regexp1</var><var>regexp2</var></code></span></dt>
<dd><p>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.
</p>
</dd>
<dt><span><code>\<var>digit</var></code></span></dt>
<dd><p>Matches the <var>digit</var>-th <code>\(&hellip;\)</code> parenthesized
subexpression in the regular expression.  This is called a <em>back
reference</em>.  Subexpressions are implicitly numbered by counting
occurrences of <code>\(</code> left-to-right.
</p>
</dd>
<dt><span><code>\n</code></span></dt>
<dd><p>Matches the newline character.
</p>
</dd>
<dt><span><code>\<var>char</var></code></span></dt>
<dd><p>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 &lsquo;<samp>t</samp>&rsquo; under most
implementations of <code>sed</code>, rather than a tab character.
</p>
</dd>
</dl>

<span id="index-Greedy-regular-expression-matching"></span>
<p>Note that the regular expression matcher is greedy, i.e., matches
are attempted from left to right and, if two or more matches are
possible starting at the same character, it selects the longest.
</p>
<p>Examples:
</p><dl compact="compact">
<dt><span>&lsquo;<samp>abcdef</samp>&rsquo;</span></dt>
<dd><p>Matches &lsquo;<samp>abcdef</samp>&rsquo;.
</p>
</dd>
<dt><span>&lsquo;<samp>a*b</samp>&rsquo;</span></dt>
<dd><p>Matches zero or more &lsquo;<samp>a</samp>&rsquo;s followed by a single
&lsquo;<samp>b</samp>&rsquo;.  For example, &lsquo;<samp>b</samp>&rsquo; or &lsquo;<samp>aaaaab</samp>&rsquo;.
</p>
</dd>
<dt><span>&lsquo;<samp>a\?b</samp>&rsquo;</span></dt>
<dd><p>Matches &lsquo;<samp>b</samp>&rsquo; or &lsquo;<samp>ab</samp>&rsquo;.
</p>
</dd>
<dt><span>&lsquo;<samp>a\+b\+</samp>&rsquo;</span></dt>
<dd><p>Matches one or more &lsquo;<samp>a</samp>&rsquo;s followed by one or more
&lsquo;<samp>b</samp>&rsquo;s: &lsquo;<samp>ab</samp>&rsquo; is the shortest possible match, but
other examples are &lsquo;<samp>aaaab</samp>&rsquo; or &lsquo;<samp>abbbbb</samp>&rsquo; or
&lsquo;<samp>aaaaaabbbbbbb</samp>&rsquo;.
</p>
</dd>
<dt><span>&lsquo;<samp>.*</samp>&rsquo;</span></dt>
<dt><span>&lsquo;<samp>.\+</samp>&rsquo;</span></dt>
<dd><p>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.
</p>
</dd>
<dt><span>&lsquo;<samp>^main.*(.*)</samp>&rsquo;</span></dt>
<dd><p>This matches a string starting with &lsquo;<samp>main</samp>&rsquo;,
followed by an opening and closing
parenthesis.  The &lsquo;<samp>n</samp>&rsquo;, &lsquo;<samp>(</samp>&rsquo; and &lsquo;<samp>)</samp>&rsquo; need not
be adjacent.
</p>
</dd>
<dt><span>&lsquo;<samp>^#</samp>&rsquo;</span></dt>
<dd><p>This matches a string beginning with &lsquo;<samp>#</samp>&rsquo;.
</p>
</dd>
<dt><span>&lsquo;<samp>\\$</samp>&rsquo;</span></dt>
<dd><p>This matches a string ending with a single backslash.  The
regexp contains two backslashes for escaping.
</p>
</dd>
<dt><span>&lsquo;<samp>\$</samp>&rsquo;</span></dt>
<dd><p>Instead, this matches a string consisting of a single dollar sign,
because it is escaped.
</p>
</dd>
<dt><span>&lsquo;<samp>[a-zA-Z0-9]</samp>&rsquo;</span></dt>
<dd><p>In the C locale, this matches any ASCII letters or digits.
</p>
</dd>
<dt><span>&lsquo;<samp>[^ <kbd><span class="key">TAB</span></kbd>]\+</samp>&rsquo;</span></dt>
<dd><p>(Here <kbd><span class="key">TAB</span></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.
</p>
</dd>
<dt><span>&lsquo;<samp>^\(.*\)\n\1$</samp>&rsquo;</span></dt>
<dd><p>This matches a string consisting of two equal substrings separated by
a newline.
</p>
</dd>
<dt><span>&lsquo;<samp>.\{9\}A$</samp>&rsquo;</span></dt>
<dd><p>This matches nine characters followed by an &lsquo;<samp>A</samp>&rsquo; at the end of a line.
</p>
</dd>
<dt><span>&lsquo;<samp>^.\{15\}A</samp>&rsquo;</span></dt>
<dd><p>This matches the start of a string that contains 16 characters,
the last of which is an &lsquo;<samp>A</samp>&rsquo;.
</p>
</dd>
</dl>


<hr>
</div>
<div class="section" id="ERE-syntax">
<div class="header">
<p>
Next: <a href="#Character-Classes-and-Bracket-Expressions" accesskey="n" rel="next">Character Classes and Bracket Expressions</a>, Previous: <a href="#BRE-syntax" accesskey="p" rel="prev">Overview of basic regular expression syntax</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Overview-of-extended-regular-expression-syntax"></span><h3 class="section">5.4 Overview of extended regular expression syntax</h3>
<span id="index-Extended-regular-expressions_002c-syntax"></span>

<p>The only difference between basic and extended regular expressions is in
the behavior of a few characters: &lsquo;<samp>?</samp>&rsquo;, &lsquo;<samp>+</samp>&rsquo;, parentheses,
braces (&lsquo;<samp>{}</samp>&rsquo;), and &lsquo;<samp>|</samp>&rsquo;.  While basic regular expressions
require these to be escaped if you want them to behave as special
characters, when using extended regular expressions you must escape
them if you want them <em>to match a literal character</em>.  &lsquo;<samp>|</samp>&rsquo;
is special here because &lsquo;<samp>\|</samp>&rsquo; is a GNU extension &ndash; standard
basic regular expressions do not provide its functionality.
</p>
<p>Examples:
</p><dl compact="compact">
<dt><span><code>abc?</code></span></dt>
<dd><p>becomes &lsquo;<samp>abc\?</samp>&rsquo; when using extended regular expressions.  It matches
the literal string &lsquo;<samp>abc?</samp>&rsquo;.
</p>
</dd>
<dt><span><code>c\+</code></span></dt>
<dd><p>becomes &lsquo;<samp>c+</samp>&rsquo; when using extended regular expressions.  It matches
one or more &lsquo;<samp>c</samp>&rsquo;s.
</p>
</dd>
<dt><span><code>a\{3,\}</code></span></dt>
<dd><p>becomes &lsquo;<samp>a{3,}</samp>&rsquo; when using extended regular expressions.  It matches
three or more &lsquo;<samp>a</samp>&rsquo;s.
</p>
</dd>
<dt><span><code>\(abc\)\{2,3\}</code></span></dt>
<dd><p>becomes &lsquo;<samp>(abc){2,3}</samp>&rsquo; when using extended regular expressions.  It
matches either &lsquo;<samp>abcabc</samp>&rsquo; or &lsquo;<samp>abcabcabc</samp>&rsquo;.
</p>
</dd>
<dt><span><code>\(abc*\)\1</code></span></dt>
<dd><p>becomes &lsquo;<samp>(abc*)\1</samp>&rsquo; when using extended regular expressions.
Backreferences must still be escaped when using extended regular
expressions.
</p>
</dd>
<dt><span><code>a\|b</code></span></dt>
<dd><p>becomes &lsquo;<samp>a|b</samp>&rsquo; when using extended regular expressions.  It matches
&lsquo;<samp>a</samp>&rsquo; or &lsquo;<samp>b</samp>&rsquo;.
</p></dd>
</dl>

<hr>
</div>
<div class="section" id="Character-Classes-and-Bracket-Expressions">
<div class="header">
<p>
Next: <a href="#regexp-extensions" accesskey="n" rel="next">regular expression extensions</a>, Previous: <a href="#ERE-syntax" accesskey="p" rel="prev">Overview of extended regular expression syntax</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Character-Classes-and-Bracket-Expressions-1"></span><h3 class="section">5.5 Character Classes and Bracket Expressions</h3>


<span id="index-bracket-expression"></span>
<span id="index-character-class"></span>
<p>A <em>bracket expression</em> is a list of characters enclosed by &lsquo;<samp>[</samp>&rsquo; and
&lsquo;<samp>]</samp>&rsquo;.
It matches any single character in that list;
if the first character of the list is the caret &lsquo;<samp>^</samp>&rsquo;,
then it matches any character <strong>not</strong> in the list.
For example, the following command replaces the strings
&lsquo;<samp>gray</samp>&rsquo; or &lsquo;<samp>grey</samp>&rsquo; with &lsquo;<samp>blue</samp>&rsquo;:
</p>
<div class="example">
<pre class="example">sed  's/gr[ae]y/blue/'
</pre></div>

<p>Bracket expressions can be used in both
<a href="#BRE-syntax">basic</a> and <a href="#ERE-syntax">extended</a>
regular expressions (that is, with or without the <samp>-E</samp>/<samp>-r</samp>
options).
</p>
<span id="index-range-expression"></span>
<p>Within a bracket expression, a <em>range expression</em> consists of two
characters separated by a hyphen.
It matches any single character that
sorts between the two characters, inclusive.
In the default C locale, the sorting sequence is the native character
order; for example, &lsquo;<samp>[a-d]</samp>&rsquo; is equivalent to &lsquo;<samp>[abcd]</samp>&rsquo;.
</p>

<p>Finally, certain named classes of characters are predefined within
bracket expressions, as follows.
</p>
<p>These named classes must be used <em>inside</em> brackets
themselves. Correct usage:
</p><div class="example">
<pre class="example">$ echo 1 | sed 's/[[:digit:]]/X/'
X
</pre></div>

<p>Incorrect usage is rejected by newer <code>sed</code> versions.
Older versions accepted it but treated it as a single bracket expression
(which is equivalent to &lsquo;<samp>[dgit:]</samp>&rsquo;,
that is, only the characters <var>d/g/i/t/:</var>):
</p><div class="example">
<pre class="example"># current GNU sed versions - incorrect usage rejected
$ echo 1 | sed 's/[:digit:]/X/'
sed: character class syntax is [[:space:]], not [:space:]

# older GNU sed versions
$ echo 1 | sed 's/[:digit:]/X/'
1
</pre></div>


<span id="index-classes-of-characters"></span>
<span id="index-character-classes"></span>
<span id="index-named-character-classes"></span>
<dl compact="compact">
<dt id='index-alnum-character-class'><span>&lsquo;<samp>[:alnum:]</samp>&rsquo;<a href='#index-alnum-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-alphanumeric-characters"></span>
<p>Alphanumeric characters:
&lsquo;<samp>[:alpha:]</samp>&rsquo; and &lsquo;<samp>[:digit:]</samp>&rsquo;; in the &lsquo;<samp>C</samp>&rsquo; locale and ASCII
character encoding, this is the same as &lsquo;<samp>[0-9A-Za-z]</samp>&rsquo;.
</p>
</dd>
<dt id='index-alpha-character-class'><span>&lsquo;<samp>[:alpha:]</samp>&rsquo;<a href='#index-alpha-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-alphabetic-characters"></span>
<p>Alphabetic characters:
&lsquo;<samp>[:lower:]</samp>&rsquo; and &lsquo;<samp>[:upper:]</samp>&rsquo;; in the &lsquo;<samp>C</samp>&rsquo; locale and ASCII
character encoding, this is the same as &lsquo;<samp>[A-Za-z]</samp>&rsquo;.
</p>
</dd>
<dt id='index-blank-character-class'><span>&lsquo;<samp>[:blank:]</samp>&rsquo;<a href='#index-blank-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-blank-characters"></span>
<p>Blank characters:
space and tab.
</p>
</dd>
<dt id='index-cntrl-character-class'><span>&lsquo;<samp>[:cntrl:]</samp>&rsquo;<a href='#index-cntrl-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-control-characters"></span>
<p>Control characters.
In ASCII, these characters have octal codes 000
through 037, and 177 (DEL).
In other character sets, these are
the equivalent characters, if any.
</p>
</dd>
<dt id='index-digit-character-class'><span>&lsquo;<samp>[:digit:]</samp>&rsquo;<a href='#index-digit-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-digit-characters"></span>
<span id="index-numeric-characters"></span>
<p>Digits: <code>0 1 2 3 4 5 6 7 8 9</code>.
</p>
</dd>
<dt id='index-graph-character-class'><span>&lsquo;<samp>[:graph:]</samp>&rsquo;<a href='#index-graph-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-graphic-characters"></span>
<p>Graphical characters:
&lsquo;<samp>[:alnum:]</samp>&rsquo; and &lsquo;<samp>[:punct:]</samp>&rsquo;.
</p>
</dd>
<dt id='index-lower-character-class'><span>&lsquo;<samp>[:lower:]</samp>&rsquo;<a href='#index-lower-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-lower_002dcase-letters"></span>
<p>Lower-case letters; in the &lsquo;<samp>C</samp>&rsquo; locale and ASCII character
encoding, this is
<code>a b c d e f g h i j k l m n o p q r s t u v w x y z</code>.
</p>
</dd>
<dt id='index-print-character-class'><span>&lsquo;<samp>[:print:]</samp>&rsquo;<a href='#index-print-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-printable-characters"></span>
<p>Printable characters:
&lsquo;<samp>[:alnum:]</samp>&rsquo;, &lsquo;<samp>[:punct:]</samp>&rsquo;, and space.
</p>
</dd>
<dt id='index-punct-character-class'><span>&lsquo;<samp>[:punct:]</samp>&rsquo;<a href='#index-punct-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-punctuation-characters"></span>
<p>Punctuation characters; in the &lsquo;<samp>C</samp>&rsquo; locale and ASCII character
encoding, this is
<code>! &quot; # $ % &amp; ' ( ) * + , - . / : ; &lt; = &gt; ? @ [ \ ] ^ _ ` { | } ~</code>.
</p>
</dd>
<dt id='index-space-character-class'><span>&lsquo;<samp>[:space:]</samp>&rsquo;<a href='#index-space-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-space-characters"></span>
<span id="index-whitespace-characters"></span>
<p>Space characters: in the &lsquo;<samp>C</samp>&rsquo; locale, this is
tab, newline, vertical tab, form feed, carriage return, and space.
</p>

</dd>
<dt id='index-upper-character-class'><span>&lsquo;<samp>[:upper:]</samp>&rsquo;<a href='#index-upper-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-upper_002dcase-letters"></span>
<p>Upper-case letters: in the &lsquo;<samp>C</samp>&rsquo; locale and ASCII character
encoding, this is
<code>A B C D E F G H I J K L M N O P Q R S T U V W X Y Z</code>.
</p>
</dd>
<dt id='index-xdigit-character-class'><span>&lsquo;<samp>[:xdigit:]</samp>&rsquo;<a href='#index-xdigit-character-class' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-xdigit-class"></span>
<span id="index-hexadecimal-digits"></span>
<p>Hexadecimal digits:
<code>0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f</code>.
</p>
</dd>
</dl>
<p>Note that the brackets in these class names are
part of the symbolic names, and must be included in addition to
the brackets delimiting the bracket expression.
</p>
<p>Most meta-characters lose their special meaning inside bracket expressions:
</p>
<dl compact="compact">
<dt><span>&lsquo;<samp>]</samp>&rsquo;</span></dt>
<dd><p>ends the bracket expression if it&rsquo;s not the first list item.
So, if you want to make the &lsquo;<samp>]</samp>&rsquo; character a list item,
you must put it first.
</p>
</dd>
<dt><span>&lsquo;<samp>-</samp>&rsquo;</span></dt>
<dd><p>represents the range if it&rsquo;s not first or last in a list or the ending point
of a range.
</p>
</dd>
<dt><span>&lsquo;<samp>^</samp>&rsquo;</span></dt>
<dd><p>represents the characters not in the list.
If you want to make the &lsquo;<samp>^</samp>&rsquo;
character a list item, place it anywhere but first.
</p></dd>
</dl>

<p>TODO: incorporate this paragraph (copied verbatim from BRE section).
</p>
<span id="index-POSIXLY_005fCORRECT-behavior_002c-bracket-expressions"></span>
<p>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 &lsquo;<samp>\</samp>&rsquo; or &lsquo;<samp>*</samp>&rsquo;, 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>.  Also, when not in
<code>POSIXLY_CORRECT</code> mode, special escapes like <code>\n</code> and
<code>\t</code> are recognized within <var>list</var>.  See <a href="#Escapes">Escape Sequences - specifying special characters</a>.
</p>


<dl compact="compact">
<dt><span>&lsquo;<samp>[.</samp>&rsquo;</span></dt>
<dd><p>represents the open collating symbol.
</p>
</dd>
<dt><span>&lsquo;<samp>.]</samp>&rsquo;</span></dt>
<dd><p>represents the close collating symbol.
</p>
</dd>
<dt><span>&lsquo;<samp>[=</samp>&rsquo;</span></dt>
<dd><p>represents the open equivalence class.
</p>
</dd>
<dt><span>&lsquo;<samp>=]</samp>&rsquo;</span></dt>
<dd><p>represents the close equivalence class.
</p>
</dd>
<dt><span>&lsquo;<samp>[:</samp>&rsquo;</span></dt>
<dd><p>represents the open character class symbol, and should be followed by a
valid character class name.
</p>
</dd>
<dt><span>&lsquo;<samp>:]</samp>&rsquo;</span></dt>
<dd><p>represents the close character class symbol.
</p></dd>
</dl>


<hr>
</div>
<div class="section" id="regexp-extensions">
<div class="header">
<p>
Next: <a href="#Back_002dreferences-and-Subexpressions" accesskey="n" rel="next">Back-references and Subexpressions</a>, Previous: <a href="#Character-Classes-and-Bracket-Expressions" accesskey="p" rel="prev">Character Classes and Bracket Expressions</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="regular-expression-extensions"></span><h3 class="section">5.6 regular expression extensions</h3>

<p>The following sequences have special meaning inside regular expressions
(used in <a href="#Regexp-Addresses">addresses</a> and the <code>s</code> command).
</p>
<p>These can be used in both
<a href="#BRE-syntax">basic</a> and <a href="#ERE-syntax">extended</a>
regular expressions (that is, with or without the <samp>-E</samp>/<samp>-r</samp>
options).
</p>
<dl compact="compact">
<dt><span><code>\w</code></span></dt>
<dd><p>Matches any &ldquo;word&rdquo; character.  A &ldquo;word&rdquo; character is any
letter or digit or the underscore character.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\w/X/g'
XXX %-= XXX.
</pre></div>


</dd>
<dt><span><code>\W</code></span></dt>
<dd><p>Matches any &ldquo;non-word&rdquo; character.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\W/X/g'
abcXXXXXdefX
</pre></div>


</dd>
<dt><span><code>\b</code></span></dt>
<dd><p>Matches a word boundary; that is it matches if the character
to the left is a &ldquo;word&rdquo; character and the character to the
right is a &ldquo;non-word&rdquo; character, or vice-versa.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\b/X/g'
XabcX %-= XdefX.
</pre></div>


</dd>
<dt><span><code>\B</code></span></dt>
<dd><p>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 &ldquo;word&rdquo; characters or both &ldquo;non-word&rdquo;
characters.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\B/X/g'
aXbXc X%X-X=X dXeXf.X
</pre></div>


</dd>
<dt><span><code>\s</code></span></dt>
<dd><p>Matches whitespace characters (spaces and tabs).
Newlines embedded in the pattern/hold spaces will also match:
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\s/X/g'
abcX%-=Xdef.
</pre></div>


</dd>
<dt><span><code>\S</code></span></dt>
<dd><p>Matches non-whitespace characters.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\S/X/g'
XXX XXX XXXX
</pre></div>


</dd>
<dt><span><code>\&lt;</code></span></dt>
<dd><p>Matches the beginning of a word.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\&lt;/X/g'
Xabc %-= Xdef.
</pre></div>


</dd>
<dt><span><code>\&gt;</code></span></dt>
<dd><p>Matches the end of a word.
</p>
<div class="example">
<pre class="example">$ echo &quot;abc %-= def.&quot; | sed 's/\&gt;/X/g'
abcX %-= defX.
</pre></div>


</dd>
<dt><span><code>\`</code></span></dt>
<dd><p>Matches only at the start of pattern space.  This is different
from <code>^</code> in multi-line mode.
</p>
<p>Compare the following two examples:
</p>
<div class="example">
<pre class="example">$ printf &quot;a\nb\nc\n&quot; | sed 'N;N;s/^/X/gm'
Xa
Xb
Xc

$ printf &quot;a\nb\nc\n&quot; | sed 'N;N;s/\`/X/gm'
Xa
b
c
</pre></div>

</dd>
<dt><span><code>\'</code></span></dt>
<dd><p>Matches only at the end of pattern space.  This is different
from <code>$</code> in multi-line mode.
</p>


</dd>
</dl>


<hr>
</div>
<div class="section" id="Back_002dreferences-and-Subexpressions">
<div class="header">
<p>
Next: <a href="#Escapes" accesskey="n" rel="next">Escape Sequences - specifying special characters</a>, Previous: <a href="#regexp-extensions" accesskey="p" rel="prev">regular expression extensions</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Back_002dreferences-and-Subexpressions-1"></span><h3 class="section">5.7 Back-references and Subexpressions</h3>
<span id="index-subexpression"></span>
<span id="index-back_002dreference"></span>

<p><em>back-references</em> are regular expression commands which refer to a
previous part of the matched regular expression.  Back-references are
specified with backslash and a single digit (e.g. &lsquo;<samp>\1</samp>&rsquo;).  The
part of the regular expression they refer to is called a
<em>subexpression</em>, and is designated with parentheses.
</p>
<p>Back-references and subexpressions are used in two cases: in the
regular expression search pattern, and in the <var>replacement</var> part
of the <code>s</code> command (see <a href="#Regexp-Addresses">Regular
Expression Addresses</a> and <a href="#The-_0022s_0022-Command">The <code>s</code> Command</a>).
</p>
<p>In a regular expression pattern, back-references are used to match
the same content as a previously matched subexpression.  In the
following example, the subexpression is &lsquo;<samp>.</samp>&rsquo; - any single
character (being surrounded by parentheses makes it a
subexpression). The back-reference &lsquo;<samp>\1</samp>&rsquo; asks to match the same
content (same character) as the sub-expression.
</p>
<p>The command below matches words starting with any character,
followed by the letter &lsquo;<samp>o</samp>&rsquo;, followed by the same character as the
first.
</p>
<div class="example">
<pre class="example">$ sed -E -n '/^(.)o\1$/p' /usr/share/dict/words
bob
mom
non
pop
sos
tot
wow
</pre></div>

<p>Multiple subexpressions are automatically numbered from
left-to-right. This command searches for 6-letter
palindromes (the first three letters are 3 subexpressions,
followed by 3 back-references in reverse order):
</p>
<div class="example">
<pre class="example">$ sed -E -n '/^(.)(.)(.)\3\2\1$/p' /usr/share/dict/words
redder
</pre></div>

<p>In the <code>s</code> command, back-references can be
used in the <var>replacement</var> part to refer back to subexpressions in
the <var>regexp</var> part.
</p>
<p>The following example uses two subexpressions in the regular
expression to match two space-separated words. The back-references in
the <var>replacement</var> part prints the words in a different order:
</p>
<div class="example">
<pre class="example">$ echo &quot;James Bond&quot; | sed -E 's/(.*) (.*)/The name is \2, \1 \2./'
The name is Bond, James Bond.
</pre></div>


<p>When used with alternation, if the group does not participate in the
match then the back-reference makes the whole match fail.  For
example, &lsquo;<samp>a(.)|b\1</samp>&rsquo; will not match &lsquo;<samp>ba</samp>&rsquo;.  When multiple
regular expressions are given with <samp>-e</samp> or from a file
(&lsquo;<samp>-f <var>file</var></samp>&rsquo;), back-references are local to each expression.
</p>

<hr>
</div>
<div class="section" id="Escapes">
<div class="header">
<p>
Next: <a href="#Locale-Considerations" accesskey="n" rel="next">Multibyte characters and Locale Considerations</a>, Previous: <a href="#Back_002dreferences-and-Subexpressions" accesskey="p" rel="prev">Back-references and Subexpressions</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Escape-Sequences-_002d-specifying-special-characters"></span><h3 class="section">5.8 Escape Sequences - specifying special characters</h3>

<span id="index-GNU-extensions_002c-special-escapes"></span>
<p>Until this chapter, we have only encountered escapes of the form
&lsquo;<samp>\^</samp>&rsquo;, which tell <code>sed</code> not to interpret the circumflex
as a special character, but rather to take it literally.  For
example, &lsquo;<samp>\*</samp>&rsquo; matches a single asterisk rather than zero
or more backslashes.
</p>
<span id="index-POSIXLY_005fCORRECT-behavior_002c-escapes"></span>
<p>This chapter introduces another kind of escape<a id="DOCF6" href="#FOOT6"><sup>6</sup></a>&mdash;that
is, escapes that are applied to a character or sequence of characters
that ordinarily are taken literally, and that <code>sed</code> 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 <code>sed</code> 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>
<p>The list of these escapes is:
</p>
<dl compact="compact">
<dt><span><code>\a</code></span></dt>
<dd><p>Produces or matches a <small>BEL</small> character, that is an &ldquo;alert&rdquo; (<small>ASCII</small> 7).
</p>
</dd>
<dt><span><code>\f</code></span></dt>
<dd><p>Produces or matches a form feed (<small>ASCII</small> 12).
</p>
</dd>
<dt><span><code>\n</code></span></dt>
<dd><p>Produces or matches a newline (<small>ASCII</small> 10).
</p>
</dd>
<dt><span><code>\r</code></span></dt>
<dd><p>Produces or matches a carriage return (<small>ASCII</small> 13).
</p>
</dd>
<dt><span><code>\t</code></span></dt>
<dd><p>Produces or matches a horizontal tab (<small>ASCII</small> 9).
</p>
</dd>
<dt><span><code>\v</code></span></dt>
<dd><p>Produces or matches a so called &ldquo;vertical tab&rdquo; (<small>ASCII</small> 11).
</p>
</dd>
<dt><span><code>\c<var>x</var></code></span></dt>
<dd><p>Produces or matches <kbd><small>CONTROL</small>-<var>x</var></kbd>, where <var>x</var> is
any character.  The precise effect of &lsquo;<samp>\c<var>x</var></samp>&rsquo; 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 &lsquo;<samp>\cz</samp>&rsquo; becomes
hex 1A, but &lsquo;<samp>\c{</samp>&rsquo; becomes hex 3B, while &lsquo;<samp>\c;</samp>&rsquo; becomes hex 7B.
</p>
</dd>
<dt><span><code>\d<var>xxx</var></code></span></dt>
<dd><p>Produces or matches a character whose decimal <small>ASCII</small> value is <var>xxx</var>.
</p>
</dd>
<dt><span><code>\o<var>xxx</var></code></span></dt>
<dd><p>Produces or matches a character whose octal <small>ASCII</small> value is <var>xxx</var>.
</p>
</dd>
<dt><span><code>\x<var>xx</var></code></span></dt>
<dd><p>Produces or matches a character whose hexadecimal <small>ASCII</small> value is <var>xx</var>.
</p></dd>
</dl>

<p>&lsquo;<samp>\b</samp>&rsquo; (backspace) was omitted because of the conflict with
the existing &ldquo;word boundary&rdquo; meaning.
</p>
<ul class="section-toc">
<li><a href="#Escaping-Precedence" accesskey="1">Escaping Precedence</a></li>
</ul>
<div class="subsection" id="Escaping-Precedence">
<h4 class="subsection">5.8.1 Escaping Precedence</h4>

<p>GNU <code>sed</code> processes escape sequences <em>before</em> passing
the text onto the regular-expression matching of the <code>s///</code> command
and Address matching. Thus the following two commands are equivalent
(&lsquo;<samp>0x5e</samp>&rsquo; is the hexadecimal <small>ASCII</small> value of the character &lsquo;<samp>^</samp>&rsquo;):
</p>
<div class="example">
<pre class="example">$ echo 'a^c' | sed 's/^/b/'
ba^c

$ echo 'a^c' | sed 's/\x5e/b/'
ba^c
</pre></div>

<p>As are the following (&lsquo;<samp>0x5b</samp>&rsquo;,&lsquo;<samp>0x5d</samp>&rsquo; are the hexadecimal
<small>ASCII</small> values of &lsquo;<samp>[</samp>&rsquo;,&lsquo;<samp>]</samp>&rsquo;, respectively):
</p>
<div class="example">
<pre class="example">$ echo abc | sed 's/[a]/x/'
Xbc
$ echo abc | sed 's/\x5ba\x5d/x/'
Xbc
</pre></div>

<p>However it is recommended to avoid such special characters
due to unexpected edge-cases. For example, the following
are not equivalent:
</p>
<div class="example">
<pre class="example">$ echo 'a^c' | sed 's/\^/b/'
abc

$ echo 'a^c' | sed 's/\\\x5e/b/'
a^c
</pre></div>



<hr>
</div>
</div>
<div class="section" id="Locale-Considerations">
<div class="header">
<p>
Previous: <a href="#Escapes" accesskey="p" rel="prev">Escape Sequences - specifying special characters</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Multibyte-characters-and-Locale-Considerations"></span><h3 class="section">5.9 Multibyte characters and Locale Considerations</h3>

<p>GNU <code>sed</code> processes valid multibyte characters in multibyte locales
(e.g. <code>UTF-8</code>).  <a id="DOCF7" href="#FOOT7"><sup>7</sup></a>
</p>
<p>The following example uses the Greek letter Capital Sigma
(&#x03A3;,
Unicode code point <code>0x03A3</code>). In a <code>UTF-8</code> locale,
<code>sed</code> correctly processes the Sigma as one character despite
it being 2 octets (bytes):
</p>
<div class="example">
<pre class="example">$ locale | grep LANG
LANG=en_US.UTF-8

$ printf 'a\u03A3b'
a&#x03A3;b

$ printf 'a\u03A3b' | sed 's/./X/g'
XXX

$ printf 'a\u03A3b' | od -tx1 -An
 61 ce a3 62
</pre></div>

<p>To force <code>sed</code> to process octets separately, use the <code>C</code> locale
(also known as the <code>POSIX</code> locale):
</p>
<div class="example">
<pre class="example">$ printf 'a\u03A3b' | LC_ALL=C sed 's/./X/g'
XXXX
</pre></div>

<ul class="section-toc">
<li><a href="#Invalid-multibyte-characters" accesskey="1">Invalid multibyte characters</a></li>
<li><a href="#Upper_002fLower-case-conversion" accesskey="2">Upper/Lower case conversion</a></li>
<li><a href="#Multibyte-regexp-character-classes" accesskey="3">Multibyte regexp character classes</a></li>
</ul>
<div class="subsection" id="Invalid-multibyte-characters">
<h4 class="subsection">5.9.1 Invalid multibyte characters</h4>

<p><code>sed</code>&rsquo;s regular expressions <em>do not</em> match
invalid multibyte sequences in a multibyte locale.
</p>
<p>In the following examples, the ascii value <code>0xCE</code> is
an incomplete multibyte character (shown here as &#xFFFD;).
The regular expression &lsquo;<samp>.</samp>&rsquo; does not match it:
</p>
<div class="example">
<pre class="example">$ printf 'a\xCEb\n'
a&#xFFFD;e

$ printf 'a\xCEb\n' | sed 's/./X/g'
X&#xFFFD;X

$ printf 'a\xCEc\n' | sed 's/./X/g' | od -tx1c -An
  58  ce  58  0a
   X      X   \n
</pre></div>

<p>Similarly, the &rsquo;catch-all&rsquo; regular expression &lsquo;<samp>.*</samp>&rsquo; does not
match the entire line:
</p>
<div class="example">
<pre class="example">$ printf 'a\xCEc\n' | sed 's/.*//' | od -tx1c -An
  ce  63  0a
       c  \n
</pre></div>

<p>GNU <code>sed</code> offers the special <code>z</code> command to clear the
current pattern space regardless of invalid multibyte characters
(i.e. it works like <code>s/.*//</code> but also removes invalid multibyte
characters):
</p>
<div class="example">
<pre class="example">$ printf 'a\xCEc\n' | sed 'z' | od -tx1c -An
   0a
   \n
</pre></div>

<p>Alternatively, force the <code>C</code> locale to process
each octet separately (every octet is a valid character in the <code>C</code>
locale):
</p>
<div class="example">
<pre class="example">$ printf 'a\xCEc\n' | LC_ALL=C sed 's/.*//' | od -tx1c -An
  0a
  \n
</pre></div>


<p><code>sed</code>&rsquo;s inability to process invalid multibyte characters
can be used to detect such invalid sequences in a file.
In the following examples, the <code>\xCE\xCE</code> is an invalid
multibyte sequence, while <code>\xCE\A3</code> is a valid multibyte sequence
(of the Greek Sigma character).
</p>
<p>The following <code>sed</code> program removes all valid
characters using <code>s/.//g</code>.  Any content left in the pattern space
(the invalid characters) are added to the hold space using the
<code>H</code> command. On the last line (<code>$</code>), the hold space is retrieved
(<code>x</code>), newlines are removed (<code>s/\n//g</code>), and any remaining
octets are printed unambiguously (<code>l</code>).  Thus, any invalid
multibyte sequences are printed as octal values:
</p>
<div class="example">
<pre class="example">$ printf 'ab\nc\n\xCE\xCEde\n\xCE\xA3f\n' &gt; invalid.txt

$ cat invalid.txt
ab
c
&#xFFFD;&#xFFFD;de
&#x03A3;f

$ sed -n 's/.//g ; H ; ${x;s/\n//g;l}' invalid.txt
\316\316$
</pre></div>

<p>With a few more commands, <code>sed</code> can print
the exact line number corresponding to each invalid characters (line 3).
These characters can then be removed by forcing the <code>C</code> locale
and using octal escape sequences:
</p>
<div class="example">
<pre class="example">$ sed -n 's/.//g;=;l' invalid.txt | paste - -  | awk '$2!=&quot;$&quot;'
3       \316\316$

$ LC_ALL=C sed '3s/\o316\o316//' invalid.txt &gt; fixed.txt
</pre></div>

</div>
<div class="subsection" id="Upper_002fLower-case-conversion">
<h4 class="subsection">5.9.2 Upper/Lower case conversion</h4>


<p>GNU <code>sed</code>&rsquo;s substitute command (<code>s</code>) supports upper/lower
case conversions using <code>\U</code>,<code>\L</code> codes.
These conversions support multibyte characters:
</p>
<div class="example">
<pre class="example">$ printf 'ABC\u03a3\n'
ABC&#x03A3;

$ printf 'ABC\u03a3\n' | sed 's/.*/\L&amp;/'
abc&#x03C3;
</pre></div>

<p>See <a href="#The-_0022s_0022-Command">The <code>s</code> Command</a>.
</p>

</div>
<div class="subsection" id="Multibyte-regexp-character-classes">
<h4 class="subsection">5.9.3 Multibyte regexp character classes</h4>


<p>In other locales, the sorting sequence is not specified, and
&lsquo;<samp>[a-d]</samp>&rsquo; might be equivalent to &lsquo;<samp>[abcd]</samp>&rsquo; or to
&lsquo;<samp>[aBbCcDd]</samp>&rsquo;, or it might fail to match any character, or the set of
characters that it matches might even be erratic.
To obtain the traditional interpretation
of bracket expressions, you can use the &lsquo;<samp>C</samp>&rsquo; locale by setting the
<code>LC_ALL</code> environment variable to the value &lsquo;<samp>C</samp>&rsquo;.
</p>
<div class="example">
<pre class="example"># TODO: is there any real-world system/locale where 'A'
#       is replaced by '-' ?
$ echo A | sed 's/[a-z]/-/'
A
</pre></div>

<p>Their interpretation depends on the <code>LC_CTYPE</code> locale;
for example, &lsquo;<samp>[[:alnum:]]</samp>&rsquo; means the character class of numbers and letters
in the current locale.
</p>
<p>TODO: show example of collation
</p>
<div class="example">
<pre class="example"># TODO: this works on glibc systems, not on musl-libc/freebsd/macosx.
$ printf 'cliché\n' | LC_ALL=fr_FR.utf8 sed 's/[[=e=]]/X/g'
clichX
</pre></div>


<hr>
</div>
</div>
</div>
<div class="chapter" id="advanced-sed">
<div class="header">
<p>
Next: <a href="#Examples" accesskey="n" rel="next">Some Sample Scripts</a>, Previous: <a href="#sed-regular-expressions" accesskey="p" rel="prev">Regular Expressions: selecting text</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Advanced-sed_003a-cycles-and-buffers"></span><h2 class="chapter">6 Advanced <code>sed</code>: cycles and buffers</h2>


<ul class="section-toc">
<li><a href="#Execution-Cycle" accesskey="1">How <code>sed</code> Works</a></li>
<li><a href="#Hold-and-Pattern-Buffers" accesskey="2">Hold and Pattern Buffers</a></li>
<li><a href="#Multiline-techniques" accesskey="3">Multiline techniques - using D,G,H,N,P to process multiple lines</a></li>
<li><a href="#Branching-and-flow-control" accesskey="4">Branching and Flow Control</a></li>
</ul>
<hr>
<div class="section" id="Execution-Cycle">
<div class="header">
<p>
Next: <a href="#Hold-and-Pattern-Buffers" accesskey="n" rel="next">Hold and Pattern Buffers</a>, Up: <a href="#advanced-sed" accesskey="u" rel="up">Advanced <code>sed</code>: cycles and buffers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="How-sed-Works"></span><h3 class="section">6.1 How <code>sed</code> Works</h3>

<span id="index-Buffer-spaces_002c-pattern-and-hold"></span>
<span id="index-Spaces_002c-pattern-and-hold"></span>
<span id="index-Pattern-space_002c-definition"></span>
<span id="index-Hold-space_002c-definition"></span>
<p><code>sed</code> maintains two data buffers: the active <em>pattern</em> space,
and the auxiliary <em>hold</em> space. Both are initially empty.
</p>
<p><code>sed</code> operates by performing the following cycle on each
line of input: first, <code>sed</code> reads one line from the input
stream, removes any trailing newline, and places it in the pattern space.
Then commands are executed; each command can have an address associated
to it: addresses are a kind of condition code, and a command is only
executed if the condition is verified before the command is to be
executed.
</p>
<p>When the end of the script is reached, unless the <samp>-n</samp> option
is in use, the contents of pattern space are printed out to the output
stream, adding back the trailing newline if it was removed.<a id="DOCF8" href="#FOOT8"><sup>8</sup></a> Then the next cycle starts for the next
input line.
</p>
<p>Unless special commands (like &lsquo;<samp>D</samp>&rsquo;) are used, the pattern space is
deleted between two cycles. The hold space, on the other hand, keeps
its data between cycles (see commands &lsquo;<samp>h</samp>&rsquo;, &lsquo;<samp>H</samp>&rsquo;, &lsquo;<samp>x</samp>&rsquo;,
&lsquo;<samp>g</samp>&rsquo;, &lsquo;<samp>G</samp>&rsquo; to move data between both buffers).
</p>
<hr>
</div>
<div class="section" id="Hold-and-Pattern-Buffers">
<div class="header">
<p>
Next: <a href="#Multiline-techniques" accesskey="n" rel="next">Multiline techniques - using D,G,H,N,P to process multiple lines</a>, Previous: <a href="#Execution-Cycle" accesskey="p" rel="prev">How <code>sed</code> Works</a>, Up: <a href="#advanced-sed" accesskey="u" rel="up">Advanced <code>sed</code>: cycles and buffers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Hold-and-Pattern-Buffers-1"></span><h3 class="section">6.2 Hold and Pattern Buffers</h3>

<p>TODO
</p>
<hr>
</div>
<div class="section" id="Multiline-techniques">
<div class="header">
<p>
Next: <a href="#Branching-and-flow-control" accesskey="n" rel="next">Branching and Flow Control</a>, Previous: <a href="#Hold-and-Pattern-Buffers" accesskey="p" rel="prev">Hold and Pattern Buffers</a>, Up: <a href="#advanced-sed" accesskey="u" rel="up">Advanced <code>sed</code>: cycles and buffers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Multiline-techniques-_002d-using-D_002cG_002cH_002cN_002cP-to-process-multiple-lines"></span><h3 class="section">6.3 Multiline techniques - using D,G,H,N,P to process multiple lines</h3>

<p>Multiple lines can be processed as one buffer using the
<code>D</code>,<code>G</code>,<code>H</code>,<code>N</code>,<code>P</code>. They are similar to
their lowercase counterparts (<code>d</code>,<code>g</code>,
<code>h</code>,<code>n</code>,<code>p</code>), except that these commands append or
subtract data while respecting embedded newlines - allowing adding and
removing lines from the pattern and hold spaces.
</p>
<p>They operate as follows:
</p><dl compact="compact">
<dt><span><code>D</code></span></dt>
<dd><p><em>deletes</em> line from the pattern space until the first newline,
and restarts the cycle.
</p>
</dd>
<dt><span><code>G</code></span></dt>
<dd><p><em>appends</em> line from the hold space to the pattern space, with a
newline before it.
</p>
</dd>
<dt><span><code>H</code></span></dt>
<dd><p><em>appends</em> line from the pattern space to the hold space, with a
newline before it.
</p>
</dd>
<dt><span><code>N</code></span></dt>
<dd><p><em>appends</em> line from the input file to the pattern space.
</p>
</dd>
<dt><span><code>P</code></span></dt>
<dd><p><em>prints</em> line from the pattern space until the first newline.
</p>
</dd>
</dl>


<p>The following example illustrates the operation of <code>N</code> and
<code>D</code> commands:
</p>
<div class="example">
<pre class="example">$ seq 6 | sed -n 'N;l;D'
1\n2$
2\n3$
3\n4$
4\n5$
5\n6$
</pre></div>

<ol>
<li> <code>sed</code> starts by reading the first line into the pattern space
(i.e. &lsquo;<samp>1</samp>&rsquo;).
</li><li> At the beginning of every cycle, the <code>N</code>
command appends a newline and the next line to the pattern space
(i.e. &lsquo;<samp>1</samp>&rsquo;, &lsquo;<samp>\n</samp>&rsquo;, &lsquo;<samp>2</samp>&rsquo; in the first cycle).
</li><li> The <code>l</code> command prints the content of the pattern space
unambiguously.
</li><li> The <code>D</code> command then removes the content of pattern
space up to the first newline (leaving &lsquo;<samp>2</samp>&rsquo; at the end of
the first cycle).
</li><li> At the next cycle the <code>N</code> command appends a
newline and the next input line to the pattern space
(e.g. &lsquo;<samp>2</samp>&rsquo;, &lsquo;<samp>\n</samp>&rsquo;, &lsquo;<samp>3</samp>&rsquo;).
</li></ol>


<span id="index-processing-paragraphs"></span>
<span id="index-paragraphs_002c-processing"></span>
<p>A common technique to process blocks of text such as paragraphs
(instead of line-by-line) is using the following construct:
</p>
<div class="example">
<pre class="example">sed '/./{H;$!d} ; x ; s/REGEXP/REPLACEMENT/'
</pre></div>

<ol>
<li> The first expression, <code>/./{H;$!d}</code> operates on all non-empty lines,
and adds the current line (in the pattern space) to the hold space.
On all lines except the last, the pattern space is deleted and the cycle is
restarted.

</li><li> The other expressions <code>x</code> and <code>s</code> are executed only on empty
lines (i.e. paragraph separators). The <code>x</code> command fetches the
accumulated lines from the hold space back to the pattern space. The
<code>s///</code> command then operates on all the text in the paragraph
(including the embedded newlines).
</li></ol>

<p>The following example demonstrates this technique:
</p><div class="example">
<pre class="example">$ cat input.txt
a a a aa aaa
aaaa aaaa aa
aaaa aaa aaa

bbbb bbb bbb
bb bb bbb bb
bbbbbbbb bbb

ccc ccc cccc
cccc ccccc c
cc cc cc cc

$ sed '/./{H;$!d} ; x ; s/^/\nSTART--&gt;/ ; s/$/\n&lt;--END/' input.txt

START--&gt;
a a a aa aaa
aaaa aaaa aa
aaaa aaa aaa
&lt;--END

START--&gt;
bbbb bbb bbb
bb bb bbb bb
bbbbbbbb bbb
&lt;--END

START--&gt;
ccc ccc cccc
cccc ccccc c
cc cc cc cc
&lt;--END
</pre></div>

<p>For more annotated examples, see <a href="#Text-search-across-multiple-lines">Text search across multiple lines</a>
and <a href="#Line-length-adjustment">Line length adjustment</a>.
</p>
<hr>
</div>
<div class="section" id="Branching-and-flow-control">
<div class="header">
<p>
Previous: <a href="#Multiline-techniques" accesskey="p" rel="prev">Multiline techniques - using D,G,H,N,P to process multiple lines</a>, Up: <a href="#advanced-sed" accesskey="u" rel="up">Advanced <code>sed</code>: cycles and buffers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Branching-and-Flow-Control"></span><h3 class="section">6.4 Branching and Flow Control</h3>

<p>The branching commands <code>b</code>, <code>t</code>, and <code>T</code> enable
changing the flow of <code>sed</code> programs.
</p>
<p>By default, <code>sed</code> reads an input line into the pattern buffer,
then continues to processes all commands in order.
Commands without addresses affect all lines.
Commands with addresses affect only matching lines.
See <a href="#Execution-Cycle">How <code>sed</code> Works</a> and <a href="#Addresses-overview">Addresses overview</a>.
</p>
<p><code>sed</code> does not support a typical <code>if/then</code> construct.
Instead, some commands can be used as conditionals or to change the
default flow control:
</p>
<dl compact="compact">
<dt><span><code>d</code></span></dt>
<dd><p>delete (clears) the current pattern space,
and restart the program cycle without processing the rest of the commands
and without printing the pattern space.
</p>
</dd>
<dt><span><code>D</code></span></dt>
<dd><p>delete the contents of the pattern space <em>up to the first newline</em>,
and restart the program cycle without processing the rest of
the commands and without printing the pattern space.
</p>
</dd>
<dt><span><code>[addr]X</code></span></dt>
<dt><span><code>[addr]{ X ; X ; X }</code></span></dt>
<dt><span><code>/regexp/X</code></span></dt>
<dt><span><code>/regexp/{ X ; X ; X }</code></span></dt>
<dd><p>Addresses and regular expressions can be used as an <code>if/then</code>
conditional: If <var>[addr]</var> matches the current pattern space,
execute the command(s).
For example: The command <code>/^#/d</code> means:
<em>if</em> the current pattern matches the regular expression <code>^#</code> (a line
starting with a hash), <em>then</em> execute the <code>d</code> command:
delete the line without printing it, and restart the program cycle
immediately.
</p>
</dd>
<dt><span><code>b</code></span></dt>
<dd><p>branch unconditionally (that is: always jump to a label, skipping
or repeating other commands, without restarting a new cycle). Combined
with an address, the branch can be conditionally executed on matched
lines.
</p>
</dd>
<dt><span><code>t</code></span></dt>
<dd><p>branch conditionally (that is: jump to a label) <em>only if</em> a
<code>s///</code> command has succeeded since the last input line was read
or another conditional branch was taken.
</p>
</dd>
<dt><span><code>T</code></span></dt>
<dd><p>similar but opposite to the <code>t</code> command: branch only if
there has been <em>no</em> successful substitutions since the last
input line was read.
</p></dd>
</dl>


<p>The following two <code>sed</code> programs are equivalent.  The first
(contrived) example uses the <code>b</code> command to skip the <code>s///</code>
command on lines containing &lsquo;<samp>1</samp>&rsquo;.  The second example uses an
address with negation (&lsquo;<samp>!</samp>&rsquo;)  to perform substitution only on
desired lines.  The <code>y///</code> command is still executed on all
lines:
</p>
<div class="example">
<pre class="example">$ printf '%s\n' a1 a2 a3 | sed -E '/1/bx ; s/a/z/ ; :x ; y/123/456/'
a4
z5
z6

$ printf '%s\n' a1 a2 a3 | sed -E '/1/!s/a/z/ ; y/123/456/'
a4
z5
z6
</pre></div>



<ul class="section-toc">
<li><a href="#Branching-and-Cycles" accesskey="1">Branching and Cycles</a></li>
<li><a href="#Branching-example_003a-joining-lines" accesskey="2">Branching example: joining lines</a></li>
</ul>
<div class="subsection" id="Branching-and-Cycles">
<h4 class="subsection">6.4.1 Branching and Cycles</h4>
<span id="index-labels"></span>
<span id="index-omitting-labels"></span>
<span id="index-cycle_002c-restarting"></span>
<span id="index-restarting-a-cycle"></span>
<p>The <code>b</code>,<code>t</code> and <code>T</code> commands can be followed by a label
(typically a single letter). Labels are defined with a colon followed by
one or more letters (e.g. &lsquo;<samp>:x</samp>&rsquo;). If the label is omitted the
branch commands restart the cycle.  Note the difference between
branching to a label and restarting the cycle: when a cycle is
restarted, <code>sed</code> first prints the current content of the
pattern space, then reads the next input line into the pattern space;
Jumping to a label (even if it is at the beginning of the program)
does not print the pattern space and does not read the next input line.
</p>
<p>The following program is a no-op. The <code>b</code> command (the only command
in the program) does not have a label, and thus simply restarts the cycle.
On each cycle, the pattern space is printed and the next input line is read:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed b
1
2
3
</pre></div>

<span id="index-infinite-loop_002c-branching"></span>
<span id="index-branching_002c-infinite-loop"></span>
<p>The following example is an infinite-loop - it doesn&rsquo;t terminate and
doesn&rsquo;t print anything. The <code>b</code> command jumps to the &lsquo;<samp>x</samp>&rsquo;
label, and a new cycle is never started:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed ':x ; bx'

# The above command requires gnu sed (which supports additional
# commands following a label, without a newline). A portable equivalent:
#     sed -e ':x' -e bx
</pre></div>

<span id="index-branching-and-n_002c-N"></span>
<span id="index-n_002c-and-branching"></span>
<span id="index-N_002c-and-branching"></span>
<p>Branching is often complemented with the <code>n</code> or <code>N</code> commands:
both commands read the next input line into the pattern space without waiting
for the cycle to restart. Before reading the next input line, <code>n</code>
prints the current pattern space then empties it, while <code>N</code>
appends a newline and the next input line to the pattern space.
</p>
<p>Consider the following two examples:
</p>
<div class="example">
<pre class="example">$ seq 3 | sed ':x ; n ; bx'
1
2
3

$ seq 3 | sed ':x ; N ; bx'
1
2
3
</pre></div>

<ul>
<li> Both examples do not inf-loop, despite never starting a new cycle.

</li><li> In the first example, the <code>n</code> commands first prints the content
of the pattern space, empties the pattern space then reads the next
input line.

</li><li> In the second example, the <code>N</code> commands appends the next input
line to the pattern space (with a newline).  Lines are accumulated in
the pattern space until there are no more input lines to read, then
the <code>N</code> command terminates the <code>sed</code> program. When the
program terminates, the end-of-cycle actions are performed, and the
entire pattern space is printed.

</li><li> The second example requires GNU <code>sed</code>,
because it uses the non-POSIX-standard behavior of <code>N</code>.
See the &ldquo;<code>N</code> command on the last line&rdquo; paragraph
in <a href="#Reporting-Bugs">Reporting Bugs</a>.

</li><li> To further examine the difference between the two examples,
try the following commands:
<div class="example">
<pre class="example">printf '%s\n' aa bb cc dd | sed ':x ; n ; = ; bx'
printf '%s\n' aa bb cc dd | sed ':x ; N ; = ; bx'
printf '%s\n' aa bb cc dd | sed ':x ; n ; s/\n/***/ ; bx'
printf '%s\n' aa bb cc dd | sed ':x ; N ; s/\n/***/ ; bx'
</pre></div>

</li></ul>



</div>
<div class="subsection" id="Branching-example_003a-joining-lines">
<h4 class="subsection">6.4.2 Branching example: joining lines</h4>

<span id="index-joining-lines-with-branching"></span>
<span id="index-branching_002c-joining-lines"></span>
<span id="index-quoted_002dprintable-lines_002c-joining"></span>
<span id="index-joining-quoted_002dprintable-lines"></span>
<span id="index-t_002c-joining-lines-with"></span>
<span id="index-b_002c-joining-lines-with"></span>
<span id="index-b_002c-versus-t"></span>
<span id="index-t_002c-versus-b"></span>
<p>As a real-world example of using branching, consider the case of
<a href="https://en.wikipedia.org/wiki/Quoted-printable">quoted-printable</a> files,
typically used to encode email messages.
In these files long lines are split and marked with a <em>soft line break</em>
consisting of a single &lsquo;<samp>=</samp>&rsquo; character at the end of the line:
</p>
<div class="example">
<pre class="example">$ cat jaques.txt
All the wor=
ld's a stag=
e,
And all the=
 men and wo=
men merely =
players:
They have t=
heir exits =
and their e=
ntrances;
And one man=
 in his tim=
e plays man=
y parts.
</pre></div>


<p>The following program uses an address match &lsquo;<samp>/=$/</samp>&rsquo; as a
conditional: If the current pattern space ends with a &lsquo;<samp>=</samp>&rsquo;, it
reads the next input line using <code>N</code>, replaces all &lsquo;<samp>=</samp>&rsquo;
characters which are followed by a newline, and unconditionally
branches (<code>b</code>) to the beginning of the program without restarting
a new cycle. If the pattern space does not ends with &lsquo;<samp>=</samp>&rsquo;, the
default action is performed: the pattern space is printed and a new
cycle is started:
</p>
<div class="example">
<pre class="example">$ sed ':x ; /=$/ { N ; s/=\n//g ; bx }' jaques.txt
All the world's a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts.
</pre></div>

<p>Here&rsquo;s an alternative program with a slightly different approach: On
all lines except the last, <code>N</code> appends the line to the pattern
space.  A substitution command then removes soft line breaks
(&lsquo;<samp>=</samp>&rsquo; at the end of a line, i.e. followed by a newline) by replacing
them with an empty string.
<em>if</em> the substitution was successful (meaning the pattern space contained
a line which should be joined), The conditional branch command <code>t</code> jumps
to the beginning of the program without completing or restarting the cycle.
If the substitution failed (meaning there were no soft line breaks),
The <code>t</code> command will <em>not</em> branch. Then, <code>P</code> will
print the pattern space content until the first newline, and <code>D</code>
will delete the pattern space content until the first new line.
(To learn more about <code>N</code>, <code>P</code> and <code>D</code> commands
see <a href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a>).
</p>

<div class="example">
<pre class="example">$ sed ':x ; $!N ; s/=\n// ; tx ; P ; D' jaques.txt
All the world's a stage,
And all the men and women merely players:
They have their exits and their entrances;
And one man in his time plays many parts.
</pre></div>


<p>For more line-joining examples see <a href="#Joining-lines">Joining lines</a>.
</p>

<hr>
</div>
</div>
</div>
<div class="chapter" id="Examples">
<div class="header">
<p>
Next: <a href="#Limitations" accesskey="n" rel="next">GNU <code>sed</code>&rsquo;s Limitations and Non-limitations</a>, Previous: <a href="#advanced-sed" accesskey="p" rel="prev">Advanced <code>sed</code>: cycles and buffers</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Some-Sample-Scripts"></span><h2 class="chapter">7 Some Sample Scripts</h2>

<p>Here are some <code>sed</code> scripts to guide you in the art of mastering
<code>sed</code>.
</p>

<ul class="section-toc">
<li><a href="#Joining-lines" accesskey="1">Joining lines</a></li>
<li><a href="#Centering-lines" accesskey="2">Centering Lines</a></li>
<li><a href="#Increment-a-number" accesskey="3">Increment a Number</a></li>
<li><a href="#Rename-files-to-lower-case" accesskey="4">Rename Files to Lower Case</a></li>
<li><a href="#Print-bash-environment" accesskey="5">Print <code>bash</code> Environment</a></li>
<li><a href="#Reverse-chars-of-lines" accesskey="6">Reverse Characters of Lines</a></li>
<li><a href="#Text-search-across-multiple-lines" accesskey="7">Text search across multiple lines</a></li>
<li><a href="#Line-length-adjustment" accesskey="8">Line length adjustment</a></li>
<li><a href="#Adding-a-header-to-multiple-files" accesskey="9">Adding a header to multiple files</a></li>
<li><a href="#tac">Reverse Lines of Files</a></li>
<li><a href="#cat-_002dn">Numbering Lines</a></li>
<li><a href="#cat-_002db">Numbering Non-blank Lines</a></li>
<li><a href="#wc-_002dc">Counting Characters</a></li>
<li><a href="#wc-_002dw">Counting Words</a></li>
<li><a href="#wc-_002dl">Counting Lines</a></li>
<li><a href="#head">Printing the First Lines</a></li>
<li><a href="#tail">Printing the Last Lines</a></li>
<li><a href="#uniq">Make Duplicate Lines Unique</a></li>
<li><a href="#uniq-_002dd">Print Duplicated Lines of Input</a></li>
<li><a href="#uniq-_002du">Remove All Duplicated Lines</a></li>
<li><a href="#cat-_002ds">Squeezing Blank Lines</a></li>
</ul>
<hr>
<div class="section" id="Joining-lines">
<div class="header">
<p>
Next: <a href="#Centering-lines" accesskey="n" rel="next">Centering Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Joining-lines-1"></span><h3 class="section">7.1 Joining lines</h3>

<p>This section uses <code>N</code>, <code>D</code> and <code>P</code> commands to process
multiple lines, and the <code>b</code> and <code>t</code> commands for branching.
See <a href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a> and <a href="#Branching-and-flow-control">Branching and Flow Control</a>.
</p>
<p>Join specific lines (e.g. if lines 2 and 3 need to be joined):
</p>
<div class="example">
<pre class="example">$ cat lines.txt
hello
hel
lo
hello

$ sed '2{N;s/\n//;}' lines.txt
hello
hello
hello
</pre></div>

<p>Join backslash-continued lines:
</p>
<div class="example">
<pre class="example">$ cat 1.txt
this \
is \
a \
long \
line
and another \
line

$ sed -e ':x /\\$/ { N; s/\\\n//g ; bx }'  1.txt
this is a long line
and another line


#TODO: The above requires gnu sed.
#      non-gnu seds need newlines after ':' and 'b'
</pre></div>

<p>Join lines that start with whitespace (e.g SMTP headers):
</p>
<div class="example">
<pre class="example">$ cat 2.txt
Subject: Hello
    World
Content-Type: multipart/alternative;
    boundary=94eb2c190cc6370f06054535da6a
Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT)
Authentication-Results: mx.gnu.org;
       dkim=pass header.i=@gnu.org;
       spf=pass
Message-ID: &lt;abcdef@gnu.org&gt;
From: John Doe &lt;jdoe@gnu.org&gt;
To: Jane Smith &lt;jsmith@gnu.org&gt;

$ sed -E ':a ; $!N ; s/\n\s+/ / ; ta ; P ; D' 2.txt
Subject: Hello World
Content-Type: multipart/alternative; boundary=94eb2c190cc6370f06054535da6a
Date: Tue, 3 Jan 2017 19:41:16 +0000 (GMT)
Authentication-Results: mx.gnu.org; dkim=pass header.i=@gnu.org; spf=pass
Message-ID: &lt;abcdef@gnu.org&gt;
From: John Doe &lt;jdoe@gnu.org&gt;
To: Jane Smith &lt;jsmith@gnu.org&gt;

# A portable (non-gnu) variation:
#   sed -e :a -e '$!N;s/\n  */ /;ta' -e 'P;D'
</pre></div>


<hr>
</div>
<div class="section" id="Centering-lines">
<div class="header">
<p>
Next: <a href="#Increment-a-number" accesskey="n" rel="next">Increment a Number</a>, Previous: <a href="#Joining-lines" accesskey="p" rel="prev">Joining lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Centering-Lines"></span><h3 class="section">7.2 Centering Lines</h3>

<p>This script centers all lines of a file on a 80 columns width.
To change that width, the number in <code>\{&hellip;\}</code> must be
replaced, and the number of added spaces also must be changed.
</p>
<p>Note how the buffer commands are used to separate parts in
the regular expressions to be matched&mdash;this is a common
technique.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

</pre><pre class="example"># Put 80 spaces in the buffer
1 {
  x
  s/^$/          /
  s/^.*$/&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;/
  x
}
</pre><pre class="example">

</pre><pre class="example"># delete leading and trailing spaces
y/<kbd><span class="key">TAB</span></kbd>/ /
s/^ *//
s/ *$//
</pre><pre class="example">

</pre><pre class="example"># add a newline and 80 spaces to end of line
G
</pre><pre class="example">

</pre><pre class="example"># keep first 81 chars (80 + a newline)
s/^\(.\{81\}\).*$/\1/
</pre><pre class="example">

</pre><pre class="example"># \2 matches half of the spaces, which are moved to the beginning
s/^\(.*\)\n\(.*\)\2/\2\1/
</pre></div>

<hr>
</div>
<div class="section" id="Increment-a-number">
<div class="header">
<p>
Next: <a href="#Rename-files-to-lower-case" accesskey="n" rel="next">Rename Files to Lower Case</a>, Previous: <a href="#Centering-lines" accesskey="p" rel="prev">Centering Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Increment-a-Number"></span><h3 class="section">7.3 Increment a Number</h3>

<p>This script is one of a few that demonstrate how to do arithmetic
in <code>sed</code>.  This is indeed possible,<a id="DOCF9" href="#FOOT9"><sup>9</sup></a> but must be done manually.
</p>
<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&rsquo;t have a nine.
</p>
<p>This solution by Bruno Haible is very clever and smart because
it uses a single buffer; if you don&rsquo;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.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

/[^0-9]/ d

</pre><pre class="example"># replace all trailing 9s by _ (any other character except digits, could
# be used)
:d
s/9\(_*\)$/_\1/
td
</pre><pre class="example">

</pre><pre class="example"># incr last digit only.  The first line adds a most-significant
# digit of 1 if we have to add a digit.
</pre><pre class="example">

</pre><pre class="example">s/^\(_*\)$/1\1/; tn
s/8\(_*\)$/9\1/; tn
s/7\(_*\)$/8\1/; tn
s/6\(_*\)$/7\1/; tn
s/5\(_*\)$/6\1/; tn
s/4\(_*\)$/5\1/; tn
s/3\(_*\)$/4\1/; tn
s/2\(_*\)$/3\1/; tn
s/1\(_*\)$/2\1/; tn
s/0\(_*\)$/1\1/; tn
</pre><pre class="example">

</pre><pre class="example">:n
y/_/0/
</pre></div>

<hr>
</div>
<div class="section" id="Rename-files-to-lower-case">
<div class="header">
<p>
Next: <a href="#Print-bash-environment" accesskey="n" rel="next">Print <code>bash</code> Environment</a>, Previous: <a href="#Increment-a-number" accesskey="p" rel="prev">Increment a Number</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Rename-Files-to-Lower-Case"></span><h3 class="section">7.4 Rename Files to Lower Case</h3>

<p>This is a pretty strange use of <code>sed</code>.  We transform text, and
transform it to be shell commands, then just feed them to shell.
Don&rsquo;t worry, even worse hacks are done when using <code>sed</code>; I have
seen a script converting the output of <code>date</code> into a <code>bc</code>
program!
</p>
<p>The main body of this is the <code>sed</code> script, which remaps the name
from lower to upper (or vice-versa) and even checks out
if the remapped name is the same as the original name.
Note how the script is parameterized using shell
variables and proper quoting.
</p>
<div class="example">
<pre class="example">#! /bin/sh
# rename files to lower/upper case...
#
# usage:
#    move-to-lower *
#    move-to-upper *
# or
#    move-to-lower -R .
#    move-to-upper -R .
#
</pre><pre class="example">

</pre><pre class="example">help()
{
        cat &lt;&lt; eof
Usage: $0 [-n] [-r] [-h] files...
</pre><pre class="example">

</pre><pre class="example">-n      do nothing, only see what would be done
-R      recursive (use find)
-h      this message
files   files to remap to lower case
</pre><pre class="example">

</pre><pre class="example">Examples:
       $0 -n *        (see if everything is ok, then...)
       $0 *
</pre><pre class="example">

       $0 -R .

</pre><pre class="example">eof
}
</pre><pre class="example">

</pre><pre class="example">apply_cmd='sh'
finder='echo &quot;$@&quot; | tr &quot; &quot; &quot;\n&quot;'
files_only=
</pre><pre class="example">

</pre><pre class="example">while :
do
    case &quot;$1&quot; in
        -n) apply_cmd='cat' ;;
        -R) finder='find &quot;$@&quot; -type f';;
        -h) help ; exit 1 ;;
        *) break ;;
    esac
    shift
done
</pre><pre class="example">

</pre><pre class="example">if [ -z &quot;$1&quot; ]; then
        echo Usage: $0 [-h] [-n] [-r] files...
        exit 1
fi
</pre><pre class="example">

</pre><pre class="example">LOWER='abcdefghijklmnopqrstuvwxyz'
UPPER='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
</pre><pre class="example">

</pre><pre class="example">case `basename $0` in
        *upper*) TO=$UPPER; FROM=$LOWER ;;
        *)       FROM=$UPPER; TO=$LOWER ;;
esac
</pre><pre class="example">

eval $finder | sed -n '

</pre><pre class="example"># remove all trailing slashes
s/\/*$//
</pre><pre class="example">

</pre><pre class="example"># add ./ if there is no path, only a filename
/\//! s/^/.\//
</pre><pre class="example">

</pre><pre class="example"># save path+filename
h
</pre><pre class="example">

</pre><pre class="example"># remove path
s/.*\///
</pre><pre class="example">

</pre><pre class="example"># do conversion only on filename
y/'$FROM'/'$TO'/
</pre><pre class="example">

</pre><pre class="example"># now line contains original path+file, while
# hold space contains the new filename
x
</pre><pre class="example">

</pre><pre class="example"># add converted file name to line, which now contains
# path/file-name\nconverted-file-name
G
</pre><pre class="example">

</pre><pre class="example"># check if converted file name is equal to original file name,
# if it is, do not print anything
/^.*\/\(.*\)\n\1/b
</pre><pre class="example">

</pre><pre class="example"># escape special characters for the shell
s/[&quot;$`\\]/\\&amp;/g
</pre><pre class="example">

</pre><pre class="example"># now, transform path/fromfile\n, into
# mv path/fromfile path/tofile and print it
s/^\(.*\/\)\(.*\)\n\(.*\)$/mv &quot;\1\2&quot; &quot;\1\3&quot;/p
</pre><pre class="example">

' | $apply_cmd
</pre></div>

<hr>
</div>
<div class="section" id="Print-bash-environment">
<div class="header">
<p>
Next: <a href="#Reverse-chars-of-lines" accesskey="n" rel="next">Reverse Characters of Lines</a>, Previous: <a href="#Rename-files-to-lower-case" accesskey="p" rel="prev">Rename Files to Lower Case</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Print-bash-Environment"></span><h3 class="section">7.5 Print <code>bash</code> Environment</h3>

<p>This script strips the definition of the shell functions
from the output of the <code>set</code> Bourne-shell command.
</p>
<div class="example">
<pre class="example">#!/bin/sh

</pre><pre class="example">set | sed -n '
:x
</pre><pre class="example">

</pre><pre class="example"># if no occurrence of &lsquo;<samp>=()</samp>&rsquo; print and load next line
/=()/! { p; b; }
/ () $/! { p; b; }
</pre><pre class="example">

</pre><pre class="example"># possible start of functions section
# save the line in case this is a var like FOO=&quot;() &quot;
h
</pre><pre class="example">

</pre><pre class="example"># if the next line has a brace, we quit because
# nothing comes after functions
n
/^{/ q
</pre><pre class="example">

</pre><pre class="example"># print the old line
x; p
</pre><pre class="example">

</pre><pre class="example"># work on the new line now
x; bx
'
</pre></div>

<hr>
</div>
<div class="section" id="Reverse-chars-of-lines">
<div class="header">
<p>
Next: <a href="#Text-search-across-multiple-lines" accesskey="n" rel="next">Text search across multiple lines</a>, Previous: <a href="#Print-bash-environment" accesskey="p" rel="prev">Print <code>bash</code> Environment</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Reverse-Characters-of-Lines"></span><h3 class="section">7.6 Reverse Characters of Lines</h3>

<p>This script can be used to reverse the position of characters
in lines.  The technique moves two characters at a time, hence
it is faster than more intuitive implementations.
</p>
<p>Note the <code>tx</code> command before the definition of the label.
This is often needed to reset the flag that is tested by
the <code>t</code> command.
</p>
<p>Imaginative readers will find uses for this script.  An example
is reversing the output of <code>banner</code>.<a id="DOCF10" href="#FOOT10"><sup>10</sup></a>
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

/../! b

</pre><pre class="example"># Reverse a line.  Begin embedding the line between two newlines
s/^.*$/\
&amp;\
/
</pre><pre class="example">

</pre><pre class="example"># Move first character at the end.  The regexp matches until
# there are zero or one characters between the markers
tx
:x
s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
tx
</pre><pre class="example">

</pre><pre class="example"># Remove the newline markers
s/\n//g
</pre></div>


<hr>
</div>
<div class="section" id="Text-search-across-multiple-lines">
<div class="header">
<p>
Next: <a href="#Line-length-adjustment" accesskey="n" rel="next">Line length adjustment</a>, Previous: <a href="#Reverse-chars-of-lines" accesskey="p" rel="prev">Reverse Characters of Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Text-search-across-multiple-lines-1"></span><h3 class="section">7.7 Text search across multiple lines</h3>

<p>This section uses <code>N</code> and <code>D</code> commands to search for
consecutive words spanning multiple lines. See <a href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a>.
</p>
<p>These examples deal with finding doubled occurrences of words in a document.
</p>
<p>Finding doubled words in a single line is easy using GNU <code>grep</code>
and similarly with GNU <code>sed</code>:
</p>
<div class="example">
<pre class="example">$ cat two-cities-dup1.txt
It was the best of times,
it was the worst of times,
it was the&nbsp;the age of wisdom,
it was the age of foolishness,

$ grep -E '\b(\w+)\s+\1\b' two-cities-dup1.txt
it was the&nbsp;the age of wisdom,

$ grep -n -E '\b(\w+)\s+\1\b' two-cities-dup1.txt
3:it was the&nbsp;the age of wisdom,

$ sed -En '/\b(\w+)\s+\1\b/p' two-cities-dup1.txt
it was the&nbsp;the age of wisdom,

$ sed -En '/\b(\w+)\s+\1\b/{=;p}' two-cities-dup1.txt
3
it was the&nbsp;the age of wisdom,
</pre></div>

<ul>
<li> The regular expression &lsquo;<samp>\b\w+\s+</samp>&rsquo; searches for word-boundary (&lsquo;<samp>\b</samp>&rsquo;),
followed by one-or-more word-characters (&lsquo;<samp>\w+</samp>&rsquo;), followed by whitespace
(&lsquo;<samp>\s+</samp>&rsquo;). See <a href="#regexp-extensions">regular expression extensions</a>.

</li><li> Adding parentheses around the &lsquo;<samp>(\w+)</samp>&rsquo; expression creates a subexpression.
The regular expression pattern &lsquo;<samp>(PATTERN)\s+\1</samp>&rsquo; defines a subexpression
(in the parentheses) followed by a back-reference, separated by whitespace.
A successful match means the <var>PATTERN</var> was repeated twice in succession.
See <a href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a>.

</li><li> The word-boundery expression (&lsquo;<samp>\b</samp>&rsquo;) at both ends ensures partial
words are not matched (e.g. &lsquo;<samp>the then</samp>&rsquo; is not a desired match).

</li><li> The <samp>-E</samp> option enables extended regular expression syntax, alleviating
the need to add backslashes before the parenthesis. See <a href="#ERE-syntax">Overview of extended regular expression syntax</a>.

</li></ul>

<p>When the doubled word span two lines the above regular expression
will not find them as <code>grep</code> and <code>sed</code> operate line-by-line.
</p>
<p>By using <code>N</code> and <code>D</code> commands, <code>sed</code> can apply
regular expressions on multiple lines (that is, multiple lines are stored
in the pattern space, and the regular expression works on it):
</p>
<div class="example">
<pre class="example">$ cat two-cities-dup2.txt
It was the best of times, it was the
worst of times, it was the
the age of wisdom,
it was the age of foolishness,

$ sed -En '{N; /\b(\w+)\s+\1\b/{=;p} ; D}'  two-cities-dup2.txt
3
worst of times, it was the
the age of wisdom,
</pre></div>

<ul>
<li> The <code>N</code> command appends the next line to the pattern space
(thus ensuring it contains two consecutive lines in every cycle).

</li><li> The regular expression uses &lsquo;<samp>\s+</samp>&rsquo; for word separator which matches
both spaces and newlines.

</li><li> The regular expression matches, the entire pattern space is printed
with <code>p</code>. No lines are printed by default due to the <samp>-n</samp> option.

</li><li> The <code>D</code> removes the first line from the pattern space (up until the
first newline), readying it for the next cycle.
</li></ul>

<p>See the GNU <code>coreutils</code> manual for an alternative solution using
<code>tr -s</code> and <code>uniq</code> at
<a href="https://gnu.org/s/coreutils/manual/html_node/Squeezing-and-deleting.html">https://gnu.org/s/coreutils/manual/html_node/Squeezing-and-deleting.html</a>.
</p>
<hr>
</div>
<div class="section" id="Line-length-adjustment">
<div class="header">
<p>
Next: <a href="#Adding-a-header-to-multiple-files" accesskey="n" rel="next">Adding a header to multiple files</a>, Previous: <a href="#Text-search-across-multiple-lines" accesskey="p" rel="prev">Text search across multiple lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Line-length-adjustment-1"></span><h3 class="section">7.8 Line length adjustment</h3>

<p>This section uses <code>N</code> and <code>P</code> commands to read and write
lines, and the <code>b</code> command for branching.
See <a href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a> and <a href="#Branching-and-flow-control">Branching and Flow Control</a>.
</p>
<p>This (somewhat contrived) example deal with formatting and wrapping
lines of text of the following input file:
</p>
<div class="example">
<pre class="example">$ cat two-cities-mix.txt
It was the best of times, it was
the worst of times, it
was the age of
wisdom,
it
was
the age
of foolishness,
</pre></div>

<p>The following sed program wraps lines at 40 characters:
</p><div class="example">
<pre class="example">$ cat wrap40.sed
# outer loop
:x

# Append a newline followed by the next input line to the pattern buffer
N

# Remove all newlines from the pattern buffer
s/\n/ /g


# Inner loop
:y

# Add a newline after the first 40 characters
s/(.{40,40})/\1\n/

# If there is a newline in the pattern buffer
# (i.e. the previous substitution added a newline)
/\n/ {
    # There are newlines in the pattern buffer -
    # print the content until the first newline.
    P

   # Remove the printed characters and the first newline
   s/.*\n//

   # branch to label 'y' - repeat inner loop
   by
 }

# No newlines in the pattern buffer - Branch to label 'x' (outer loop)
# and read the next input line
bx
</pre></div>



<p>The wrapped output:
</p><div class="example">
<pre class="example">$ sed -E -f wrap40.sed two-cities-mix.txt
It was the best of times, it was the wor
st of times, it was the age of wisdom, i
t was the age of foolishness,
</pre></div>




<hr>
</div>
<div class="section" id="Adding-a-header-to-multiple-files">
<div class="header">
<p>
Next: <a href="#tac" accesskey="n" rel="next">Reverse Lines of Files</a>, Previous: <a href="#Line-length-adjustment" accesskey="p" rel="prev">Line length adjustment</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Adding-a-header-to-multiple-files-1"></span><h3 class="section">7.9 Adding a header to multiple files</h3>

<p>GNU <code>sed</code> can be used to safely modify multiple files at once.
</p>
<p>Add a single line to the beginning of source code files:
</p>
<div class="example">
<pre class="example">sed -i '1i/* Copyright (C) FOO BAR */' *.c
</pre></div>

<p>Adding a few lines is possible using &lsquo;<samp>\n</samp>&rsquo; in the text:
</p>
<div class="example">
<pre class="example">sed -i '1i/*\n * Copyright (C) FOO BAR\n * Created by Jane Doe\n */' *.c
</pre></div>

<p>To add multiple lines from another file, use <code>0rFILE</code>.
A typical use case is adding a license notice header to all files:
</p>
<div class="example">
<pre class="example">## Create the header file:
$ cat&lt;&lt;'EOF'&gt;LIC.TXT
/*
    Copyright (C) 1989-2021 FOO BAR

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; If not, see &lt;https://www.gnu.org/licenses/&gt;.
*/
EOF

## Add the file at the beginning of all source code files:
$ sed -i '0rLIC.TXT' *.cpp *.h
</pre></div>


<p>With script files (e.g. <samp>.sh</samp>,<samp>.py</samp>,<samp>.pl</samp> files)
the license notice typically appears <em>after</em> the first line (the
&rsquo;shebang&rsquo; &lsquo;<samp>#!</samp>&rsquo; line). The <code>1rFILE</code> command will add <samp>FILE</samp>
<em>after</em> the first line:
</p>
<div class="example">
<pre class="example">## Create the header file:
$ cat&lt;&lt;'EOF'&gt;LIC.TXT
##
## Copyright (C) 1989-2021 FOO BAR
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see &lt;https://www.gnu.org/licenses/&gt;.
##
##
EOF

## Add the file at the beginning of all source code files:
$ sed -i '1rLIC.TXT' *.py *.sh
</pre></div>

<p>The above <code>sed</code> commands can be combined with <code>find</code>
to locate files in all subdirectories, <code>xargs</code> to run additional
commands on selected files and <code>grep</code> to filter out files that already
contain a copyright notice:
</p>
<div class="example">
<pre class="example">find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) \
    | xargs grep -Li copyright \
    | xargs -r sed -i '0rLIC.TXT'
</pre></div>

<p>Or a slightly safe version (handling files with spaces and newlines):
</p>
<div class="example">
<pre class="example">find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) -print0 \
    | xargs -0 grep -Z -Li copyright \
    | xargs -0 -r sed -i '0rLIC.TXT'
</pre></div>

<p>Note: using the <code>0</code> address with <code>r</code> command requires GNU <code>sed</code>
version 4.9 or later. See <a href="#Zero-Address">Zero Address</a>.
</p>


<hr>
</div>
<div class="section" id="tac">
<div class="header">
<p>
Next: <a href="#cat-_002dn" accesskey="n" rel="next">Numbering Lines</a>, Previous: <a href="#Adding-a-header-to-multiple-files" accesskey="p" rel="prev">Adding a header to multiple files</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Reverse-Lines-of-Files"></span><h3 class="section">7.10 Reverse Lines of Files</h3>

<p>This one begins a series of totally useless (yet interesting)
scripts emulating various Unix commands.  This, in particular,
is a <code>tac</code> workalike.
</p>
<p>Note that on implementations other than GNU <code>sed</code>
this script might easily overflow internal buffers.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

# reverse all lines of input, i.e. first line became last, ...

</pre><pre class="example"># from the second line, the buffer (which contains all previous lines)
# is *appended* to current line, so, the order will be reversed
1! G
</pre><pre class="example">

</pre><pre class="example"># on the last line we're done -- print everything
$ p
</pre><pre class="example">

</pre><pre class="example"># store everything on the buffer again
h
</pre></div>

<hr>
</div>
<div class="section" id="cat-_002dn">
<div class="header">
<p>
Next: <a href="#cat-_002db" accesskey="n" rel="next">Numbering Non-blank Lines</a>, Previous: <a href="#tac" accesskey="p" rel="prev">Reverse Lines of Files</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Numbering-Lines"></span><h3 class="section">7.11 Numbering Lines</h3>

<p>This script replaces &lsquo;<samp>cat -n</samp>&rsquo;; in fact it formats its output
exactly like GNU <code>cat</code> does.
</p>
<p>Of course this is completely useless and for two reasons:  first,
because somebody else did it in C, second, because the following
Bourne-shell script could be used for the same purpose and would
be much faster:
</p>
<div class="example">
<pre class="example">#! /bin/sh
sed -e &quot;=&quot; $@ | sed -e '
  s/^/      /
  N
  s/^ *\(......\)\n/\1  /
'
</pre></div>

<p>It uses <code>sed</code> to print the line number, then groups lines two
by two using <code>N</code>.  Of course, this script does not teach as much as
the one presented below.
</p>
<p>The algorithm used for incrementing uses both buffers, so the line
is printed as soon as possible and then discarded.  The number
is split so that changing digits go in a buffer and unchanged ones go
in the other; the changed digits are modified in a single step
(using a <code>y</code> command).  The line number for the next line
is then composed and stored in the hold space, to be used in the
next iteration.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example"># Prime the pump on the first line
x
/^$/ s/^.*$/1/
</pre><pre class="example">

</pre><pre class="example"># Add the correct line number before the pattern
G
h
</pre><pre class="example">

</pre><pre class="example"># Format it and print it
s/^/      /
s/^ *\(......\)\n/\1  /p
</pre><pre class="example">

</pre><pre class="example"># Get the line number from hold space; add a zero
# if we're going to add a digit on the next line
g
s/\n.*$//
/^9*$/ s/^/0/
</pre><pre class="example">

</pre><pre class="example"># separate changing/unchanged digits with an x
s/.9*$/x&amp;/
</pre><pre class="example">

</pre><pre class="example"># keep changing digits in hold space
h
s/^.*x//
y/0123456789/1234567890/
x
</pre><pre class="example">

</pre><pre class="example"># keep unchanged digits in pattern space
s/x.*$//
</pre><pre class="example">

</pre><pre class="example"># compose the new number, remove the newline implicitly added by G
G
s/\n//
h
</pre></div>

<hr>
</div>
<div class="section" id="cat-_002db">
<div class="header">
<p>
Next: <a href="#wc-_002dc" accesskey="n" rel="next">Counting Characters</a>, Previous: <a href="#cat-_002dn" accesskey="p" rel="prev">Numbering Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Numbering-Non_002dblank-Lines"></span><h3 class="section">7.12 Numbering Non-blank Lines</h3>

<p>Emulating &lsquo;<samp>cat -b</samp>&rsquo; is almost the same as &lsquo;<samp>cat -n</samp>&rsquo;&mdash;we only
have to select which lines are to be numbered and which are not.
</p>
<p>The part that is common to this script and the previous one is
not commented to show how important it is to comment <code>sed</code>
scripts properly...
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example">/^$/ {
  p
  b
}
</pre><pre class="example">

</pre><pre class="example"># Same as cat -n from now
x
/^$/ s/^.*$/1/
G
h
s/^/      /
s/^ *\(......\)\n/\1  /p
x
s/\n.*$//
/^9*$/ s/^/0/
s/.9*$/x&amp;/
h
s/^.*x//
y/0123456789/1234567890/
x
s/x.*$//
G
s/\n//
h
</pre></div>

<hr>
</div>
<div class="section" id="wc-_002dc">
<div class="header">
<p>
Next: <a href="#wc-_002dw" accesskey="n" rel="next">Counting Words</a>, Previous: <a href="#cat-_002db" accesskey="p" rel="prev">Numbering Non-blank Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Counting-Characters"></span><h3 class="section">7.13 Counting Characters</h3>

<p>This script shows another way to do arithmetic with <code>sed</code>.
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>
<p>The approach is to map numbers to letters, kind of an abacus
implemented with <code>sed</code>.  &lsquo;<samp>a</samp>&rsquo;s are units, &lsquo;<samp>b</samp>&rsquo;s are
tens and so on: we simply add the number of characters
on the current line as units, and then propagate the carry
to tens, hundreds, and so on.
</p>
<p>As usual, running totals are kept in hold space.
</p>
<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 id="DOCF11" href="#FOOT11"><sup>11</sup></a>: first we
convert units, removing &lsquo;<samp>a</samp>&rsquo;s from the number; then we
rotate letters so that tens become &lsquo;<samp>a</samp>&rsquo;s, and so on
until no more letters remain.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example"># Add n+1 a's to hold space (+1 is for the newline)
s/./a/g
H
x
s/\n/a/
</pre><pre class="example">

</pre><pre class="example"># Do the carry.  The t's and b's are not necessary,
# but they do speed up the thing
t a
: a;  s/aaaaaaaaaa/b/g; t b; b done
: b;  s/bbbbbbbbbb/c/g; t c; b done
: c;  s/cccccccccc/d/g; t d; b done
: d;  s/dddddddddd/e/g; t e; b done
: e;  s/eeeeeeeeee/f/g; t f; b done
: f;  s/ffffffffff/g/g; t g; b done
: g;  s/gggggggggg/h/g; t h; b done
: h;  s/hhhhhhhhhh//g
</pre><pre class="example">

</pre><pre class="example">: done
$! {
  h
  b
}
</pre><pre class="example">

# On the last line, convert back to decimal

</pre><pre class="example">: loop
/a/! s/[b-h]*/&amp;0/
s/aaaaaaaaa/9/
s/aaaaaaaa/8/
s/aaaaaaa/7/
s/aaaaaa/6/
s/aaaaa/5/
s/aaaa/4/
s/aaa/3/
s/aa/2/
s/a/1/
</pre><pre class="example">

</pre><pre class="example">: next
y/bcdefgh/abcdefg/
/[a-h]/ b loop
p
</pre></div>

<hr>
</div>
<div class="section" id="wc-_002dw">
<div class="header">
<p>
Next: <a href="#wc-_002dl" accesskey="n" rel="next">Counting Lines</a>, Previous: <a href="#wc-_002dc" accesskey="p" rel="prev">Counting Characters</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Counting-Words"></span><h3 class="section">7.14 Counting Words</h3>

<p>This script is almost the same as the previous one, once each
of the words on the line is converted to a single &lsquo;<samp>a</samp>&rsquo;
(in the previous script each letter was changed to an &lsquo;<samp>a</samp>&rsquo;).
</p>
<p>It is interesting that real <code>wc</code> programs have optimized
loops for &lsquo;<samp>wc -c</samp>&rsquo;, so they are much slower at counting
words rather than characters.  This script&rsquo;s bottleneck,
instead, is arithmetic, and hence the word-counting one
is faster (it has to manage smaller numbers).
</p>
<p>Again, the common parts are not commented to show the importance
of commenting <code>sed</code> scripts.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example"># Convert words to a's
s/[ <kbd><span class="key">TAB</span></kbd>][ <kbd><span class="key">TAB</span></kbd>]*/ /g
s/^/ /
s/ [^ ][^ ]*/a /g
s/ //g
</pre><pre class="example">

</pre><pre class="example"># Append them to hold space
H
x
s/\n//
</pre><pre class="example">

</pre><pre class="example"># From here on it is the same as in wc -c.
/aaaaaaaaaa/! bx;   s/aaaaaaaaaa/b/g
/bbbbbbbbbb/! bx;   s/bbbbbbbbbb/c/g
/cccccccccc/! bx;   s/cccccccccc/d/g
/dddddddddd/! bx;   s/dddddddddd/e/g
/eeeeeeeeee/! bx;   s/eeeeeeeeee/f/g
/ffffffffff/! bx;   s/ffffffffff/g/g
/gggggggggg/! bx;   s/gggggggggg/h/g
s/hhhhhhhhhh//g
:x
$! { h; b; }
:y
/a/! s/[b-h]*/&amp;0/
s/aaaaaaaaa/9/
s/aaaaaaaa/8/
s/aaaaaaa/7/
s/aaaaaa/6/
s/aaaaa/5/
s/aaaa/4/
s/aaa/3/
s/aa/2/
s/a/1/
y/bcdefgh/abcdefg/
/[a-h]/ by
p
</pre></div>

<hr>
</div>
<div class="section" id="wc-_002dl">
<div class="header">
<p>
Next: <a href="#head" accesskey="n" rel="next">Printing the First Lines</a>, Previous: <a href="#wc-_002dw" accesskey="p" rel="prev">Counting Words</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Counting-Lines"></span><h3 class="section">7.15 Counting Lines</h3>

<p>No strange things are done now, because <code>sed</code> gives us
&lsquo;<samp>wc -l</samp>&rsquo; functionality for free!!! Look:
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf
$=
</pre></div>

<hr>
</div>
<div class="section" id="head">
<div class="header">
<p>
Next: <a href="#tail" accesskey="n" rel="next">Printing the Last Lines</a>, Previous: <a href="#wc-_002dl" accesskey="p" rel="prev">Counting Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Printing-the-First-Lines"></span><h3 class="section">7.16 Printing the First Lines</h3>

<p>This script is probably the simplest useful <code>sed</code> script.
It displays the first 10 lines of input; the number of displayed
lines is right before the <code>q</code> command.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f
10q
</pre></div>

<hr>
</div>
<div class="section" id="tail">
<div class="header">
<p>
Next: <a href="#uniq" accesskey="n" rel="next">Make Duplicate Lines Unique</a>, Previous: <a href="#head" accesskey="p" rel="prev">Printing the First Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Printing-the-Last-Lines"></span><h3 class="section">7.17 Printing the Last Lines</h3>

<p>Printing the last <var>n</var> lines rather than the first is more complex
but indeed possible.  <var>n</var> is encoded in the second line, before
the bang character.
</p>
<p>This script is similar to the <code>tac</code> script in that it keeps the
final output in the hold space and prints it at the end:
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example">1! {; H; g; }
1,10 !s/[^\n]*\n//
$p
h
</pre></div>

<p>Mainly, the scripts keeps a window of 10 lines and slides it
by adding a line and deleting the oldest (the substitution command
on the second line works like a <code>D</code> command but does not
restart the loop).
</p>
<p>The &ldquo;sliding window&rdquo; technique is a very powerful way to write
efficient and complex <code>sed</code> scripts, because commands like
<code>P</code> would require a lot of work if implemented manually.
</p>
<p>To introduce the technique, which is fully demonstrated in the
rest of this chapter and is based on the <code>N</code>, <code>P</code>
and <code>D</code> commands, here is an implementation of <code>tail</code>
using a simple &ldquo;sliding window.&rdquo;
</p>
<p>This looks complicated but in fact the working is the same as
the last script: after we have kicked in the appropriate number
of lines, however, we stop using the hold space to keep inter-line
state, and instead use <code>N</code> and <code>D</code> to slide pattern
space by one line:
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

</pre><pre class="example">1h
2,10 {; H; g; }
$q
1,9d
N
D
</pre></div>

<p>Note how the first, second and fourth line are inactive after
the first ten lines of input.  After that, all the script does
is: exiting on the last line of input, appending the next input
line to pattern space, and removing the first line.
</p>
<hr>
</div>
<div class="section" id="uniq">
<div class="header">
<p>
Next: <a href="#uniq-_002dd" accesskey="n" rel="next">Print Duplicated Lines of Input</a>, Previous: <a href="#tail" accesskey="p" rel="prev">Printing the Last Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Make-Duplicate-Lines-Unique"></span><h3 class="section">7.18 Make Duplicate Lines Unique</h3>

<p>This is an example of the art of using the <code>N</code>, <code>P</code>
and <code>D</code> commands, probably the most difficult to master.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f
h
</pre><pre class="example">

</pre><pre class="example">:b
# On the last line, print and exit
$b
N
/^\(.*\)\n\1$/ {
    # The two lines are identical.  Undo the effect of
    # the n command.
    g
    bb
}
</pre><pre class="example">

</pre><pre class="example"># If the <code>N</code> command had added the last line, print and exit
$b
</pre><pre class="example">

</pre><pre class="example"># The lines are different; print the first and go
# back working on the second.
P
D
</pre></div>

<p>As you can see, we maintain a 2-line window using <code>P</code> and <code>D</code>.
This technique is often used in advanced <code>sed</code> scripts.
</p>
<hr>
</div>
<div class="section" id="uniq-_002dd">
<div class="header">
<p>
Next: <a href="#uniq-_002du" accesskey="n" rel="next">Remove All Duplicated Lines</a>, Previous: <a href="#uniq" accesskey="p" rel="prev">Make Duplicate Lines Unique</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Print-Duplicated-Lines-of-Input"></span><h3 class="section">7.19 Print Duplicated Lines of Input</h3>

<p>This script prints only duplicated lines, like &lsquo;<samp>uniq -d</samp>&rsquo;.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example">$b
N
/^\(.*\)\n\1$/ {
    # Print the first of the duplicated lines
    s/.*\n//
    p
</pre><pre class="example">

</pre><pre class="example">    # Loop until we get a different line
    :b
    $b
    N
    /^\(.*\)\n\1$/ {
        s/.*\n//
        bb
    }
}
</pre><pre class="example">

</pre><pre class="example"># The last line cannot be followed by duplicates
$b
</pre><pre class="example">

</pre><pre class="example"># Found a different one.  Leave it alone in the pattern space
# and go back to the top, hunting its duplicates
D
</pre></div>

<hr>
</div>
<div class="section" id="uniq-_002du">
<div class="header">
<p>
Next: <a href="#cat-_002ds" accesskey="n" rel="next">Squeezing Blank Lines</a>, Previous: <a href="#uniq-_002dd" accesskey="p" rel="prev">Print Duplicated Lines of Input</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Remove-All-Duplicated-Lines"></span><h3 class="section">7.20 Remove All Duplicated Lines</h3>

<p>This script prints only unique lines, like &lsquo;<samp>uniq -u</samp>&rsquo;.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

</pre><pre class="example"># Search for a duplicate line --- until that, print what you find.
$b
N
/^\(.*\)\n\1$/ ! {
    P
    D
}
</pre><pre class="example">

</pre><pre class="example">:c
# Got two equal lines in pattern space.  At the
# end of the file we simply exit
$d
</pre><pre class="example">

</pre><pre class="example"># Else, we keep reading lines with <code>N</code> until we
# find a different one
s/.*\n//
N
/^\(.*\)\n\1$/ {
    bc
}
</pre><pre class="example">

</pre><pre class="example"># Remove the last instance of the duplicate line
# and go back to the top
D
</pre></div>

<hr>
</div>
<div class="section" id="cat-_002ds">
<div class="header">
<p>
Previous: <a href="#uniq-_002du" accesskey="p" rel="prev">Remove All Duplicated Lines</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Squeezing-Blank-Lines"></span><h3 class="section">7.21 Squeezing Blank Lines</h3>

<p>As a final example, here are three scripts, of increasing complexity
and speed, that implement the same function as &lsquo;<samp>cat -s</samp>&rsquo;, that is
squeezing blank lines.
</p>
<p>The first leaves a blank line at the beginning and end if there are
some already.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

</pre><pre class="example"># on empty lines, join with next
# Note there is a star in the regexp
:x
/^\n*$/ {
N
bx
}
</pre><pre class="example">

</pre><pre class="example"># now, squeeze all '\n', this can be also done by:
# s/^\(\n\)*/\1/
s/\n*/\
/
</pre></div>

<p>This one is a bit more complex and removes all empty lines
at the beginning.  It does leave a single blank line at end
if one was there.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -f

</pre><pre class="example"># delete all leading empty lines
1,/^./{
/./!d
}
</pre><pre class="example">

</pre><pre class="example"># on an empty line we remove it and all the following
# empty lines, but one
:x
/./!{
N
s/^\n$//
tx
}
</pre></div>

<p>This removes leading and trailing blank lines.  It is also the
fastest.  Note that loops are completely done with <code>n</code> and
<code>b</code>, without relying on <code>sed</code> to restart the
script automatically at the end of a line.
</p>
<div class="example">
<pre class="example">#!/usr/bin/sed -nf

</pre><pre class="example"># delete all (leading) blanks
/./!d
</pre><pre class="example">

</pre><pre class="example"># get here: so there is a non empty
:x
# print it
p
# get next
n
# got chars? print it again, etc...
/./bx
</pre><pre class="example">

</pre><pre class="example"># no, don't have chars: got an empty line
:z
# get next, if last line we finish here so no trailing
# empty lines are written
n
# also empty? then ignore it, and get next... this will
# remove ALL empty lines
/./!bz
</pre><pre class="example">

</pre><pre class="example"># all empty lines were deleted/ignored, but we have a non empty.  As
# what we want to do is to squeeze, insert a blank line artificially
i\
</pre><pre class="example">

bx
</pre></div>

<hr>
</div>
</div>
<div class="chapter" id="Limitations">
<div class="header">
<p>
Next: <a href="#Other-Resources" accesskey="n" rel="next">Other Resources for Learning About <code>sed</code></a>, Previous: <a href="#Examples" accesskey="p" rel="prev">Some Sample Scripts</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="GNU-sed_0027s-Limitations-and-Non_002dlimitations"></span><h2 class="chapter">8 GNU <code>sed</code>&rsquo;s Limitations and Non-limitations</h2>

<span id="index-GNU-extensions_002c-unlimited-line-length"></span>
<span id="index-Portability_002c-line-length-limitations"></span>
<p>For those who want to write portable <code>sed</code> 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 <small>POSIX</small> standard specifies that conforming <code>sed</code>
implementations shall support at least 8192 byte line lengths.
GNU <code>sed</code> 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>
<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.
</p>

<hr>
</div>
<div class="chapter" id="Other-Resources">
<div class="header">
<p>
Next: <a href="#Reporting-Bugs" accesskey="n" rel="next">Reporting Bugs</a>, Previous: <a href="#Limitations" accesskey="p" rel="prev">GNU <code>sed</code>&rsquo;s Limitations and Non-limitations</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Other-Resources-for-Learning-About-sed"></span><h2 class="chapter">9 Other Resources for Learning About <code>sed</code></h2>

<p>For up to date information about GNU <code>sed</code> please
visit <a href="https://www.gnu.org/software/sed/">https://www.gnu.org/software/sed/</a>.
</p>
<p>Send general questions and suggestions to <a href="mailto:sed-devel@gnu.org">sed-devel@gnu.org</a>.
Visit the mailing list archives for past discussions at
<a href="https://lists.gnu.org/archive/html/sed-devel/">https://lists.gnu.org/archive/html/sed-devel/</a>.
</p>
<span id="index-Additional-reading-about-sed"></span>
<p>The following resources provide information about <code>sed</code>
(both GNU <code>sed</code> and other variations). Note these not maintained by
GNU <code>sed</code> developers.
</p>
<ul>
<li> sed <code>$HOME</code>: <a href="http://sed.sf.net">http://sed.sf.net</a>

</li><li> sed FAQ: <a href="http://sed.sf.net/sedfaq.html">http://sed.sf.net/sedfaq.html</a>

</li><li> seder&rsquo;s grabbag: <a href="http://sed.sf.net/grabbag">http://sed.sf.net/grabbag</a>

</li><li> The <code>sed-users</code> mailing list maintained by Sven Guckes:
<a href="http://groups.yahoo.com/group/sed-users/">http://groups.yahoo.com/group/sed-users/</a>
(note this is <em>not</em> the GNU <code>sed</code> mailing list).

</li></ul>

<hr>
</div>
<div class="chapter" id="Reporting-Bugs">
<div class="header">
<p>
Next: <a href="#GNU-Free-Documentation-License" accesskey="n" rel="next">GNU Free Documentation License</a>, Previous: <a href="#Other-Resources" accesskey="p" rel="prev">Other Resources for Learning About <code>sed</code></a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Reporting-Bugs-1"></span><h2 class="chapter">10 Reporting Bugs</h2>

<span id="index-Bugs_002c-reporting"></span>
<p>Email bug reports to <a href="mailto:bug-sed@gnu.org">bug-sed@gnu.org</a>.
Also, please include the output of &lsquo;<samp>sed --version</samp>&rsquo; in the body
of your report if at all possible.
</p>
<p>Please do not send a bug report like this:
</p>
<div class="example">
<pre class="example"><i><i><span class="roman">while building frobme-1.3.4</span></i></i>
$ configure
error&rarr; sed: file sedscr line 1: Unknown option to 's'
</pre></div>

<p>If GNU <code>sed</code> doesn&rsquo;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 <code>sed</code> is quite simple.
</p>
<p>A stand-alone test case includes all the data necessary to perform the
test, and the specific invocation of <code>sed</code> 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 <code>sed</code> as &ldquo;try to configure
frobme-1.3.4&rdquo;.  Yes, that is in principle enough information to look
for the bug, but that is not a very practical prospect.
</p>
<p>Here are a few commonly reported bugs that are not bugs.
</p>
<dl compact="compact">
<dd><span id="N_005fcommand_005flast_005fline"></span></dd>
<dt id='index-Portability_002c-N-command-on-the-last-line'><span><code>N</code> command on the last line<a href='#index-Portability_002c-N-command-on-the-last-line' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Non_002dbugs_002c-N-command-on-the-last-line"></span>

<p>Most versions of <code>sed</code> exit without printing anything when
the <code>N</code> command is issued on the last line of a file.
GNU <code>sed</code> prints pattern space before exiting unless of course
the <code>-n</code> command switch has been specified.  This choice is
by design.
</p>
<p>Default behavior (gnu extension, non-POSIX conforming):
</p><div class="example">
<pre class="example">$ seq 3 | sed N
1
2
3
</pre></div>
<p>To force POSIX-conforming behavior:
</p><div class="example">
<pre class="example">$ seq 3 | sed --posix N
1
2
</pre></div>

<p>For example, the behavior of
</p><div class="example">
<pre class="example">sed N foo bar
</pre></div>
<p>would depend on whether foo has an even or an odd number of
lines<a id="DOCF12" href="#FOOT12"><sup>12</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
something like
</p><div class="example">
<pre class="example">/foo/{ $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N }
</pre></div>
<p>instead of just
</p><div class="example">
<pre class="example">/foo/{ N;N;N;N;N;N;N;N;N; }
</pre></div>

<span id="index-POSIXLY_005fCORRECT-behavior_002c-N-command"></span>
<p>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.
</p>
</dd>
<dt id='index-GNU-extensions_002c-to-basic-regular-expressions-5'><span>Regex syntax clashes (problems with backslashes)<a href='#index-GNU-extensions_002c-to-basic-regular-expressions-5' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-Non_002dbugs_002c-regex-syntax-clashes"></span>
<p><code>sed</code> uses the <small>POSIX</small> basic regular expression syntax.  According to
the standard, the meaning of some escape sequences is undefined in
this syntax;  notable in the case of <code>sed</code> are <code>\|</code>,
<code>\+</code>, <code>\?</code>, <code>\`</code>, <code>\'</code>, <code>\&lt;</code>,
<code>\&gt;</code>, <code>\b</code>, <code>\B</code>, <code>\w</code>, and <code>\W</code>.
</p>
<p>As in all GNU programs that use <small>POSIX</small> basic regular
expressions, <code>sed</code> interprets these escape sequences as special
characters.  So, <code>x\+</code> matches one or more occurrences of &lsquo;<samp>x</samp>&rsquo;.
<code>abc\|def</code> matches either &lsquo;<samp>abc</samp>&rsquo; or &lsquo;<samp>def</samp>&rsquo;.
</p>
<p>This syntax may cause problems when running scripts written for other
<code>sed</code>s.  Some <code>sed</code> programs have been written with the
assumption that <code>\|</code> and <code>\+</code> match the literal characters
<code>|</code> and <code>+</code>.  Such scripts must be modified by removing the
spurious backslashes if they are to be used with modern implementations
of <code>sed</code>, like
GNU <code>sed</code>.
</p>
<p>On the other hand, some scripts use s|abc\|def||g to remove occurrences
of <em>either</em> <code>abc</code> or <code>def</code>.  While this worked until
<code>sed</code> 4.0.x, newer versions interpret this as removing the
string <code>abc|def</code>.  This is again undefined behavior according to
POSIX, and this interpretation is arguably more robust: older
<code>sed</code>s, for example, required that the regex matcher parsed
<code>\/</code> as <code>/</code> in the common case of escaping a slash, which is
again undefined behavior; the new behavior avoids this, and this is good
because the regex matcher is only partially under our control.
</p>
<span id="index-GNU-extensions_002c-special-escapes-1"></span>
<p>In addition, this version of <code>sed</code> 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 <code>sed</code>s.
</p>
</dd>
<dt id='index-In_002dplace-editing'><span><samp>-i</samp> clobbers read-only files<a href='#index-In_002dplace-editing' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-in_002dplace-editing-1"></span>
<span id="index-Non_002dbugs_002c-in_002dplace-editing"></span>

<p>In short, &lsquo;<samp>sed -i</samp>&rsquo; will let you delete the contents of
a read-only file, and in general the <samp>-i</samp> option
(see <a href="#Invoking-sed">Invocation</a>) lets you clobber
protected files.  This is not a bug, but rather a consequence
of how the Unix file system works.
</p>
<p>The permissions on a file say what can happen to the data
in that file, while the permissions on a directory say what can
happen to the list of files in that directory.  &lsquo;<samp>sed -i</samp>&rsquo;
will not ever open for writing  a file that is already on disk.
Rather, it will work on a temporary file that is finally renamed
to the original name: if you rename or delete files, you&rsquo;re actually
modifying the contents of the directory, so the operation depends on
the permissions of the directory, not of the file.  For this same
reason, <code>sed</code> does not let you use <samp>-i</samp> on a writable file
in a read-only directory, and will break hard or symbolic links when
<samp>-i</samp> is used on such a file.
</p>
</dd>
<dt id='index-0-address'><span><code>0a</code> does not work (gives an error)<a href='#index-0-address' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-0-address-1"></span>
<span id="index-Non_002dbugs_002c-0-address"></span>

<p>There is no line 0.  0 is a special address that is only used to treat
addresses like <code>0,/<var>RE</var>/</code> as active when the script starts: if
you write <code>1,/abc/d</code> and the first line includes the string &lsquo;<samp>abc</samp>&rsquo;,
then that match would be ignored because address ranges must span at least
two lines (barring the end of the file); but what you probably wanted is
to delete every line up to the first one including &lsquo;<samp>abc</samp>&rsquo;, and this
is obtained with <code>0,/abc/d</code>.
</p>
</dd>
<dt id='index-Non_002dbugs_002c-localization_002drelated'><span><code>[a-z]</code> is case insensitive<a href='#index-Non_002dbugs_002c-localization_002drelated' class='copiable-anchor'> &para;</a></span></dt>
<dd>
<p>You are encountering problems with locales.  POSIX mandates that <code>[a-z]</code>
uses the current locale&rsquo;s collation order &ndash; in C parlance, that means using
<code>strcoll(3)</code> instead of <code>strcmp(3)</code>.  Some locales have a
case-insensitive collation order, others don&rsquo;t.
</p>
<p>Another problem is that <code>[a-z]</code> tries to use collation symbols.
This only happens if you are on the GNU system, using
GNU libc&rsquo;s regular expression matcher instead of compiling the
one supplied with GNU sed.  In a Danish locale, for example,
the regular expression <code>^[a-z]$</code> matches the string &lsquo;<samp>aa</samp>&rsquo;,
because this is a single collating symbol that comes after &lsquo;<samp>a</samp>&rsquo;
and before &lsquo;<samp>b</samp>&rsquo;; &lsquo;<samp>ll</samp>&rsquo; behaves similarly in Spanish
locales, or &lsquo;<samp>ij</samp>&rsquo; in Dutch locales.
</p>
<p>To work around these problems, which may cause bugs in shell scripts, set
the <code>LC_COLLATE</code> and <code>LC_CTYPE</code> environment variables to &lsquo;<samp>C</samp>&rsquo;.
</p>
</dd>
<dt id='index-Non_002dbugs_002c-localization_002drelated-1'><span><code>s/.*//</code> does not clear pattern space<a href='#index-Non_002dbugs_002c-localization_002drelated-1' class='copiable-anchor'> &para;</a></span></dt>
<dd><span id="index-GNU-extensions_002c-emptying-pattern-space-1"></span>
<span id="index-Emptying-pattern-space-1"></span>

<p>This happens if your input stream includes invalid multibyte
sequences.  <small>POSIX</small> mandates that such sequences
are <em>not</em> matched by &lsquo;<samp>.</samp>&rsquo;, so that &lsquo;<samp>s/.*//</samp>&rsquo; will not clear
pattern space as you would expect.  In fact, there is no way to clear
sed&rsquo;s buffers in the middle of the script in most multibyte locales
(including UTF-8 locales).  For this reason, GNU <code>sed</code> provides a &lsquo;z&rsquo;
command (for &lsquo;zap&rsquo;) as an extension.
</p>
<p>To work around these problems, which may cause bugs in shell scripts, set
the <code>LC_COLLATE</code> and <code>LC_CTYPE</code> environment variables to &lsquo;<samp>C</samp>&rsquo;.
</p></dd>
</dl>




<hr>
</div>
<div class="appendix" id="GNU-Free-Documentation-License">
<div class="header">
<p>
Next: <a href="#Concept-Index" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="#Reporting-Bugs" accesskey="p" rel="prev">Reporting Bugs</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="GNU-Free-Documentation-License-1"></span><h2 class="appendix">Appendix A GNU Free Documentation License</h2>

<div align="center">Version 1.3, 3 November 2008
</div>

<div class="display">
<pre class="display">Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.
<a href="https://fsf.org/">https://fsf.org/</a>

Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
</pre></div>

<ol start="0">
<li> PREAMBLE

<p>The purpose of this License is to make a manual, textbook, or other
functional and useful document <em>free</em> in the sense of freedom: to
assure everyone the effective freedom to copy and redistribute it,
with or without modifying it, either commercially or noncommercially.
Secondarily, this License preserves for the author and publisher a way
to get credit for their work, while not being considered responsible
for modifications made by others.
</p>
<p>This License is a kind of &ldquo;copyleft&rdquo;, which means that derivative
works of the document must themselves be free in the same sense.  It
complements the GNU General Public License, which is a copyleft
license designed for free software.
</p>
<p>We have designed this License in order to use it for manuals for free
software, because free software needs free documentation: a free
program should come with manuals providing the same freedoms that the
software does.  But this License is not limited to software manuals;
it can be used for any textual work, regardless of subject matter or
whether it is published as a printed book.  We recommend this License
principally for works whose purpose is instruction or reference.
</p>
</li><li> APPLICABILITY AND DEFINITIONS

<p>This License applies to any manual or other work, in any medium, that
contains a notice placed by the copyright holder saying it can be
distributed under the terms of this License.  Such a notice grants a
world-wide, royalty-free license, unlimited in duration, to use that
work under the conditions stated herein.  The &ldquo;Document&rdquo;, below,
refers to any such manual or work.  Any member of the public is a
licensee, and is addressed as &ldquo;you&rdquo;.  You accept the license if you
copy, modify or distribute the work in a way requiring permission
under copyright law.
</p>
<p>A &ldquo;Modified Version&rdquo; of the Document means any work containing the
Document or a portion of it, either copied verbatim, or with
modifications and/or translated into another language.
</p>
<p>A &ldquo;Secondary Section&rdquo; is a named appendix or a front-matter section
of the Document that deals exclusively with the relationship of the
publishers or authors of the Document to the Document&rsquo;s overall
subject (or to related matters) and contains nothing that could fall
directly within that overall subject.  (Thus, if the Document is in
part a textbook of mathematics, a Secondary Section may not explain
any mathematics.)  The relationship could be a matter of historical
connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding
them.
</p>
<p>The &ldquo;Invariant Sections&rdquo; are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice
that says that the Document is released under this License.  If a
section does not fit the above definition of Secondary then it is not
allowed to be designated as Invariant.  The Document may contain zero
Invariant Sections.  If the Document does not identify any Invariant
Sections then there are none.
</p>
<p>The &ldquo;Cover Texts&rdquo; are certain short passages of text that are listed,
as Front-Cover Texts or Back-Cover Texts, in the notice that says that
the Document is released under this License.  A Front-Cover Text may
be at most 5 words, and a Back-Cover Text may be at most 25 words.
</p>
<p>A &ldquo;Transparent&rdquo; copy of the Document means a machine-readable copy,
represented in a format whose specification is available to the
general public, that is suitable for revising the document
straightforwardly with generic text editors or (for images composed of
pixels) generic paint programs or (for drawings) some widely available
drawing editor, and that is suitable for input to text formatters or
for automatic translation to a variety of formats suitable for input
to text formatters.  A copy made in an otherwise Transparent file
format whose markup, or absence of markup, has been arranged to thwart
or discourage subsequent modification by readers is not Transparent.
An image format is not Transparent if used for any substantial amount
of text.  A copy that is not &ldquo;Transparent&rdquo; is called &ldquo;Opaque&rdquo;.
</p>
<p>Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input
format, SGML or XML using a publicly available
DTD, and standard-conforming simple HTML,
PostScript or PDF designed for human modification.  Examples
of transparent image formats include PNG, XCF and
JPG.  Opaque formats include proprietary formats that can be
read and edited only by proprietary word processors, SGML or
XML for which the DTD and/or processing tools are
not generally available, and the machine-generated HTML,
PostScript or PDF produced by some word processors for
output purposes only.
</p>
<p>The &ldquo;Title Page&rdquo; means, for a printed book, the title page itself,
plus such following pages as are needed to hold, legibly, the material
this License requires to appear in the title page.  For works in
formats which do not have any title page as such, &ldquo;Title Page&rdquo; means
the text near the most prominent appearance of the work&rsquo;s title,
preceding the beginning of the body of the text.
</p>
<p>The &ldquo;publisher&rdquo; means any person or entity that distributes copies
of the Document to the public.
</p>
<p>A section &ldquo;Entitled XYZ&rdquo; means a named subunit of the Document whose
title either is precisely XYZ or contains XYZ in parentheses following
text that translates XYZ in another language.  (Here XYZ stands for a
specific section name mentioned below, such as &ldquo;Acknowledgements&rdquo;,
&ldquo;Dedications&rdquo;, &ldquo;Endorsements&rdquo;, or &ldquo;History&rdquo;.)  To &ldquo;Preserve the Title&rdquo;
of such a section when you modify the Document means that it remains a
section &ldquo;Entitled XYZ&rdquo; according to this definition.
</p>
<p>The Document may include Warranty Disclaimers next to the notice which
states that this License applies to the Document.  These Warranty
Disclaimers are considered to be included by reference in this
License, but only as regards disclaiming warranties: any other
implication that these Warranty Disclaimers may have is void and has
no effect on the meaning of this License.
</p>
</li><li> VERBATIM COPYING

<p>You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no other
conditions whatsoever to those of this License.  You may not use
technical measures to obstruct or control the reading or further
copying of the copies you make or distribute.  However, you may accept
compensation in exchange for copies.  If you distribute a large enough
number of copies you must also follow the conditions in section 3.
</p>
<p>You may also lend copies, under the same conditions stated above, and
you may publicly display copies.
</p>
</li><li> COPYING IN QUANTITY

<p>If you publish printed copies (or copies in media that commonly have
printed covers) of the Document, numbering more than 100, and the
Document&rsquo;s license notice requires Cover Texts, you must enclose the
copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover.  Both covers must also clearly and legibly identify
you as the publisher of these copies.  The front cover must present
the full title with all words of the title equally prominent and
visible.  You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.
</p>
<p>If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.
</p>
<p>If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable Transparent
copy along with each Opaque copy, or state in or with each Opaque copy
a computer-network location from which the general network-using
public has access to download using public-standard network protocols
a complete Transparent copy of the Document, free of added material.
If you use the latter option, you must take reasonably prudent steps,
when you begin distribution of Opaque copies in quantity, to ensure
that this Transparent copy will remain thus accessible at the stated
location until at least one year after the last time you distribute an
Opaque copy (directly or through your agents or retailers) of that
edition to the public.
</p>
<p>It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to give
them a chance to provide you with an updated version of the Document.
</p>
</li><li> MODIFICATIONS

<p>You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it.  In addition, you must do these things in the Modified Version:
</p>
<ol type="A" start="1">
<li> Use in the Title Page (and on the covers, if any) a title distinct
from that of the Document, and from those of previous versions
(which should, if there were any, be listed in the History section
of the Document).  You may use the same title as a previous version
if the original publisher of that version gives permission.

</li><li> List on the Title Page, as authors, one or more persons or entities
responsible for authorship of the modifications in the Modified
Version, together with at least five of the principal authors of the
Document (all of its principal authors, if it has fewer than five),
unless they release you from this requirement.

</li><li> State on the Title page the name of the publisher of the
Modified Version, as the publisher.

</li><li> Preserve all the copyright notices of the Document.

</li><li> Add an appropriate copyright notice for your modifications
adjacent to the other copyright notices.

</li><li> Include, immediately after the copyright notices, a license notice
giving the public permission to use the Modified Version under the
terms of this License, in the form shown in the Addendum below.

</li><li> Preserve in that license notice the full lists of Invariant Sections
and required Cover Texts given in the Document&rsquo;s license notice.

</li><li> Include an unaltered copy of this License.

</li><li> Preserve the section Entitled &ldquo;History&rdquo;, Preserve its Title, and add
to it an item stating at least the title, year, new authors, and
publisher of the Modified Version as given on the Title Page.  If
there is no section Entitled &ldquo;History&rdquo; in the Document, create one
stating the title, year, authors, and publisher of the Document as
given on its Title Page, then add an item describing the Modified
Version as stated in the previous sentence.

</li><li> Preserve the network location, if any, given in the Document for
public access to a Transparent copy of the Document, and likewise
the network locations given in the Document for previous versions
it was based on.  These may be placed in the &ldquo;History&rdquo; section.
You may omit a network location for a work that was published at
least four years before the Document itself, or if the original
publisher of the version it refers to gives permission.

</li><li> For any section Entitled &ldquo;Acknowledgements&rdquo; or &ldquo;Dedications&rdquo;, Preserve
the Title of the section, and preserve in the section all the
substance and tone of each of the contributor acknowledgements and/or
dedications given therein.

</li><li> Preserve all the Invariant Sections of the Document,
unaltered in their text and in their titles.  Section numbers
or the equivalent are not considered part of the section titles.

</li><li> Delete any section Entitled &ldquo;Endorsements&rdquo;.  Such a section
may not be included in the Modified Version.

</li><li> Do not retitle any existing section to be Entitled &ldquo;Endorsements&rdquo; or
to conflict in title with any Invariant Section.

</li><li> Preserve any Warranty Disclaimers.
</li></ol>

<p>If the Modified Version includes new front-matter sections or
appendices that qualify as Secondary Sections and contain no material
copied from the Document, you may at your option designate some or all
of these sections as invariant.  To do this, add their titles to the
list of Invariant Sections in the Modified Version&rsquo;s license notice.
These titles must be distinct from any other section titles.
</p>
<p>You may add a section Entitled &ldquo;Endorsements&rdquo;, provided it contains
nothing but endorsements of your Modified Version by various
parties&mdash;for example, statements of peer review or that the text has
been approved by an organization as the authoritative definition of a
standard.
</p>
<p>You may add a passage of up to five words as a Front-Cover Text, and a
passage of up to 25 words as a Back-Cover Text, to the end of the list
of Cover Texts in the Modified Version.  Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity.  If the Document already
includes a cover text for the same cover, previously added by you or
by arrangement made by the same entity you are acting on behalf of,
you may not add another; but you may replace the old one, on explicit
permission from the previous publisher that added the old one.
</p>
<p>The author(s) and publisher(s) of the Document do not by this License
give permission to use their names for publicity for or to assert or
imply endorsement of any Modified Version.
</p>
</li><li> COMBINING DOCUMENTS

<p>You may combine the Document with other documents released under this
License, under the terms defined in section 4 above for modified
versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and
list them all as Invariant Sections of your combined work in its
license notice, and that you preserve all their Warranty Disclaimers.
</p>
<p>The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy.  If there are multiple Invariant Sections with the same name but
different contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.
</p>
<p>In the combination, you must combine any sections Entitled &ldquo;History&rdquo;
in the various original documents, forming one section Entitled
&ldquo;History&rdquo;; likewise combine any sections Entitled &ldquo;Acknowledgements&rdquo;,
and any sections Entitled &ldquo;Dedications&rdquo;.  You must delete all
sections Entitled &ldquo;Endorsements.&rdquo;
</p>
</li><li> COLLECTIONS OF DOCUMENTS

<p>You may make a collection consisting of the Document and other documents
released under this License, and replace the individual copies of this
License in the various documents with a single copy that is included in
the collection, provided that you follow the rules of this License for
verbatim copying of each of the documents in all other respects.
</p>
<p>You may extract a single document from such a collection, and distribute
it individually under this License, provided you insert a copy of this
License into the extracted document, and follow this License in all
other respects regarding verbatim copying of that document.
</p>
</li><li> AGGREGATION WITH INDEPENDENT WORKS

<p>A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, is called an &ldquo;aggregate&rdquo; if the copyright
resulting from the compilation is not used to limit the legal rights
of the compilation&rsquo;s users beyond what the individual works permit.
When the Document is included in an aggregate, this License does not
apply to the other works in the aggregate which are not themselves
derivative works of the Document.
</p>
<p>If the Cover Text requirement of section 3 is applicable to these
copies of the Document, then if the Document is less than one half of
the entire aggregate, the Document&rsquo;s Cover Texts may be placed on
covers that bracket the Document within the aggregate, or the
electronic equivalent of covers if the Document is in electronic form.
Otherwise they must appear on printed covers that bracket the whole
aggregate.
</p>
</li><li> TRANSLATION

<p>Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of section 4.
Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections.  You may include a
translation of this License, and all the license notices in the
Document, and any Warranty Disclaimers, provided that you also include
the original English version of this License and the original versions
of those notices and disclaimers.  In case of a disagreement between
the translation and the original version of this License or a notice
or disclaimer, the original version will prevail.
</p>
<p>If a section in the Document is Entitled &ldquo;Acknowledgements&rdquo;,
&ldquo;Dedications&rdquo;, or &ldquo;History&rdquo;, the requirement (section 4) to Preserve
its Title (section 1) will typically require changing the actual
title.
</p>
</li><li> TERMINATION

<p>You may not copy, modify, sublicense, or distribute the Document
except as expressly provided under this License.  Any attempt
otherwise to copy, modify, sublicense, or distribute it is void, and
will automatically terminate your rights under this License.
</p>
<p>However, if you cease all violation of this License, then your license
from a particular copyright holder is reinstated (a) provisionally,
unless and until the copyright holder explicitly and finally
terminates your license, and (b) permanently, if the copyright holder
fails to notify you of the violation by some reasonable means prior to
60 days after the cessation.
</p>
<p>Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
</p>
<p>Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License.  If your rights have been terminated and not permanently
reinstated, receipt of a copy of some or all of the same material does
not give you any rights to use it.
</p>
</li><li> FUTURE REVISIONS OF THIS LICENSE

<p>The Free Software Foundation may publish new, revised versions
of the GNU Free Documentation License from time to time.  Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.  See
<a href="https://www.gnu.org/licenses/">https://www.gnu.org/licenses/</a>.
</p>
<p>Each version of the License is given a distinguishing version number.
If the Document specifies that a particular numbered version of this
License &ldquo;or any later version&rdquo; applies to it, you have the option of
following the terms and conditions either of that specified version or
of any later version that has been published (not as a draft) by the
Free Software Foundation.  If the Document does not specify a version
number of this License, you may choose any version ever published (not
as a draft) by the Free Software Foundation.  If the Document
specifies that a proxy can decide which future versions of this
License can be used, that proxy&rsquo;s public statement of acceptance of a
version permanently authorizes you to choose that version for the
Document.
</p>
</li><li> RELICENSING

<p>&ldquo;Massive Multiauthor Collaboration Site&rdquo; (or &ldquo;MMC Site&rdquo;) means any
World Wide Web server that publishes copyrightable works and also
provides prominent facilities for anybody to edit those works.  A
public wiki that anybody can edit is an example of such a server.  A
&ldquo;Massive Multiauthor Collaboration&rdquo; (or &ldquo;MMC&rdquo;) contained in the
site means any set of copyrightable works thus published on the MMC
site.
</p>
<p>&ldquo;CC-BY-SA&rdquo; means the Creative Commons Attribution-Share Alike 3.0
license published by Creative Commons Corporation, a not-for-profit
corporation with a principal place of business in San Francisco,
California, as well as future copyleft versions of that license
published by that same organization.
</p>
<p>&ldquo;Incorporate&rdquo; means to publish or republish a Document, in whole or
in part, as part of another Document.
</p>
<p>An MMC is &ldquo;eligible for relicensing&rdquo; if it is licensed under this
License, and if all works that were first published under this License
somewhere other than this MMC, and subsequently incorporated in whole
or in part into the MMC, (1) had no cover texts or invariant sections,
and (2) were thus incorporated prior to November 1, 2008.
</p>
<p>The operator of an MMC Site may republish an MMC contained in the site
under CC-BY-SA on the same site at any time before August 1, 2009,
provided the MMC is eligible for relicensing.
</p>
</li></ol>

<span id="ADDENDUM_003a-How-to-use-this-License-for-your-documents"></span><h3 class="heading">ADDENDUM: How to use this License for your documents</h3>

<p>To use this License in a document you have written, include a copy of
the License in the document and put the following copyright and
license notices just after the title page:
</p>
<div class="example">
<pre class="example">  Copyright (C)  <var>year</var>  <var>your name</var>.
  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''.
</pre></div>

<p>If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
replace the &ldquo;with&hellip;Texts.&rdquo; line with this:
</p>
<div class="example">
<pre class="example">    with the Invariant Sections being <var>list their titles</var>, with
    the Front-Cover Texts being <var>list</var>, and with the Back-Cover Texts
    being <var>list</var>.
</pre></div>

<p>If you have Invariant Sections without Cover Texts, or some other
combination of the three, merge those two alternatives to suit the
situation.
</p>
<p>If your document contains nontrivial examples of program code, we
recommend releasing these examples in parallel under your choice of
free software license, such as the GNU General Public License,
to permit their use in free software.
</p>


<hr>
</div>
<div class="unnumbered" id="Concept-Index">
<div class="header">
<p>
Next: <a href="#Command-and-Option-Index" accesskey="n" rel="next">Command and Option Index</a>, Previous: <a href="#GNU-Free-Documentation-License" accesskey="p" rel="prev">GNU Free Documentation License</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Concept-Index-1"></span><h2 class="unnumbered">Concept Index</h2>

<p>This is a general index of all issues discussed in this manual, with the
exception of the <code>sed</code> commands and command-line options.
</p>
<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-Index_cp_symbol-1"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_symbol-2"><b>0</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_symbol-3"><b>;</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Concept-Index_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-X"><b>X</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-Z"><b>Z</b></a>
 &nbsp; 
</td></tr></table>
<table class="index-cp" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_symbol-1">-</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002de_002c-example">-e, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002de_002c-example-1">-e, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dexpression_002c-example">&ndash;expression, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002df_002c-example">-f, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002df_002c-example-1">-f, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dfile_002c-example">&ndash;file, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002di_002c-example">-i, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dn_002c-example">-n, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002ds_002c-example">-s, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_symbol-2">0</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-0-address"><code>0</code> address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_symbol-3">;</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003b_002c-command-separator">;, command separator</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-A">A</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-a_002c-and-semicolons"><code>a</code>, and semicolons</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Additional-reading-about-sed">Additional reading about <code>sed</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Resources">Other Resources</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addr1_002c_002bN"><var>addr1</var>,+N</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addr1_002c_007eN"><var>addr1</var>,~N</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-address-range_002c-example">address range, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Address_002c-as-a-regular-expression">Address, as a regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Address_002c-last-line">Address, last line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Address_002c-numeric">Address, numeric</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-excluding">addresses, excluding</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Addresses_002c-in-sed-scripts">Addresses, in <code>sed</code> scripts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-negating">addresses, negating</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-numeric">addresses, numeric</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-range">addresses, range</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-regular-expression">addresses, regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-addresses_002c-syntax">addresses, syntax</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-alphabetic-characters">alphabetic characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-alphanumeric-characters">alphanumeric characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Append-hold-space-to-pattern-space">Append hold space to pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Append-next-input-line-to-pattern-space">Append next input line to pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Append-pattern-space-to-hold-space">Append pattern space to hold space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Appending-text-after-a-line">Appending text after a line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-B">B</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-b_002c-joining-lines-with">b, joining lines with</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-b_002c-versus-t">b, versus t</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-back_002dreference">back-reference</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Backreferences_002c-in-regular-expressions">Backreferences, in regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-blank-characters">blank characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-bracket-expression">bracket expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Branch-to-a-label_002c-if-s_002f_002f_002f-failed">Branch to a label, if <code>s///</code> failed</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Branch-to-a-label_002c-if-s_002f_002f_002f-succeeded">Branch to a label, if <code>s///</code> succeeded</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Branch-to-a-label_002c-unconditionally">Branch to a label, unconditionally</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-branching-and-n_002c-N">branching and n, N</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-branching_002c-infinite-loop">branching, infinite loop</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-branching_002c-joining-lines">branching, joining lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Buffer-spaces_002c-pattern-and-hold">Buffer spaces, pattern and hold</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Bugs_002c-reporting">Bugs, reporting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-C">C</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-c_002c-and-semicolons"><code>c</code>, and semicolons</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-case-insensitive_002c-regular-expression">case insensitive, regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Case_002dinsensitive-matching">Case-insensitive matching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Caveat-_002d_002d_002d-_0023n-on-first-line">Caveat &mdash; #n on first line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-character-class">character class</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-character-classes">character classes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-classes-of-characters">classes of characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Command-groups">Command groups</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Comments_002c-in-scripts">Comments, in scripts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Conditional-branch">Conditional branch</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Conditional-branch-1">Conditional branch</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-control-characters">control characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Copy-hold-space-into-pattern-space">Copy hold space into pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Copy-pattern-space-into-hold-space">Copy pattern space into hold space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cycle_002c-restarting">cycle, restarting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-D">D</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-d_002c-example"><code>d</code>, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Delete-first-line-from-pattern-space">Delete first line from pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-digit-characters">digit characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Disabling-autoprint_002c-from-command-line">Disabling autoprint, from command line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-E">E</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-empty-regular-expression">empty regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Emptying-pattern-space">Emptying pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Emptying-pattern-space-1">Emptying pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Evaluate-Bourne_002dshell-commands">Evaluate Bourne-shell commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution">Evaluate Bourne-shell commands, after substitution</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-example_002c-address-range">example, address range</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-example_002c-regular-expression">example, regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Exchange-hold-space-with-pattern-space">Exchange hold space with pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Excluding-lines">Excluding lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-exit-status">exit status</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Exit-status">Exit status</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-exit-status_002c-example">exit status, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Exit-status">Exit status</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Extended-regular-expressions_002c-choosing">Extended regular expressions, choosing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Extended-regular-expressions_002c-syntax">Extended regular expressions, syntax</a>:</td><td>&nbsp;</td><td valign="top"><a href="#ERE-syntax">ERE syntax</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-F">F</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-File-name_002c-printing">File name, printing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Files-to-be-processed-as-input">Files to be processed as input</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Flow-of-control-in-scripts">Flow of control in scripts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-G">G</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Global-substitution">Global substitution</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstderr-file">GNU extensions, <samp>/dev/stderr</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstderr-file-1">GNU extensions, <samp>/dev/stderr</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstdin-file">GNU extensions, <samp>/dev/stdin</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstdin-file-1">GNU extensions, <samp>/dev/stdin</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file">GNU extensions, <samp>/dev/stdout</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file-1">GNU extensions, <samp>/dev/stdout</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file-2">GNU extensions, <samp>/dev/stdout</samp> file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-0-address">GNU extensions, <code>0</code> address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-0-address-1">GNU extensions, <code>0</code> address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-0_002caddr2-addressing">GNU extensions, 0,<var>addr2</var> addressing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-addr1_002c_002bN-addressing">GNU extensions, <var>addr1</var>,+<var>N</var> addressing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-addr1_002c_007eN-addressing">GNU extensions, <var>addr1</var>,~<var>N</var> addressing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-branch-if-s_002f_002f_002f-failed">GNU extensions, branch if <code>s///</code> failed</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-case-modifiers-in-s-commands">GNU extensions, case modifiers in <code>s</code> commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-checking-for-their-presence">GNU extensions, checking for their presence</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-debug">GNU extensions, debug</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-disabling">GNU extensions, disabling</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-emptying-pattern-space">GNU extensions, emptying pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-emptying-pattern-space-1">GNU extensions, emptying pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands">GNU extensions, evaluating Bourne-shell commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands-1">GNU extensions, evaluating Bourne-shell commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-extended-regular-expressions">GNU extensions, extended regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-g-and-number-modifier">GNU extensions, <code>g</code> and <var>number</var> modifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-I-modifier">GNU extensions, <code>I</code> modifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-I-modifier-1">GNU extensions, <code>I</code> modifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-in_002dplace-editing">GNU extensions, in-place editing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-in_002dplace-editing-1">GNU extensions, in-place editing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-M-modifier">GNU extensions, <code>M</code> modifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-M-modifier-1">GNU extensions, <code>M</code> modifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-modifiers-and-the-empty-regular-expression">GNU extensions, modifiers and the empty regular expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-n_007em-addresses">GNU extensions, &lsquo;<samp><var>n</var>~<var>m</var></samp>&rsquo; addresses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-quitting-silently">GNU extensions, quitting silently</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-R-command">GNU extensions, <code>R</code> command</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-reading-a-file-a-line-at-a-time">GNU extensions, reading a file a line at a time</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-returning-an-exit-code">GNU extensions, returning an exit code</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-returning-an-exit-code-1">GNU extensions, returning an exit code</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-setting-line-length">GNU extensions, setting line length</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-special-escapes">GNU extensions, special escapes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Escapes">Escapes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-special-escapes-1">GNU extensions, special escapes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-special-two_002daddress-forms">GNU extensions, special two-address forms</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-subprocesses">GNU extensions, subprocesses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-subprocesses-1">GNU extensions, subprocesses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-1">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-2">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-3">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-4">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-5">GNU extensions, to basic regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands">GNU extensions, two addresses supported by most commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-1">GNU extensions, two addresses supported by most commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-2">GNU extensions, two addresses supported by most commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-3">GNU extensions, two addresses supported by most commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-unlimited-line-length">GNU extensions, unlimited line length</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Limitations">Limitations</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-GNU-extensions_002c-writing-first-line-to-a-file">GNU extensions, writing first line to a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Goto_002c-in-scripts">Goto, in scripts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-graphic-characters">graphic characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Greedy-regular-expression-matching">Greedy regular expression matching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#BRE-syntax">BRE syntax</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Grouping-commands">Grouping commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-H">H</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-hexadecimal-digits">hexadecimal digits</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-appending-from-pattern-space">Hold space, appending from pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-appending-to-pattern-space">Hold space, appending to pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-copy-into-pattern-space">Hold space, copy into pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-copying-pattern-space-into">Hold space, copying pattern space into</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-definition">Hold space, definition</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Hold-space_002c-exchange-with-pattern-space">Hold space, exchange with pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-I">I</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-i_002c-and-semicolons"><code>i</code>, and semicolons</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-In_002dplace-editing">In-place editing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-In_002dplace-editing_002c-activating">In-place editing, activating</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names">In-place editing, Perl-style backup file names</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-infinite-loop_002c-branching">infinite loop, branching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Inserting-text-before-a-line">Inserting text before a line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-J">J</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-joining-lines-with-branching">joining lines with branching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-joining-quoted_002dprintable-lines">joining quoted-printable lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-L">L</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-labels">labels</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Labels_002c-in-scripts">Labels, in scripts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Last-line_002c-selecting">Last line, selecting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line-length_002c-setting">Line length, setting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line-length_002c-setting-1">Line length, setting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line-number_002c-printing">Line number, printing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line-selection">Line selection</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line_002c-selecting-by-number">Line, selecting by number</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line_002c-selecting-by-regular-expression-match">Line, selecting by regular expression match</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Line_002c-selecting-last">Line, selecting last</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-List-pattern-space">List pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lower_002dcase-letters">lower-case letters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-M">M</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Mixing-g-and-number-modifiers-in-the-s-command">Mixing <code>g</code> and <var>number</var> modifiers in the <code>s</code> command</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-multiple-files">multiple files</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-multiple-sed-commands">multiple <code>sed</code> commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-N">N</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-n_002c-and-branching">n, and branching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-N_002c-and-branching">N, and branching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-named-character-classes">named character classes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-newline_002c-command-separator">newline, command separator</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Next-input-line_002c-append-to-pattern-space">Next input line, append to pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Next-input-line_002c-replace-pattern-space-with">Next input line, replace pattern space with</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-0-address">Non-bugs, <code>0</code> address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-in_002dplace-editing">Non-bugs, in-place editing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-localization_002drelated">Non-bugs, localization-related</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-localization_002drelated-1">Non-bugs, localization-related</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-N-command-on-the-last-line">Non-bugs, <code>N</code> command on the last line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Non_002dbugs_002c-regex-syntax-clashes">Non-bugs, regex syntax clashes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-numeric-addresses">numeric addresses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-numeric-characters">numeric characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-O">O</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-omitting-labels">omitting labels</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-output">output</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-output_002c-suppressing">output, suppressing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-P">P</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-p_002c-example">p, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-paragraphs_002c-processing">paragraphs, processing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiline-techniques">Multiline techniques</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parameters_002c-script">parameters, script</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Parenthesized-substrings">Parenthesized substrings</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Pattern-space_002c-definition">Pattern space, definition</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Portability_002c-comments">Portability, comments</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Portability_002c-line-length-limitations">Portability, line length limitations</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Limitations">Limitations</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Portability_002c-N-command-on-the-last-line">Portability, <code>N</code> command on the last line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-bracket-expressions"><code>POSIXLY_CORRECT</code> behavior, bracket expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-enabling"><code>POSIXLY_CORRECT</code> behavior, enabling</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-escapes"><code>POSIXLY_CORRECT</code> behavior, escapes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Escapes">Escapes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-N-command"><code>POSIXLY_CORRECT</code> behavior, <code>N</code> command</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Print-first-line-from-pattern-space">Print first line from pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-printable-characters">printable characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Printing-file-name">Printing file name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Printing-line-number">Printing line number</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Printing-text-unambiguously">Printing text unambiguously</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-processing-paragraphs">processing paragraphs</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiline-techniques">Multiline techniques</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-punctuation-characters">punctuation characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-Q">Q</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Q_002c-example">Q, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Exit-status">Exit status</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-q_002c-example"><code>q</code>, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Quitting">Quitting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Quitting-1">Quitting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-quoted_002dprintable-lines_002c-joining">quoted-printable lines, joining</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-R">R</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-range-addresses">range addresses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-range-expression">range expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Range-of-lines">Range of lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Range-with-start-address-of-zero">Range with start address of zero</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Read-next-input-line">Read next input line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Read-text-from-a-file">Read text from a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Read-text-from-a-file-1">Read text from a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-regex-addresses-and-input-lines">regex addresses and input lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-regex-addresses-and-pattern-space">regex addresses and pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-regular-expression-addresses">regular expression addresses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-regular-expression_002c-example">regular expression, example</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Replace-hold-space-with-copy-of-pattern-space">Replace hold space with copy of pattern space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Replace-pattern-space-with-copy-of-hold-space">Replace pattern space with copy of hold space</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Replacing-all-text-matching-regexp-in-a-line">Replacing all text matching regexp in a line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Replacing-only-nth-match-of-regexp-in-a-line">Replacing only <var>n</var>th match of regexp in a line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Replacing-selected-lines-with-other-text">Replacing selected lines with other text</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Requiring-GNU-sed">Requiring GNU <code>sed</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-restarting-a-cycle">restarting a cycle</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-S">S</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sandbox-mode">Sandbox mode</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-script-parameter">script parameter</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Script-structure">Script structure</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Script_002c-from-a-file">Script, from a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Script_002c-from-command-line">Script, from command line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sed-commands-syntax"><code>sed</code> commands syntax</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sed-commands_002c-multiple"><code>sed</code> commands, multiple</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sed-script-structure"><code>sed</code> script structure</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Selecting-lines-to-process">Selecting lines to process</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Selecting-non_002dmatching-lines">Selecting non-matching lines</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Addresses-overview">Addresses overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-semicolons_002c-command-separator">semicolons, command separator</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Several-lines_002c-selecting">Several lines, selecting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Slash-character_002c-in-regular-expressions">Slash character, in regular expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-space-characters">space characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Spaces_002c-pattern-and-hold">Spaces, pattern and hold</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Special-addressing-forms">Special addressing forms</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-standard-input">standard input</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Standard-input_002c-processing-as-input">Standard input, processing as input</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-standard-output">standard output</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-stdin">stdin</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-stdout">stdout</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Stream-editor">Stream editor</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introduction">Introduction</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-subexpression">subexpression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Subprocesses">Subprocesses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Subprocesses-1">Subprocesses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Substitution-of-text_002c-options">Substitution of text, options</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-suppressing-output">suppressing output</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-syntax_002c-addresses">syntax, addresses</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-syntax_002c-sed-commands">syntax, <code>sed</code> commands</a>:</td><td>&nbsp;</td><td valign="top"><a href="#sed-script-overview">sed script overview</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-T">T</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-t_002c-joining-lines-with">t, joining lines with</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-t_002c-versus-b">t, versus b</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-appending">Text, appending</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-deleting">Text, deleting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-insertion">Text, insertion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-printing">Text, printing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-printing-after-substitution">Text, printing after substitution</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Text_002c-writing-to-a-file-after-substitution">Text, writing to a file after substitution</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Transliteration">Transliteration</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-U">U</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Unbuffered-I_002fO_002c-choosing">Unbuffered I/O, choosing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-upper_002dcase-letters">upper-case letters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Usage-summary_002c-printing">Usage summary, printing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-V">V</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Version_002c-printing">Version, printing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-W">W</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-whitespace-characters">whitespace characters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Working-on-separate-files">Working on separate files</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Write-first-line-to-a-file">Write first line to a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Write-to-a-file">Write to a file</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-X">X</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-xdigit-class">xdigit class</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Concept-Index_cp_letter-Z">Z</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Zero-Address">Zero Address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Zero-Address">Zero Address</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Zero_002c-as-range-start-address">Zero, as range start address</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Range-Addresses">Range Addresses</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-Index_cp_symbol-1"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_symbol-2"><b>0</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_symbol-3"><b>;</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Concept-Index_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-X"><b>X</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-Index_cp_letter-Z"><b>Z</b></a>
 &nbsp; 
</td></tr></table>

<hr>
</div>
<div class="unnumbered" id="Command-and-Option-Index">
<div class="header">
<p>
Previous: <a href="#Concept-Index" accesskey="p" rel="prev">Concept Index</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code>sed</code></a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<span id="Command-and-Option-Index-1"></span><h2 class="unnumbered">Command and Option Index</h2>

<p>This is an alphabetical list of all <code>sed</code> commands and command-line
options.
</p>
<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-1"><b>#</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-2"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-3"><b>:</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-4"><b>=</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-5"><b>{</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-X"><b>X</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Y"><b>Y</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Z"><b>Z</b></a>
 &nbsp; 
</td></tr></table>
<table class="index-fn" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_symbol-1">#</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0023-_0028comments_0029"><code># (comments)</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_symbol-2">-</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dbinary"><code>--binary</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002ddebug"><code>--debug</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dexpression"><code>--expression</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dfile"><code>--file</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dfollow_002dsymlinks"><code>--follow-symlinks</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dhelp"><code>--help</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002din_002dplace"><code>--in-place</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dline_002dlength"><code>--line-length</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dnull_002ddata"><code>--null-data</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dposix"><code>--posix</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dquiet"><code>--quiet</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dregexp_002dextended"><code>--regexp-extended</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dsandbox"><code>--sandbox</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dseparate"><code>--separate</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dsilent"><code>--silent</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dunbuffered"><code>--unbuffered</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dversion"><code>--version</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d_002dzero_002dterminated"><code>--zero-terminated</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002db"><code>-b</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002de"><code>-e</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dE"><code>-E</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002df"><code>-f</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002di"><code>-i</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dl"><code>-l</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dn"><code>-n</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dn_002c-forcing-from-within-a-script"><code>-n, forcing from within a script</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dr"><code>-r</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002ds"><code>-s</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002du"><code>-u</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002dz"><code>-z</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_symbol-3">:</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003a-_0028label_0029-command"><code>: (label) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_symbol-4">=</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003d-_0028print-line-number_0029-command"><code>= (print line number) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_symbol-5">{</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_007b_007d-command-grouping"><code>{} command grouping</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-A">A</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-a-_0028append-text-lines_0029-command"><code>a (append text lines) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-alnum-character-class"><code>alnum <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-alpha-character-class"><code>alpha <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-B">B</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-b-_0028branch_0029-command"><code>b (branch) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-blank-character-class"><code>blank <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-C">C</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-c-_0028change-to-text-lines_0029-command"><code>c (change to text lines) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cntrl-character-class"><code>cntrl <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-D">D</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-D-_0028delete-first-line_0029-command"><code>D (delete first line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-d-_0028delete_0029-command"><code>d (delete) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-digit-character-class"><code>digit <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-E">E</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-e-_0028evaluate_0029-command"><code>e (evaluate) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-F">F</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-F-_0028File-name_0029-command"><code>F (File name) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-G">G</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-G-_0028appending-Get_0029-command"><code>G (appending Get) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-g-_0028get_0029-command"><code>g (get) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-graph-character-class"><code>graph <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-H">H</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-H-_0028append-Hold_0029-command"><code>H (append Hold) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-h-_0028hold_0029-command"><code>h (hold) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-I">I</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-i-_0028insert-text-lines_0029-command"><code>i (insert text lines) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-L">L</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-l-_0028list-unambiguously_0029-command"><code>l (list unambiguously) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lower-character-class"><code>lower <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-N">N</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-N-_0028append-Next-line_0029-command"><code>N (append Next line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-n-_0028next_002dline_0029-command"><code>n (next-line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-P">P</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-P-_0028print-first-line_0029-command"><code>P (print first line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-p-_0028print_0029-command"><code>p (print) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-print-character-class"><code>print <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-punct-character-class"><code>punct <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-Q">Q</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-q-_0028quit_0029-command"><code>q (quit) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Common-Commands">Common Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Q-_0028silent-Quit_0029-command"><code>Q (silent Quit) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-R">R</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-r-_0028read-file_0029-command"><code>r (read file) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-R-_0028read-line_0029-command"><code>R (read line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-S">S</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-s-command_002c-option-flags"><code>s command, option flags</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-_0022s_0022-Command">The &quot;s&quot; Command</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-space-character-class"><code>space <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-T">T</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-T-_0028test-and-branch-if-failed_0029-command"><code>T (test and branch if failed) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-t-_0028test-and-branch-if-successful_0029-command"><code>t (test and branch if successful) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Programming-Commands">Programming Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-U">U</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-upper-character-class"><code>upper <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-V">V</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-v-_0028version_0029-command"><code>v (version) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-W">W</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-w-_0028write-file_0029-command"><code>w (write file) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-W-_0028write-first-line_0029-command"><code>W (write first line) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-X">X</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-x-_0028eXchange_0029-command"><code>x (eXchange) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-xdigit-character-class"><code>xdigit <span class="roman">character class</span></code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-Y">Y</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-y-_0028transliterate_0029-command"><code>y (transliterate) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-Commands">Other Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th id="Command-and-Option-Index_fn_letter-Z">Z</th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-z-_0028Zap_0029-command"><code>z (Zap) command</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extended-Commands">Extended Commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-1"><b>#</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-2"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-3"><b>:</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-4"><b>=</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_symbol-5"><b>{</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-X"><b>X</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Y"><b>Y</b></a>
 &nbsp; 
<a class="summary-letter" href="#Command-and-Option-Index_fn_letter-Z"><b>Z</b></a>
 &nbsp; 
</td></tr></table>

</div>
</div>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>

<h5><a id="FOOT1" href="#DOCF1">(1)</a></h5>
<p>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>
<span id="index-GNU-extensions_002c-_002fdev_002fstdout-file"></span>
or <code>W</code> commands together with the <samp>/dev/stdout</samp>
special file</p>
<h5><a id="FOOT2" href="#DOCF2">(2)</a></h5>
<p>Note that GNU <code>sed</code> creates the backup
file whether or not any output is actually changed.</p>
<h5><a id="FOOT3" href="#DOCF3">(3)</a></h5>
<p>This is equivalent to <code>p</code> unless the <samp>-i</samp>
option is being used.</p>
<h5><a id="FOOT4" href="#DOCF4">(4)</a></h5>
<p>This is equivalent to <code>p</code> unless the <samp>-i</samp>
option is being used.</p>
<h5><a id="FOOT5" href="#DOCF5">(5)</a></h5>
<p>There are of course many other ways to do the same,
e.g.
</p><div class="example">
<pre class="example">grep 'bash$' /etc/passwd
awk -F: '$7 == &quot;/bin/bash&quot;' /etc/passwd
</pre></div>
<h5><a id="FOOT6" href="#DOCF6">(6)</a></h5>
<p>All
the escapes introduced here are GNU
extensions, with the exception of <code>\n</code>.  In basic regular
expression mode, setting <code>POSIXLY_CORRECT</code> disables them inside
bracket expressions.</p>
<h5><a id="FOOT7" href="#DOCF7">(7)</a></h5>
<p>Some regexp edge-cases depends on the
operating system and libc implementation. The examples shown are known
to work as-expected on GNU/Linux systems using glibc.</p>
<h5><a id="FOOT8" href="#DOCF8">(8)</a></h5>
<p>Actually,
if <code>sed</code> prints a line without the terminating newline, it will
nevertheless print the missing newline as soon as more text is sent to
the same output stream, which gives the &ldquo;least expected surprise&rdquo;
even though it does not make commands like &lsquo;<samp>sed -n p</samp>&rsquo; exactly
identical to <code>cat</code>.</p>
<h5><a id="FOOT9" href="#DOCF9">(9)</a></h5>
<p><code>sed</code> guru Greg
Ubben wrote an implementation of the <code>dc</code> <small>RPN</small> calculator!
It is distributed together with sed.</p>
<h5><a id="FOOT10" href="#DOCF10">(10)</a></h5>
<p>This requires
another script to pad the output of banner; for example
</p>
<div class="example">
<pre class="example">#! /bin/sh

banner -w $1 $2 $3 $4 |
  sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
  ~/sedscripts/reverseline.sed
</pre></div>
<h5><a id="FOOT11" href="#DOCF11">(11)</a></h5>
<p>Some implementations
have a limit of 199 commands per script</p>
<h5><a id="FOOT12" href="#DOCF12">(12)</a></h5>
<p>which is the actual &ldquo;bug&rdquo; that prompted the
change in behavior</p>
</div>



</body>
</html>

Youez - 2016 - github.com/yon3zu
LinuXploit