7097 lines
405 KiB
HTML
7097 lines
405 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<!-- This file documents version 4.10 of
|
|
GNU sed, a stream editor.
|
|
|
|
Copyright © 1998-2026 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-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
|
a.summary-letter-printindex {text-decoration: none}
|
|
div.center {text-align:center}
|
|
div.display {margin-left: 3.2em}
|
|
div.example {margin-left: 3.2em}
|
|
kbd.kbd {font-style: oblique}
|
|
kbd.key {font-style: normal}
|
|
pre.display-preformatted {font-family: inherit}
|
|
span.r {font-family: initial; font-weight: normal; font-style: normal}
|
|
span:hover a.copiable-link {visibility: visible}
|
|
td.printindex-index-entry {vertical-align: top}
|
|
td.printindex-index-section {vertical-align: top; padding-left: 1em}
|
|
th.entries-header-printindex {text-align:left}
|
|
th.sections-header-printindex {text-align:left; padding-left: 1em}
|
|
ul.mark-bullet {list-style-type: disc}
|
|
ul.toc-numbered-mark {list-style: none}
|
|
-->
|
|
</style>
|
|
|
|
|
|
</head>
|
|
|
|
<body lang="en">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div class="top-level-extent" id="Top">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h1 class="top" id="GNU-sed"><span>GNU <code class="command">sed</code><a class="copiable-link" href="#GNU-sed"> ¶</a></span></h1>
|
|
|
|
<p>This file documents version 4.10 of
|
|
GNU <code class="command">sed</code>, a stream editor.
|
|
</p>
|
|
<p>Copyright © 1998–2026 Free Software Foundation, Inc.
|
|
</p>
|
|
<blockquote class="quotation">
|
|
<p>Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3
|
|
or any later version published by the Free Software Foundation;
|
|
with no Invariant Sections, no Front-Cover Texts, and no
|
|
Back-Cover Texts. A copy of the license is included in the
|
|
section entitled “GNU Free Documentation License”.
|
|
</p></blockquote>
|
|
|
|
|
|
|
|
<div class="element-contents" id="SEC_Contents">
|
|
<h2 class="contents-heading">Table of Contents</h2>
|
|
|
|
<div class="contents">
|
|
|
|
<ul class="toc-numbered-mark">
|
|
<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="toc-numbered-mark">
|
|
<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 class="command">sed</code> scripts</a>
|
|
<ul class="toc-numbered-mark">
|
|
<li><a id="toc-sed-script-overview-1" href="#sed-script-overview">3.1 <code class="command">sed</code> script overview</a></li>
|
|
<li><a id="toc-sed-commands-summary" href="#sed-commands-list">3.2 <code class="command">sed</code> commands summary</a></li>
|
|
<li><a id="toc-The-s-Command" href="#The-_0022s_0022-Command">3.3 The <code class="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 class="command">sed</code> gurus</a></li>
|
|
<li><a id="toc-Commands-Specific-to-GNU-sed" href="#Extended-Commands">3.7 Commands Specific to GNU <code class="command">sed</code></a></li>
|
|
<li><a id="toc-Multiple-commands-syntax-1" href="#Multiple-commands-syntax">3.8 Multiple commands syntax</a>
|
|
<ul class="toc-numbered-mark">
|
|
<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="toc-numbered-mark">
|
|
<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="toc-numbered-mark">
|
|
<li><a id="toc-Overview-of-regular-expression-in-sed" href="#Regular-Expressions-Overview">5.1 Overview of regular expression in <code class="command">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="toc-numbered-mark">
|
|
<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="toc-numbered-mark">
|
|
<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 class="command">sed</code>: cycles and buffers</a>
|
|
<ul class="toc-numbered-mark">
|
|
<li><a id="toc-How-sed-Works" href="#Execution-Cycle">6.1 How <code class="command">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="toc-numbered-mark">
|
|
<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="toc-numbered-mark">
|
|
<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 class="command">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 class="command">sed</code>’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 class="command">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-level-extent" id="Introduction">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Invoking-sed" accesskey="n" rel="next">Running sed</a>, Previous: <a href="#Top" accesskey="p" rel="prev">GNU <code class="command">sed</code></a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Introduction-1"><span>1 Introduction<a class="copiable-link" href="#Introduction-1"> ¶</a></span></h2>
|
|
|
|
<a class="index-entry-id" id="index-Stream-editor"></a>
|
|
<p><code class="command">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 class="command">ed</code>),
|
|
<code class="command">sed</code> works by making only one pass over the
|
|
input(s), and is consequently more efficient.
|
|
But it is <code class="command">sed</code>’s ability to filter text in a pipeline
|
|
which particularly distinguishes it from other types of
|
|
editors.
|
|
</p>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="chapter-level-extent" id="Invoking-sed">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#sed-scripts" accesskey="n" rel="next"><code class="command">sed</code> scripts</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Running-sed"><span>2 Running sed<a class="copiable-link" href="#Running-sed"> ¶</a></span></h2>
|
|
|
|
<p>This chapter covers how to run <code class="command">sed</code>. Details of <code class="command">sed</code>
|
|
scripts and individual <code class="command">sed</code> commands are discussed in the
|
|
next chapter.
|
|
</p>
|
|
|
|
|
|
<ul class="mini-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-level-extent" id="Overview">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Overview-1"><span>2.1 Overview<a class="copiable-link" href="#Overview-1"> ¶</a></span></h3>
|
|
<p>Normally <code class="command">sed</code> is invoked like this:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed SCRIPT INPUTFILE...
|
|
</pre></div>
|
|
|
|
<p>For example, to change every ‘<samp class="samp">hello</samp>’ to ‘<samp class="samp">world</samp>’
|
|
in the file <samp class="file">input.txt</samp>:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed 's/hello/world/g' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
<p>Without the ‘<samp class="samp">g</samp>’ (global) modifier, <code class="command">sed</code> affects
|
|
only the first instance per line.
|
|
</p>
|
|
<a class="index-entry-id" id="index-stdin"></a>
|
|
<a class="index-entry-id" id="index-standard-input"></a>
|
|
<p>If you do not specify <var class="var">INPUTFILE</var>, or if <var class="var">INPUTFILE</var> is <samp class="file">-</samp>,
|
|
<code class="command">sed</code> filters the contents of the standard input. The following
|
|
commands are equivalent:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed 's/hello/world/g' input.txt > output.txt
|
|
sed 's/hello/world/g' < input.txt > output.txt
|
|
cat input.txt | sed 's/hello/world/g' - > output.txt
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-stdout"></a>
|
|
<a class="index-entry-id" id="index-output"></a>
|
|
<a class="index-entry-id" id="index-standard-output"></a>
|
|
<a class="index-entry-id" id="index-_002di_002c-example"></a>
|
|
<p><code class="command">sed</code> writes output to standard output. Use <samp class="option">-i</samp> to edit
|
|
files in-place instead of printing to standard output.
|
|
See also the <code class="code">W</code> and <code class="code">s///w</code> commands for writing output to
|
|
other files. The following command modifies <samp class="file">file.txt</samp> and
|
|
does not produce any output:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -i 's/hello/world/' file.txt
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-_002dn_002c-example"></a>
|
|
<a class="index-entry-id" id="index-p_002c-example"></a>
|
|
<a class="index-entry-id" id="index-suppressing-output"></a>
|
|
<a class="index-entry-id" id="index-output_002c-suppressing"></a>
|
|
<p>By default <code class="command">sed</code> prints all processed input (except input
|
|
that has been modified/deleted by commands such as <code class="command">d</code>).
|
|
Use <samp class="option">-n</samp> to suppress output, and the <code class="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-preformatted">sed -n '45p' file.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
<a class="index-entry-id" id="index-multiple-files"></a>
|
|
<a class="index-entry-id" id="index-_002ds_002c-example"></a>
|
|
<p><code class="command">sed</code> treats multiple input files as one long stream.
|
|
The following example prints the first line of the first file
|
|
(<samp class="file">one.txt</samp>) and the last line of the last file (<samp class="file">three.txt</samp>).
|
|
Use <samp class="option">-s</samp> to reverse this behavior.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -n '1p ; $p' one.txt two.txt three.txt
|
|
</pre></div>
|
|
|
|
|
|
<a class="index-entry-id" id="index-_002de_002c-example"></a>
|
|
<a class="index-entry-id" id="index-_002d_002dexpression_002c-example"></a>
|
|
<a class="index-entry-id" id="index-_002df_002c-example"></a>
|
|
<a class="index-entry-id" id="index-_002d_002dfile_002c-example"></a>
|
|
<a class="index-entry-id" id="index-script-parameter"></a>
|
|
<a class="index-entry-id" id="index-parameters_002c-script"></a>
|
|
<p>Without <samp class="option">-e</samp> or <samp class="option">-f</samp> options, <code class="command">sed</code> uses
|
|
the first non-option parameter as the <var class="var">script</var>, and the following
|
|
non-option parameters as input files.
|
|
If <samp class="option">-e</samp> or <samp class="option">-f</samp> options are used to specify a <var class="var">script</var>,
|
|
all non-option parameters are taken as input files.
|
|
Options <samp class="option">-e</samp> and <samp class="option">-f</samp> can be combined, and can appear
|
|
multiple times (in which case the final effective <var class="var">script</var> will be
|
|
concatenation of all the individual <var class="var">script</var>s).
|
|
</p>
|
|
<p>The following examples are equivalent:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed 's/hello/world/' input.txt > output.txt
|
|
|
|
sed -e 's/hello/world/' input.txt > output.txt
|
|
sed --expression='s/hello/world/' input.txt > output.txt
|
|
|
|
echo 's/hello/world/' > myscript.sed
|
|
sed -f myscript.sed input.txt > output.txt
|
|
sed --file=myscript.sed input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Command_002dLine-Options">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Command_002dLine-Options-1"><span>2.2 Command-Line Options<a class="copiable-link" href="#Command_002dLine-Options-1"> ¶</a></span></h3>
|
|
|
|
<p>The full format for invoking <code class="command">sed</code> is:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed OPTIONS... [SCRIPT] [INPUTFILE...]
|
|
</pre></div>
|
|
|
|
<p><code class="command">sed</code> may be invoked with the following command-line options:
|
|
</p>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Version_002c-printing"></a>
|
|
<a id="index-_002d_002dversion"></a><span><code class="code">--version</code><a class="copiable-link" href="#index-_002d_002dversion"> ¶</a></span></dt>
|
|
<dd><p>Print out the version of <code class="command">sed</code> that is being run and a copyright notice,
|
|
then exit.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Usage-summary_002c-printing"></a>
|
|
<a id="index-_002d_002dhelp"></a><span><code class="code">--help</code><a class="copiable-link" href="#index-_002d_002dhelp"> ¶</a></span></dt>
|
|
<dd><p>Print a usage message briefly summarizing these command-line options
|
|
and the bug-reporting address,
|
|
then exit.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dquiet"></a>
|
|
<a class="index-entry-id" id="index-_002d_002dsilent"></a>
|
|
<a class="index-entry-id" id="index-Disabling-autoprint_002c-from-command-line"></a>
|
|
<a id="index-_002dn"></a><span><code class="code">-n</code><a class="copiable-link" href="#index-_002dn"> ¶</a></span></dt>
|
|
<dt><code class="code">--quiet</code></dt>
|
|
<dt><code class="code">--silent</code></dt>
|
|
<dd><p>By default, <code class="command">sed</code> prints out the pattern space
|
|
at the end of each cycle through the script (see <a class="pxref" href="#Execution-Cycle">How <code class="code">sed</code> works</a>).
|
|
These options disable this automatic printing,
|
|
and <code class="command">sed</code> only produces output when explicitly told to
|
|
via the <code class="code">p</code> command.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-debug"></a>
|
|
<a id="index-_002d_002ddebug"></a><span><code class="code">--debug</code><a class="copiable-link" href="#index-_002d_002ddebug"> ¶</a></span></dt>
|
|
<dd><p>Print the input sed program in canonical form,
|
|
and annotate program execution.
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ 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><a class="index-entry-id" id="index-_002d_002dexpression"></a>
|
|
<a class="index-entry-id" id="index-Script_002c-from-command-line"></a>
|
|
<a id="index-_002de"></a><span><code class="code">-e <var class="var">script</var></code><a class="copiable-link" href="#index-_002de"> ¶</a></span></dt>
|
|
<dt><code class="code">--expression=<var class="var">script</var></code></dt>
|
|
<dd><p>Add the commands in <var class="var">script</var> to the set of commands to be
|
|
run while processing the input.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dfile"></a>
|
|
<a class="index-entry-id" id="index-Script_002c-from-a-file"></a>
|
|
<a id="index-_002df"></a><span><code class="code">-f <var class="var">script-file</var></code><a class="copiable-link" href="#index-_002df"> ¶</a></span></dt>
|
|
<dt><code class="code">--file=<var class="var">script-file</var></code></dt>
|
|
<dd><p>Add the commands contained in the file <var class="var">script-file</var>
|
|
to the set of commands to be run while processing the input.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002din_002dplace"></a>
|
|
<a class="index-entry-id" id="index-In_002dplace-editing_002c-activating"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-in_002dplace-editing"></a>
|
|
<a id="index-_002di"></a><span><code class="code">-i[<var class="var">SUFFIX</var>]</code><a class="copiable-link" href="#index-_002di"> ¶</a></span></dt>
|
|
<dt><code class="code">--in-place[=<var class="var">SUFFIX</var>]</code></dt>
|
|
<dd><p>This option specifies that files are to be edited in-place.
|
|
GNU <code class="command">sed</code> does this by creating a temporary file and
|
|
sending output to this file rather than to the standard
|
|
output.<a class="footnote" id="DOCF1" href="#FOOT1"><sup>1</sup></a>.
|
|
</p>
|
|
<p>This option implies <samp class="option">-s</samp>.
|
|
</p>
|
|
<p>When the end of the file is reached, the temporary file is
|
|
renamed to the output file’s original name. The extension,
|
|
if supplied, is used to modify the name of the old file
|
|
before renaming the temporary file, thereby making a backup
|
|
copy<a class="footnote" id="DOCF2" href="#FOOT2"><sup>2</sup></a>).
|
|
</p>
|
|
<a class="index-entry-id" id="index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names"></a>
|
|
<p>This rule is followed: if the extension doesn’t contain a <code class="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 class="code">*</code>
|
|
characters, then <em class="emph">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 class="option">-i</samp> takes an optional argument, it should
|
|
not be followed by other short options:
|
|
</p><dl class="table">
|
|
<dt><code class="code">sed -Ei '...' FILE</code></dt>
|
|
<dd><p>Same as <samp class="option">-E -i</samp> with no backup suffix - <samp class="file">FILE</samp> will be
|
|
edited in-place without creating a backup.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">sed -iE '...' FILE</code></dt>
|
|
<dd><p>This is equivalent to <samp class="option">--in-place=E</samp>, creating <samp class="file">FILEE</samp> as backup
|
|
of <samp class="file">FILE</samp>
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<p>Be cautious of using <samp class="option">-n</samp> with <samp class="option">-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 class="code">p</code> command),
|
|
the output file will be empty:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted"># WRONG USAGE: 'FILE' will be truncated.
|
|
sed -ni 's/foo/bar/' FILE
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dline_002dlength"></a>
|
|
<a class="index-entry-id" id="index-Line-length_002c-setting"></a>
|
|
<a id="index-_002dl"></a><span><code class="code">-l <var class="var">N</var></code><a class="copiable-link" href="#index-_002dl"> ¶</a></span></dt>
|
|
<dt><code class="code">--line-length=<var class="var">N</var></code></dt>
|
|
<dd><p>Specify the default line-wrap length for the <code class="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><a class="index-entry-id" id="index-GNU-extensions_002c-disabling"></a>
|
|
<a id="index-_002d_002dposix"></a><span><code class="code">--posix</code><a class="copiable-link" href="#index-_002d_002dposix"> ¶</a></span></dt>
|
|
<dd><p>GNU <code class="command">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.
|
|
<a class="index-entry-id" id="index-POSIXLY_005fCORRECT-behavior_002c-enabling"></a>
|
|
Most of the extensions accept <code class="command">sed</code> programs that
|
|
are outside the syntax mandated by POSIX, but some
|
|
of them (such as the behavior of the <code class="command">N</code> command
|
|
described in <a class="ref" 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 class="code">POSIXLY_CORRECT</code> variable
|
|
to a non-empty value.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dbinary"></a>
|
|
<a id="index-_002db"></a><span><code class="code">-b</code><a class="copiable-link" href="#index-_002db"> ¶</a></span></dt>
|
|
<dt><code class="code">--binary</code></dt>
|
|
<dd><p>This option is available on every platform, but is only effective where the
|
|
operating system makes a distinction between text files and binary files.
|
|
When such a distinction is made—as is the case for MS-DOS, Windows,
|
|
Cygwin—text files are composed of lines separated by a carriage return
|
|
<em class="emph">and</em> a line feed character, and <code class="command">sed</code> does not see the
|
|
ending CR. When this option is specified, <code class="command">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><a id="index-_002d_002dfollow_002dsymlinks"></a><span><code class="code">--follow-symlinks</code><a class="copiable-link" href="#index-_002d_002dfollow_002dsymlinks"> ¶</a></span></dt>
|
|
<dd><p>This option is available only on platforms that support
|
|
symbolic links and has an effect only if option <samp class="option">-i</samp>
|
|
is specified. In this case, if the file that is specified
|
|
on the command line is a symbolic link, <code class="command">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><a class="index-entry-id" id="index-_002dr"></a>
|
|
<a class="index-entry-id" id="index-_002d_002dregexp_002dextended"></a>
|
|
<a class="index-entry-id" id="index-Extended-regular-expressions_002c-choosing"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-extended-regular-expressions"></a>
|
|
<a id="index-_002dE"></a><span><code class="code">-E</code><a class="copiable-link" href="#index-_002dE"> ¶</a></span></dt>
|
|
<dt><code class="code">-r</code></dt>
|
|
<dt><code class="code">--regexp-extended</code></dt>
|
|
<dd><p>Use extended regular expressions rather than basic
|
|
regular expressions. Extended regexps are those that
|
|
<code class="command">egrep</code> accepts; they can be clearer because they
|
|
usually have fewer backslashes.
|
|
Historically this was a GNU extension,
|
|
but the <samp class="option">-E</samp>
|
|
extension has since been added to the POSIX standard
|
|
(http://austingroupbugs.net/view.php?id=528),
|
|
so use <samp class="option">-E</samp> for portability.
|
|
GNU sed has accepted <samp class="option">-E</samp> as an undocumented option for years,
|
|
and *BSD seds have accepted <samp class="option">-E</samp> for years as well,
|
|
but scripts that use <samp class="option">-E</samp> might not port to other older systems.
|
|
See <a class="xref" href="#ERE-syntax">Extended regular expressions</a>.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dseparate"></a>
|
|
<a class="index-entry-id" id="index-Working-on-separate-files"></a>
|
|
<a id="index-_002ds"></a><span><code class="code">-s</code><a class="copiable-link" href="#index-_002ds"> ¶</a></span></dt>
|
|
<dt><code class="code">--separate</code></dt>
|
|
<dd><p>By default, <code class="command">sed</code> will consider the files specified on the
|
|
command line as a single continuous long stream. This GNU <code class="command">sed</code>
|
|
extension allows the user to consider them as separate files:
|
|
range addresses (such as ‘<samp class="samp">/abc/,/def/</samp>’) are not allowed
|
|
to span several files, line numbers are relative to the start
|
|
of each file, <code class="code">$</code> refers to the last line of each file,
|
|
and files invoked from the <code class="code">R</code> commands are rewound at the
|
|
start of each file.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Sandbox-mode"></a>
|
|
<a id="index-_002d_002dsandbox"></a><span><code class="code">--sandbox</code><a class="copiable-link" href="#index-_002d_002dsandbox"> ¶</a></span></dt>
|
|
<dd><p>In sandbox mode, <code class="code">e/w/r</code> commands are rejected - programs containing
|
|
them will be aborted without being run. Sandbox mode ensures <code class="command">sed</code>
|
|
operates only on the input files designated on the command line, and
|
|
cannot run external programs.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dunbuffered"></a>
|
|
<a class="index-entry-id" id="index-Unbuffered-I_002fO_002c-choosing"></a>
|
|
<a id="index-_002du"></a><span><code class="code">-u</code><a class="copiable-link" href="#index-_002du"> ¶</a></span></dt>
|
|
<dt><code class="code">--unbuffered</code></dt>
|
|
<dd><p>Buffer both input and output as minimally as practical.
|
|
(This is particularly useful if the input is coming from
|
|
the likes of ‘<samp class="samp">tail -f</samp>’, and you wish to see the transformed
|
|
output as soon as possible.)
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-_002d_002dnull_002ddata"></a>
|
|
<a class="index-entry-id" id="index-_002d_002dzero_002dterminated"></a>
|
|
<a id="index-_002dz"></a><span><code class="code">-z</code><a class="copiable-link" href="#index-_002dz"> ¶</a></span></dt>
|
|
<dt><code class="code">--null-data</code></dt>
|
|
<dt><code class="code">--zero-terminated</code></dt>
|
|
<dd><p>Treat the input as a set of lines, each terminated by a zero byte
|
|
(the ASCII ‘<samp class="samp">NUL</samp>’ character) instead of a newline. This option can
|
|
be used with commands like ‘<samp class="samp">sort -z</samp>’ and ‘<samp class="samp">find -print0</samp>’
|
|
to process arbitrary file names.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<p>If no <samp class="option">-e</samp>, <samp class="option">-f</samp>, <samp class="option">--expression</samp>, or <samp class="option">--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 class="var">script</var> to be executed.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Files-to-be-processed-as-input"></a>
|
|
<p>If any command-line parameters remain after processing the above,
|
|
these parameters are interpreted as the names of input files to
|
|
be processed.
|
|
<a class="index-entry-id" id="index-Standard-input_002c-processing-as-input"></a>
|
|
A file name of ‘<samp class="samp">-</samp>’ refers to the standard input stream.
|
|
The standard input will be processed if no file names are specified.
|
|
</p>
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Exit-status">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Exit-status-1"><span>2.3 Exit status<a class="copiable-link" href="#Exit-status-1"> ¶</a></span></h3>
|
|
<a class="index-entry-id" id="index-exit-status"></a>
|
|
<p>An exit status of zero indicates success, and a nonzero value
|
|
indicates failure. GNU <code class="command">sed</code> returns the following exit status
|
|
error values:
|
|
</p>
|
|
<dl class="table">
|
|
<dt>0</dt>
|
|
<dd><p>Successful completion.
|
|
</p>
|
|
</dd>
|
|
<dt>1</dt>
|
|
<dd><p>Invalid command, invalid syntax, invalid regular expression or a
|
|
GNU <code class="command">sed</code> extension command used with <samp class="option">--posix</samp>.
|
|
</p>
|
|
</dd>
|
|
<dt>2</dt>
|
|
<dd><p>One or more of the input file specified on the command line could not be
|
|
opened (e.g. if a file is not found, or read permission is denied).
|
|
Processing continued with other files.
|
|
</p>
|
|
</dd>
|
|
<dt>4</dt>
|
|
<dd><p>An I/O error, or a serious processing error during runtime,
|
|
GNU <code class="command">sed</code> aborted immediately.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<a class="index-entry-id" id="index-Q_002c-example"></a>
|
|
<a class="index-entry-id" id="index-exit-status_002c-example"></a>
|
|
<p>Additionally, the commands <code class="code">q</code> and <code class="code">Q</code> can be used to terminate
|
|
<code class="command">sed</code> with a custom exit code value (this is a GNU <code class="command">sed</code> extension):
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo | sed 'Q42' ; echo $?
|
|
42
|
|
</pre></div>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
<div class="chapter-level-extent" id="sed-scripts">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="sed-scripts-1"><span>3 <code class="command">sed</code> scripts<a class="copiable-link" href="#sed-scripts-1"> ¶</a></span></h2>
|
|
|
|
|
|
|
|
<ul class="mini-toc">
|
|
<li><a href="#sed-script-overview" accesskey="1"><code class="command">sed</code> script overview</a></li>
|
|
<li><a href="#sed-commands-list" accesskey="2"><code class="command">sed</code> commands summary</a></li>
|
|
<li><a href="#The-_0022s_0022-Command" accesskey="3">The <code class="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 class="command">sed</code> gurus</a></li>
|
|
<li><a href="#Extended-Commands" accesskey="7">Commands Specific to GNU <code class="command">sed</code></a></li>
|
|
<li><a href="#Multiple-commands-syntax" accesskey="8">Multiple commands syntax</a></li>
|
|
</ul>
|
|
<hr>
|
|
<div class="section-level-extent" id="sed-script-overview">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#sed-commands-list" accesskey="n" rel="next"><code class="command">sed</code> commands summary</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="sed-script-overview-1"><span>3.1 <code class="command">sed</code> script overview<a class="copiable-link" href="#sed-script-overview-1"> ¶</a></span></h3>
|
|
|
|
<a class="index-entry-id" id="index-sed-script-structure"></a>
|
|
<a class="index-entry-id" id="index-Script-structure"></a>
|
|
|
|
<p>A <code class="command">sed</code> program consists of one or more <code class="command">sed</code> commands,
|
|
passed in by one or more of the
|
|
<samp class="option">-e</samp>, <samp class="option">-f</samp>, <samp class="option">--expression</samp>, and <samp class="option">--file</samp>
|
|
options, or the first non-option argument if zero of these
|
|
options are used.
|
|
This document will refer to “the” <code class="command">sed</code> script;
|
|
this is understood to mean the in-order concatenation
|
|
of all of the <var class="var">script</var>s and <var class="var">script-file</var>s passed in.
|
|
See <a class="xref" href="#Overview">Overview</a>.
|
|
</p>
|
|
|
|
<a class="index-entry-id" id="index-sed-commands-syntax"></a>
|
|
<a class="index-entry-id" id="index-syntax_002c-sed-commands"></a>
|
|
<a class="index-entry-id" id="index-addresses_002c-syntax"></a>
|
|
<a class="index-entry-id" id="index-syntax_002c-addresses"></a>
|
|
<p><code class="command">sed</code> commands follow this syntax:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">[addr]<var class="var">X</var>[options]
|
|
</pre></div>
|
|
|
|
<p><var class="var">X</var> is a single-letter <code class="command">sed</code> command.
|
|
<code class="code">[addr]</code> is an optional line address. If <code class="code">[addr]</code> is specified,
|
|
the command <var class="var">X</var> will be executed only on the matched lines.
|
|
<code class="code">[addr]</code> can be a single line number, a regular expression,
|
|
or a range of lines (see <a class="pxref" href="#sed-addresses">Addresses: selecting lines</a>).
|
|
Additional <code class="code">[options]</code> are used for some <code class="command">sed</code> commands.
|
|
</p>
|
|
<a class="index-entry-id" id="index-d_002c-example"></a>
|
|
<a class="index-entry-id" id="index-address-range_002c-example"></a>
|
|
<a class="index-entry-id" id="index-example_002c-address-range"></a>
|
|
<p>The following example deletes lines 30 to 35 in the input.
|
|
<code class="code">30,35</code> is an address range. <code class="command">d</code> is the delete command:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '30,35d' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-q_002c-example"></a>
|
|
<a class="index-entry-id" id="index-regular-expression_002c-example"></a>
|
|
<a class="index-entry-id" id="index-example_002c-regular-expression"></a>
|
|
<p>The following example prints all input until a line
|
|
starting with the string ‘<samp class="samp">foo</samp>’ is found. If such line is found,
|
|
<code class="command">sed</code> will terminate with exit status 42.
|
|
If such line was not found (and no other error occurred), <code class="command">sed</code>
|
|
will exit with status 0.
|
|
<code class="code">/^foo/</code> is a regular-expression address.
|
|
<code class="command">q</code> is the quit command. <code class="code">42</code> is the command option.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '/^foo/q42' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
<a class="index-entry-id" id="index-multiple-sed-commands"></a>
|
|
<a class="index-entry-id" id="index-sed-commands_002c-multiple"></a>
|
|
<a class="index-entry-id" id="index-newline_002c-command-separator"></a>
|
|
<a class="index-entry-id" id="index-semicolons_002c-command-separator"></a>
|
|
<a class="index-entry-id" id="index-_003b_002c-command-separator"></a>
|
|
<a class="index-entry-id" id="index-_002de_002c-example-1"></a>
|
|
<a class="index-entry-id" id="index-_002df_002c-example-1"></a>
|
|
<p>Commands within a <var class="var">script</var> or <var class="var">script-file</var> can be
|
|
separated by semicolons (<code class="code">;</code>) or newlines (ASCII 10).
|
|
Multiple scripts can be specified with <samp class="option">-e</samp> or <samp class="option">-f</samp>
|
|
options.
|
|
</p>
|
|
<p>The following examples are all equivalent. They perform two <code class="command">sed</code>
|
|
operations: deleting any lines matching the regular expression <code class="code">/^foo/</code>,
|
|
and replacing all occurrences of the string ‘<samp class="samp">hello</samp>’ with ‘<samp class="samp">world</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '/^foo/d ; s/hello/world/g' input.txt > output.txt
|
|
|
|
sed -e '/^foo/d' -e 's/hello/world/g' input.txt > output.txt
|
|
|
|
echo '/^foo/d' > script.sed
|
|
echo 's/hello/world/g' >> script.sed
|
|
sed -f script.sed input.txt > output.txt
|
|
|
|
echo 's/hello/world/g' > script2.sed
|
|
sed -e '/^foo/d' -f script2.sed input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
<a class="index-entry-id" id="index-a_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-c_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-e_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-i_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-r_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-R_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-w_002c-and-semicolons"></a>
|
|
<a class="index-entry-id" id="index-W_002c-and-semicolons"></a>
|
|
<p>Commands <code class="command">a</code>, <code class="command">c</code>, <code class="command">e</code>, <code class="command">i</code>,
|
|
<code class="command">r</code>, <code class="command">R</code>, <code class="command">w</code>, <code class="command">W</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 class="var">script</var> or <var class="var">script-file</var>.
|
|
Commands can also be preceded with optional non-significant
|
|
whitespace characters.
|
|
See <a class="xref" href="#Multiple-commands-syntax">Multiple commands syntax</a>.
|
|
</p>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="sed-commands-list">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#The-_0022s_0022-Command" accesskey="n" rel="next">The <code class="code">s</code> Command</a>, Previous: <a href="#sed-script-overview" accesskey="p" rel="prev"><code class="command">sed</code> script overview</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="sed-commands-summary"><span>3.2 <code class="command">sed</code> commands summary<a class="copiable-link" href="#sed-commands-summary"> ¶</a></span></h3>
|
|
|
|
<p>The following commands are supported in GNU <code class="command">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 class="table">
|
|
<dt><code class="code">a\</code></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><p>Append <var class="var">text</var> after a line.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">a <var class="var">text</var></code></dt>
|
|
<dd><p>Append <var class="var">text</var> after a line (alternative syntax).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">b <var class="var">label</var></code></dt>
|
|
<dd><p>Branch unconditionally to <var class="var">label</var>.
|
|
The <var class="var">label</var> may be omitted, in which case the next cycle is started.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">c\</code></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><p>Replace (change) lines with <var class="var">text</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">c <var class="var">text</var></code></dt>
|
|
<dd><p>Replace (change) lines with <var class="var">text</var> (alternative syntax).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">d</code></dt>
|
|
<dd><p>Delete the pattern space;
|
|
immediately start next cycle.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">D</code></dt>
|
|
<dd><p>If pattern space contains newlines, delete text in the pattern
|
|
space up to the first newline, and restart cycle with the resultant
|
|
pattern space, without reading a new line of input.
|
|
</p>
|
|
<p>If pattern space contains no newline, start a normal new cycle as if
|
|
the <code class="code">d</code> command was issued.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">e</code></dt>
|
|
<dd><p>Executes the command that is found in pattern space and
|
|
replaces the pattern space with the output; a trailing newline
|
|
is suppressed.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">e <var class="var">command</var></code></dt>
|
|
<dd><p>Executes <var class="var">command</var> and sends its output to the output stream.
|
|
The command can run across multiple lines, all but the last ending with
|
|
a back-slash.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">F</code></dt>
|
|
<dd><p>(filename) Print the file name of the current input file (with a trailing
|
|
newline).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">g</code></dt>
|
|
<dd><p>Replace the contents of the pattern space with the contents of the hold space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">G</code></dt>
|
|
<dd><p>Append a newline to the contents of the pattern space,
|
|
and then append the contents of the hold space to that of the pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">h</code></dt>
|
|
<dd><p>(hold) Replace the contents of the hold space with the contents of the
|
|
pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">H</code></dt>
|
|
<dd><p>Append a newline to the contents of the hold space,
|
|
and then append the contents of the pattern space to that of the hold space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">i\</code></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><p>insert <var class="var">text</var> before a line.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">i <var class="var">text</var></code></dt>
|
|
<dd><p>insert <var class="var">text</var> before a line (alternative syntax).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">l</code></dt>
|
|
<dd><p>Print the pattern space in an unambiguous form.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">n</code></dt>
|
|
<dd><p>(next) If auto-print is not disabled, print the pattern space,
|
|
then, regardless, replace the pattern space with the next line of input.
|
|
If there is no more input then <code class="command">sed</code> exits without processing
|
|
any more commands.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">N</code></dt>
|
|
<dd><p>Add a newline to the pattern space,
|
|
then append the next line of input to the pattern space.
|
|
If there is no more input then <code class="command">sed</code> exits without processing
|
|
any more commands.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">p</code></dt>
|
|
<dd><p>Print the pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">P</code></dt>
|
|
<dd><p>Print the pattern space, up to the first <newline>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">q<var class="var">[exit-code]</var></code></dt>
|
|
<dd><p>(quit) Exit <code class="command">sed</code> without processing any more commands or input.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">Q<var class="var">[exit-code]</var></code></dt>
|
|
<dd><p>(quit) This command is the same as <code class="code">q</code>, but will not print the
|
|
contents of pattern space. Like <code class="code">q</code>, it provides the
|
|
ability to return an exit code to the caller.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">r filename</code></dt>
|
|
<dd><p>Reads file <var class="var">filename</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">R filename</code></dt>
|
|
<dd><p>Queue a line of <var class="var">filename</var> to be read and
|
|
inserted into the output stream at the end of the current cycle,
|
|
or when the next input line is read.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">s<var class="var">/regexp/replacement/[flags]</var></code></dt>
|
|
<dd><p>(substitute) Match the regular-expression against the content of the
|
|
pattern space. If found, replace matched string with
|
|
<var class="var">replacement</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">t <var class="var">label</var></code></dt>
|
|
<dd><p>(test) Branch to <var class="var">label</var> only if there has been a successful
|
|
<code class="code">s</code>ubstitution since the last input line was read or conditional
|
|
branch was taken. The <var class="var">label</var> may be omitted, in which case the
|
|
next cycle is started.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">T <var class="var">label</var></code></dt>
|
|
<dd><p>(test) Branch to <var class="var">label</var> only if there have been no successful
|
|
<code class="code">s</code>ubstitutions since the last input line was read or
|
|
conditional branch was taken. The <var class="var">label</var> may be omitted,
|
|
in which case the next cycle is started.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">v <var class="var">[version]</var></code></dt>
|
|
<dd><p>(version) This command does nothing, but makes <code class="command">sed</code> fail if
|
|
GNU <code class="command">sed</code> extensions are not supported, or if the requested version
|
|
is not available.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">w filename</code></dt>
|
|
<dd><p>Write the pattern space to <var class="var">filename</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">W filename</code></dt>
|
|
<dd><p>Write to the given filename the portion of the pattern space up to
|
|
the first newline
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">x</code></dt>
|
|
<dd><p>Exchange the contents of the hold and pattern spaces.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><code class="code">y/src/dst/</code></dt>
|
|
<dd><p>Transliterate any characters in the pattern space which match
|
|
any of the <var class="var">source-chars</var> with the corresponding character
|
|
in <var class="var">dest-chars</var>.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><code class="code">z</code></dt>
|
|
<dd><p>(zap) This command empties the content of pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">#</code></dt>
|
|
<dd><p>A comment, until the next newline.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><code class="code">{ <var class="var">cmd ; cmd ...</var> }</code></dt>
|
|
<dd><p>Group several commands together.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">=</code></dt>
|
|
<dd><p>Print the current input line number (with a trailing newline).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">: <var class="var">label</var></code></dt>
|
|
<dd><p>Specify the location of <var class="var">label</var> for branch commands (<code class="code">b</code>,
|
|
<code class="code">t</code>, <code class="code">T</code>).
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="The-_0022s_0022-Command">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code> commands summary</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="The-s-Command"><span>3.3 The <code class="code">s</code> Command<a class="copiable-link" href="#The-s-Command"> ¶</a></span></h3>
|
|
|
|
<p>The <code class="code">s</code> command (as in substitute) is probably the most important
|
|
in <code class="command">sed</code> and has a lot of different options. The syntax of
|
|
the <code class="code">s</code> command is
|
|
‘<samp class="samp">s/<var class="var">regexp</var>/<var class="var">replacement</var>/<var class="var">flags</var></samp>’.
|
|
</p>
|
|
<p>Its basic concept is simple: the <code class="code">s</code> command attempts to match
|
|
the pattern space against the supplied regular expression <var class="var">regexp</var>;
|
|
if the match is successful, then that portion of the
|
|
pattern space which was matched is replaced with <var class="var">replacement</var>.
|
|
</p>
|
|
<p>For details about <var class="var">regexp</var> syntax see <a class="pxref" href="#Regexp-Addresses">Regular
|
|
Expression Addresses</a>.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Backreferences_002c-in-regular-expressions"></a>
|
|
<a class="index-entry-id" id="index-Parenthesized-substrings"></a>
|
|
<p>The <var class="var">replacement</var> can contain <code class="code">\<var class="var">n</var></code> (<var class="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 class="var">n</var>th
|
|
<code class="code">\(</code> and its matching <code class="code">\)</code>.
|
|
Also, the <var class="var">replacement</var> can contain unescaped <code class="code">&</code>
|
|
characters which reference the whole matched portion
|
|
of the pattern space.
|
|
</p>
|
|
|
|
<p>The <code class="code">/</code>
|
|
characters may be uniformly replaced by any other single
|
|
character within any given <code class="code">s</code> command. The <code class="code">/</code>
|
|
character (or whatever other character is used in its stead)
|
|
can appear in the <var class="var">regexp</var> or <var class="var">replacement</var>
|
|
only if it is preceded by a <code class="code">\</code> character.
|
|
</p>
|
|
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-case-modifiers-in-s-commands"></a>
|
|
<p>Finally, as a GNU <code class="command">sed</code> extension, you can include a
|
|
special sequence made of a backslash and one of the letters
|
|
<code class="code">L</code>, <code class="code">l</code>, <code class="code">U</code>, <code class="code">u</code>, or <code class="code">E</code>.
|
|
The meaning is as follows:
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code">\L</code></dt>
|
|
<dd><p>Turn the replacement
|
|
to lowercase until a <code class="code">\U</code> or <code class="code">\E</code> is found,
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\l</code></dt>
|
|
<dd><p>Turn the
|
|
next character to lowercase,
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\U</code></dt>
|
|
<dd><p>Turn the replacement to uppercase
|
|
until a <code class="code">\L</code> or <code class="code">\E</code> is found,
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\u</code></dt>
|
|
<dd><p>Turn the next character
|
|
to uppercase,
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\E</code></dt>
|
|
<dd><p>Stop case conversion started by <code class="code">\L</code> or <code class="code">\U</code>.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<p>When the <code class="code">g</code> flag is being used, case conversion does not
|
|
propagate from one occurrence of the regular expression to
|
|
another. For example, when the following command is executed
|
|
with ‘<samp class="samp">a-b-</samp>’ in pattern space:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">s/\(b\?\)-/x\u\1/g
|
|
</pre></div>
|
|
|
|
<p>the output is ‘<samp class="samp">axxB</samp>’. When replacing the first ‘<samp class="samp">-</samp>’,
|
|
the ‘<samp class="samp">\u</samp>’ sequence only affects the empty replacement of
|
|
‘<samp class="samp">\1</samp>’. It does not affect the <code class="code">x</code> character that is
|
|
added to pattern space when replacing <code class="code">b-</code> with <code class="code">xB</code>.
|
|
</p>
|
|
<p>On the other hand, <code class="code">\l</code> and <code class="code">\u</code> do affect the remainder
|
|
of the replacement text if they are followed by an empty substitution.
|
|
With ‘<samp class="samp">a-b-</samp>’ in pattern space, the following command:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">s/\(b\?\)-/\u\1x/g
|
|
</pre></div>
|
|
|
|
<p>will replace ‘<samp class="samp">-</samp>’ with ‘<samp class="samp">X</samp>’ (uppercase) and ‘<samp class="samp">b-</samp>’ with
|
|
‘<samp class="samp">Bx</samp>’. If this behavior is undesirable, you can prevent it by
|
|
adding a ‘<samp class="samp">\E</samp>’ sequence—after ‘<samp class="samp">\1</samp>’ in this case.
|
|
</p>
|
|
<p>To include a literal <code class="code">\</code>, <code class="code">&</code>, or newline in the final
|
|
replacement, be sure to precede the desired <code class="code">\</code>, <code class="code">&</code>,
|
|
or newline in the <var class="var">replacement</var> with a <code class="code">\</code>.
|
|
</p>
|
|
<a class="index-entry-id" id="index-s-command_002c-option-flags"></a>
|
|
<a class="index-entry-id" id="index-Substitution-of-text_002c-options"></a>
|
|
<p>The <code class="code">s</code> command can be followed by zero or more of the
|
|
following <var class="var">flags</var>:
|
|
</p>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Replacing-all-text-matching-regexp-in-a-line"></a>
|
|
<a id="index-Global-substitution"></a><span><code class="code">g</code><a class="copiable-link" href="#index-Global-substitution"> ¶</a></span></dt>
|
|
<dd><p>Apply the replacement to <em class="emph">all</em> matches to the <var class="var">regexp</var>,
|
|
not just the first.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-Replacing-only-nth-match-of-regexp-in-a-line"></a><span><code class="code"><var class="var">number</var></code><a class="copiable-link" href="#index-Replacing-only-nth-match-of-regexp-in-a-line"> ¶</a></span></dt>
|
|
<dd><p>Only replace the <var class="var">number</var>th match of the <var class="var">regexp</var>.
|
|
</p>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-g-and-number-modifier"></a>
|
|
<p>interaction in <code class="code">s</code> command
|
|
<a class="index-entry-id" id="index-Mixing-g-and-number-modifiers-in-the-s-command"></a>
|
|
Note: the <small class="sc">POSIX</small> standard does not specify what should happen
|
|
when you mix the <code class="code">g</code> and <var class="var">number</var> modifiers,
|
|
and currently there is no widely agreed upon meaning
|
|
across <code class="command">sed</code> implementations.
|
|
For GNU <code class="command">sed</code>, the interaction is defined to be:
|
|
ignore matches before the <var class="var">number</var>th,
|
|
and then match and replace all matches from
|
|
the <var class="var">number</var>th on.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-Text_002c-printing-after-substitution"></a><span><code class="code">p</code><a class="copiable-link" href="#index-Text_002c-printing-after-substitution"> ¶</a></span></dt>
|
|
<dd><p>If the substitution was made, then print the new pattern space.
|
|
</p>
|
|
<p>Note: when both the <code class="code">p</code> and <code class="code">e</code> options are specified,
|
|
the relative ordering of the two produces very different results.
|
|
In general, <code class="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 class="command">sed</code> interprets
|
|
specially the presence of <code class="code">p</code> options both before and after
|
|
<code class="code">e</code>, printing the pattern space before and after evaluation,
|
|
while in general flags for the <code class="code">s</code> command show their
|
|
effect just once. This behavior, although documented, might
|
|
change in future versions.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstdout-file-1"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstderr-file"></a>
|
|
<a id="index-Text_002c-writing-to-a-file-after-substitution"></a><span><code class="code">w <var class="var">filename</var></code><a class="copiable-link" href="#index-Text_002c-writing-to-a-file-after-substitution"> ¶</a></span></dt>
|
|
<dd><p>If the substitution was made, then write out the result to the named file.
|
|
As a GNU <code class="command">sed</code> extension, two special values of <var class="var">filename</var> are
|
|
supported: <samp class="file">/dev/stderr</samp>, which writes the result to the standard
|
|
error, and <samp class="file">/dev/stdout</samp>, which writes to the standard
|
|
output.<a class="footnote" id="DOCF3" href="#FOOT3"><sup>3</sup></a>
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Subprocesses"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-subprocesses"></a>
|
|
<a id="index-Evaluate-Bourne_002dshell-commands_002c-after-substitution"></a><span><code class="code">e</code><a class="copiable-link" href="#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution"> ¶</a></span></dt>
|
|
<dd><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 class="sc">NUL</small> character. This is a GNU <code class="command">sed</code> extension.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Case_002dinsensitive-matching"></a>
|
|
<a id="index-GNU-extensions_002c-I-modifier"></a><span><code class="code">I</code><a class="copiable-link" href="#index-GNU-extensions_002c-I-modifier"> ¶</a></span></dt>
|
|
<dt><code class="code">i</code></dt>
|
|
<dd><p>The <code class="code">I</code> modifier to regular-expression matching is a GNU
|
|
extension which makes <code class="command">sed</code> match <var class="var">regexp</var> in a
|
|
case-insensitive manner.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-M-modifier"></a><span><code class="code">M</code><a class="copiable-link" href="#index-GNU-extensions_002c-M-modifier"> ¶</a></span></dt>
|
|
<dt><code class="code">m</code></dt>
|
|
<dd><p>The <code class="code">M</code> modifier to regular-expression matching is a GNU <code class="command">sed</code>
|
|
extension which directs GNU <code class="command">sed</code> to match the regular expression
|
|
in <cite class="cite">multi-line</cite> mode. The modifier causes <code class="code">^</code> and <code class="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 class="code">\`</code> and <code class="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-level-extent" id="Common-Commands">
|
|
<div class="nav-panel">
|
|
<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 class="code">s</code> Command</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Often_002dUsed-Commands"><span>3.4 Often-Used Commands<a class="copiable-link" href="#Often_002dUsed-Commands"> ¶</a></span></h3>
|
|
|
|
<p>If you use <code class="command">sed</code> at all, you will quite likely want to know
|
|
these commands.
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code">#</code></dt>
|
|
<dd><p>[No addresses allowed.]
|
|
</p>
|
|
<a class="index-entry-id" id="index-_0023-_0028comments_0029"></a>
|
|
<a class="index-entry-id" id="index-Comments_002c-in-scripts"></a>
|
|
<p>The <code class="code">#</code> character begins a comment;
|
|
the comment continues until the next newline.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Portability_002c-comments"></a>
|
|
<p>If you are concerned about portability, be aware that
|
|
some implementations of <code class="command">sed</code> (which are not <small class="sc">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 class="code">#</code>.
|
|
</p>
|
|
<a class="index-entry-id" id="index-_002dn_002c-forcing-from-within-a-script"></a>
|
|
<a class="index-entry-id" id="index-Caveat-_002d_002d_002d-_0023n-on-first-line"></a>
|
|
<p>Warning: if the first two characters of the <code class="command">sed</code> script
|
|
are <code class="code">#n</code>, then the <samp class="option">-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 ‘<samp class="samp">n</samp>’
|
|
and you do not want this behavior,
|
|
then be sure to either use a capital ‘<samp class="samp">N</samp>’,
|
|
or place at least one space before the ‘<samp class="samp">n</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-returning-an-exit-code"></a>
|
|
<a class="index-entry-id" id="index-Quitting"></a>
|
|
<a id="index-q-_0028quit_0029-command"></a><span><code class="code">q [<var class="var">exit-code</var>]</code><a class="copiable-link" href="#index-q-_0028quit_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Exit <code class="command">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-preformatted">$ 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 class="option">-n</samp> options. The ability to return
|
|
an exit code from the <code class="command">sed</code> script is a GNU <code class="command">sed</code> extension.
|
|
</p>
|
|
<p>See also the GNU <code class="command">sed</code> extension <code class="code">Q</code> command which quits silently
|
|
without printing the current pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Text_002c-deleting"></a>
|
|
<a id="index-d-_0028delete_0029-command"></a><span><code class="code">d</code><a class="copiable-link" href="#index-d-_0028delete_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Delete the pattern space;
|
|
immediately start next cycle.
|
|
</p>
|
|
<p>Example: delete the second input line:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed 2d
|
|
1
|
|
3
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Text_002c-printing"></a>
|
|
<a id="index-p-_0028print_0029-command"></a><span><code class="code">p</code><a class="copiable-link" href="#index-p-_0028print_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Print out the pattern space (to the standard output).
|
|
This command is usually only used in conjunction with the <samp class="option">-n</samp>
|
|
command-line option.
|
|
</p>
|
|
<p>Example: print only the second input line:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed -n 2p
|
|
2
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Next-input-line_002c-replace-pattern-space-with"></a>
|
|
<a class="index-entry-id" id="index-Read-next-input-line"></a>
|
|
<a id="index-n-_0028next_002dline_0029-command"></a><span><code class="code">n</code><a class="copiable-link" href="#index-n-_0028next_002dline_0029-command"> ¶</a></span></dt>
|
|
<dd><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 class="command">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 class="code">n</code> commands
|
|
skip two lines):
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 6 | sed 'n;n;s/./x/'
|
|
1
|
|
2
|
|
x
|
|
4
|
|
5
|
|
x
|
|
</pre></div>
|
|
|
|
<p>GNU <code class="command">sed</code> provides an extension address syntax of <var class="var">first</var>~<var class="var">step</var>
|
|
to achieve the same result:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ seq 6 | sed '0~3s/./x/'
|
|
1
|
|
2
|
|
x
|
|
4
|
|
5
|
|
x
|
|
</pre></div>
|
|
|
|
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Grouping-commands"></a>
|
|
<a class="index-entry-id" id="index-Command-groups"></a>
|
|
<a id="index-_007b_007d-command-grouping"></a><span><code class="code">{ <var class="var">commands</var> }</code><a class="copiable-link" href="#index-_007b_007d-command-grouping"> ¶</a></span></dt>
|
|
<dd><p>A group of commands may be enclosed between
|
|
<code class="code">{</code> and <code class="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-preformatted">$ seq 3 | sed -n '2{s/2/X/ ; p}'
|
|
X
|
|
</pre></div>
|
|
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Other-Commands">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Programming-Commands" accesskey="n" rel="next">Commands for <code class="command">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 class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Less-Frequently_002dUsed-Commands"><span>3.5 Less Frequently-Used Commands<a class="copiable-link" href="#Less-Frequently_002dUsed-Commands"> ¶</a></span></h3>
|
|
|
|
<p>Though perhaps less frequently used than those in the previous
|
|
section, some very small yet useful <code class="command">sed</code> scripts can be built with
|
|
these commands.
|
|
</p>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Transliteration"></a>
|
|
<a id="index-y-_0028transliterate_0029-command"></a><span><code class="code">y/<var class="var">source-chars</var>/<var class="var">dest-chars</var>/</code><a class="copiable-link" href="#index-y-_0028transliterate_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Transliterate any characters in the pattern space which match
|
|
any of the <var class="var">source-chars</var> with the corresponding character
|
|
in <var class="var">dest-chars</var>.
|
|
</p>
|
|
<p>Example: transliterate ‘<samp class="samp">a-j</samp>’ into ‘<samp class="samp">0-9</samp>’:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ echo hello world | sed 'y/abcdefghij/0123456789/'
|
|
74llo worl3
|
|
</pre></div>
|
|
|
|
<p>(The <code class="code">/</code> characters may be uniformly replaced by
|
|
any other single character within any given <code class="code">y</code> command.)
|
|
</p>
|
|
<p>Instances of the <code class="code">/</code> (or whatever other character is used in its stead),
|
|
<code class="code">\</code>, or newlines can appear in the <var class="var">source-chars</var> or <var class="var">dest-chars</var>
|
|
lists, provide that each instance is escaped by a <code class="code">\</code>.
|
|
The <var class="var">source-chars</var> and <var class="var">dest-chars</var> lists <em class="emph">must</em>
|
|
contain the same number of characters (after de-escaping).
|
|
</p>
|
|
<p>See the <code class="command">tr</code> command from GNU coreutils for similar functionality.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">a <var class="var">text</var></code></dt>
|
|
<dd><p>Appending <var class="var">text</var> after a line. This is a GNU extension
|
|
to the standard <code class="code">a</code> command - see below for details.
|
|
</p>
|
|
<p>Example: Add ‘<samp class="samp">hello</samp>’ after the second line:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2a hello'
|
|
1
|
|
2
|
|
hello
|
|
3
|
|
</pre></div>
|
|
|
|
<p>Leading whitespace after the <code class="code">a</code> command is ignored.
|
|
The text to add is read until the end of the line.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Appending-text-after-a-line"></a>
|
|
<a class="index-entry-id" id="index-Text_002c-appending"></a>
|
|
<a id="index-a-_0028append-text-lines_0029-command"></a><span><code class="code">a\</code><a class="copiable-link" href="#index-a-_0028append-text-lines_0029-command"> ¶</a></span></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><p>Appending <var class="var">text</var> after a line.
|
|
</p>
|
|
<p>Example: Add ‘<samp class="samp">hello</samp>’ after the second line
|
|
(-| indicates printed output lines):
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2a\
|
|
hello'
|
|
-|1
|
|
-|2
|
|
-|hello
|
|
-|3
|
|
</pre></div>
|
|
|
|
<p>The <code class="code">a</code> command queues the lines of text which follow this command
|
|
(each but the last ending with a <code class="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>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands"></a>
|
|
<p>As a GNU extension, this command accepts two addresses.
|
|
</p>
|
|
<p>Escape sequences in <var class="var">text</var> are processed, so you should
|
|
use <code class="code">\\</code> in <var class="var">text</var> to print a single backslash.
|
|
</p>
|
|
<p>The commands resume after the last line without a backslash (<code class="code">\</code>) -
|
|
‘<samp class="samp">world</samp>’ in the following example:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2a\
|
|
hello\
|
|
world
|
|
3s/./X/'
|
|
-|1
|
|
-|2
|
|
-|hello
|
|
-|world
|
|
-|X
|
|
</pre></div>
|
|
|
|
<p>As a GNU extension, the <code class="code">a</code> command and <var class="var">text</var> can be
|
|
separated into two <code class="code">-e</code> parameters, enabling easier scripting:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed -e '2a\' -e hello
|
|
1
|
|
2
|
|
hello
|
|
3
|
|
|
|
$ sed -e '2a\' -e "$VAR"
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">i <var class="var">text</var></code></dt>
|
|
<dd><p>insert <var class="var">text</var> before a line. This is a GNU extension
|
|
to the standard <code class="code">i</code> command - see below for details.
|
|
</p>
|
|
<p>Example: Insert ‘<samp class="samp">hello</samp>’ before the second line:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2i hello'
|
|
1
|
|
hello
|
|
2
|
|
3
|
|
</pre></div>
|
|
|
|
<p>Leading whitespace after the <code class="code">i</code> command is ignored.
|
|
The text to add is read until the end of the line.
|
|
</p>
|
|
<a class="anchor" id="insert-command"></a></dd>
|
|
<dt><a class="index-entry-id" id="index-Inserting-text-before-a-line"></a>
|
|
<a class="index-entry-id" id="index-Text_002c-insertion"></a>
|
|
<a id="index-i-_0028insert-text-lines_0029-command"></a><span><code class="code">i\</code><a class="copiable-link" href="#index-i-_0028insert-text-lines_0029-command"> ¶</a></span></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><p>Immediately output the lines of text which follow this command.
|
|
</p>
|
|
<p>Example: Insert ‘<samp class="samp">hello</samp>’ before the second line
|
|
(-| indicates printed output lines):
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2i\
|
|
hello'
|
|
-|1
|
|
-|hello
|
|
-|2
|
|
-|3
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-1"></a>
|
|
<p>As a GNU extension, this command accepts two addresses.
|
|
</p>
|
|
<p>Escape sequences in <var class="var">text</var> are processed, so you should
|
|
use <code class="code">\\</code> in <var class="var">text</var> to print a single backslash.
|
|
</p>
|
|
<p>The commands resume after the last line without a backslash (<code class="code">\</code>) -
|
|
‘<samp class="samp">world</samp>’ in the following example:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2i\
|
|
hello\
|
|
world
|
|
s/./X/'
|
|
-|X
|
|
-|hello
|
|
-|world
|
|
-|X
|
|
-|X
|
|
</pre></div>
|
|
|
|
<p>As a GNU extension, the <code class="code">i</code> command and <var class="var">text</var> can be
|
|
separated into two <code class="code">-e</code> parameters, enabling easier scripting:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed -e '2i\' -e hello
|
|
1
|
|
hello
|
|
2
|
|
3
|
|
|
|
$ sed -e '2i\' -e "$VAR"
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">c <var class="var">text</var></code></dt>
|
|
<dd><p>Replaces the line(s) with <var class="var">text</var>. This is a GNU extension
|
|
to the standard <code class="code">c</code> command - see below for details.
|
|
</p>
|
|
<p>Example: Replace the 2nd to 9th lines with the word ‘<samp class="samp">hello</samp>’:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 10 | sed '2,9c hello'
|
|
1
|
|
hello
|
|
10
|
|
</pre></div>
|
|
|
|
<p>Leading whitespace after the <code class="code">c</code> command is ignored.
|
|
The text to add is read until the end of the line.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Replacing-selected-lines-with-other-text"></a>
|
|
<a id="index-c-_0028change-to-text-lines_0029-command"></a><span><code class="code">c\</code><a class="copiable-link" href="#index-c-_0028change-to-text-lines_0029-command"> ¶</a></span></dt>
|
|
<dt><code class="code"><var class="var">text</var></code></dt>
|
|
<dd><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 ‘<samp class="samp">hello</samp>’ and
|
|
‘<samp class="samp">world</samp>’ (-| indicates printed output lines):
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ 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 class="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-preformatted">$ seq 3 | sed '2c\
|
|
hello
|
|
s/./X/'
|
|
-|X
|
|
-|hello
|
|
-|X
|
|
</pre></div>
|
|
|
|
<p>As a GNU extension, the <code class="code">c</code> command and <var class="var">text</var> can be
|
|
separated into two <code class="code">-e</code> parameters, enabling easier scripting:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed -e '2c\' -e hello
|
|
1
|
|
hello
|
|
3
|
|
|
|
$ sed -e '2c\' -e "$VAR"
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Printing-line-number"></a>
|
|
<a class="index-entry-id" id="index-Line-number_002c-printing"></a>
|
|
<a id="index-_003d-_0028print-line-number_0029-command"></a><span><code class="code">=</code><a class="copiable-link" href="#index-_003d-_0028print-line-number_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Print out the current input line number (with a trailing newline).
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf '%s\n' aaa bbb ccc | sed =
|
|
1
|
|
aaa
|
|
2
|
|
bbb
|
|
3
|
|
ccc
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-2"></a>
|
|
<p>As a GNU extension, this command accepts two addresses.
|
|
</p>
|
|
|
|
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-List-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Printing-text-unambiguously"></a>
|
|
<a class="index-entry-id" id="index-Line-length_002c-setting-1"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-setting-line-length"></a>
|
|
<a id="index-l-_0028list-unambiguously_0029-command"></a><span><code class="code">l <var class="var">n</var></code><a class="copiable-link" href="#index-l-_0028list-unambiguously_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Print the pattern space in an unambiguous form:
|
|
non-printable characters (and the <code class="code">\</code> character)
|
|
are printed in C-style escaped form; long lines are split,
|
|
with a trailing <code class="code">\</code> character to indicate the split;
|
|
the end of each line is marked with a <code class="code">$</code>.
|
|
</p>
|
|
<p><var class="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 class="var">n</var>
|
|
parameter is a GNU <code class="command">sed</code> extension.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">r <var class="var">filename</var></code></dt>
|
|
<dd>
|
|
<a class="index-entry-id" id="index-r-_0028read-file_0029-command"></a>
|
|
<a class="index-entry-id" id="index-Read-text-from-a-file"></a>
|
|
<p>Reads file <var class="var">filename</var>. Example:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed '2r/etc/hostname'
|
|
1
|
|
2
|
|
fencepost.gnu.org
|
|
3
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstdin-file"></a>
|
|
<p>Queue the contents of <var class="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 class="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 class="command">sed</code> extension, the special value <samp class="file">/dev/stdin</samp>
|
|
is supported for the file name, which reads the contents of the
|
|
standard input.
|
|
</p>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-3"></a>
|
|
<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 class="command">sed</code> extension, the <code class="code">r</code> command accepts a zero address,
|
|
inserting a file <em class="emph">before</em> the first line of the input
|
|
see <a class="pxref" href="#Adding-a-header-to-multiple-files">Adding a header to multiple files</a>.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Write-to-a-file"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstdout-file-2"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstderr-file-1"></a>
|
|
<a id="index-w-_0028write-file_0029-command"></a><span><code class="code">w <var class="var">filename</var></code><a class="copiable-link" href="#index-w-_0028write-file_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Write the pattern space to <var class="var">filename</var>.
|
|
As a GNU <code class="command">sed</code> extension, two special values of <var class="var">filename</var> are
|
|
supported: <samp class="file">/dev/stderr</samp>, which writes the result to the standard
|
|
error, and <samp class="file">/dev/stdout</samp>, which writes to the standard
|
|
output.<a class="footnote" 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 class="code">w</code> commands (including instances of the <code class="code">w</code> flag
|
|
on successful <code class="code">s</code> commands) which refer to the same <var class="var">filename</var>
|
|
are output without closing and reopening the file.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Delete-first-line-from-pattern-space"></a>
|
|
<a id="index-D-_0028delete-first-line_0029-command"></a><span><code class="code">D</code><a class="copiable-link" href="#index-D-_0028delete-first-line_0029-command"> ¶</a></span></dt>
|
|
<dd><p>If pattern space contains no newline, start a normal new cycle as if
|
|
the <code class="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><a class="index-entry-id" id="index-Next-input-line_002c-append-to-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Append-next-input-line-to-pattern-space"></a>
|
|
<a id="index-N-_0028append-Next-line_0029-command"></a><span><code class="code">N</code><a class="copiable-link" href="#index-N-_0028append-Next-line_0029-command"> ¶</a></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 class="command">sed</code> exits without processing
|
|
any more commands.
|
|
</p>
|
|
<p>When <samp class="option">-z</samp> is used, a zero byte (the ascii ‘<samp class="samp">NUL</samp>’ character) is
|
|
added between the lines (instead of a new line).
|
|
</p>
|
|
<p>By default <code class="command">sed</code> does not terminate if there is no ’next’ input line.
|
|
This is a GNU extension which can be disabled with <samp class="option">--posix</samp>.
|
|
See <a class="xref" href="#N_005fcommand_005flast_005fline">N command on the last line</a>.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Print-first-line-from-pattern-space"></a>
|
|
<a id="index-P-_0028print-first-line_0029-command"></a><span><code class="code">P</code><a class="copiable-link" href="#index-P-_0028print-first-line_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Print out the portion of the pattern space up to the first newline.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Copy-pattern-space-into-hold-space"></a>
|
|
<a class="index-entry-id" id="index-Replace-hold-space-with-copy-of-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-copying-pattern-space-into"></a>
|
|
<a id="index-h-_0028hold_0029-command"></a><span><code class="code">h</code><a class="copiable-link" href="#index-h-_0028hold_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Replace the contents of the hold space with the contents of the pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Append-pattern-space-to-hold-space"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-appending-from-pattern-space"></a>
|
|
<a id="index-H-_0028append-Hold_0029-command"></a><span><code class="code">H</code><a class="copiable-link" href="#index-H-_0028append-Hold_0029-command"> ¶</a></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><a class="index-entry-id" id="index-Copy-hold-space-into-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Replace-pattern-space-with-copy-of-hold-space"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-copy-into-pattern-space"></a>
|
|
<a id="index-g-_0028get_0029-command"></a><span><code class="code">g</code><a class="copiable-link" href="#index-g-_0028get_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Replace the contents of the pattern space with the contents of the hold space.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Append-hold-space-to-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-appending-to-pattern-space"></a>
|
|
<a id="index-G-_0028appending-Get_0029-command"></a><span><code class="code">G</code><a class="copiable-link" href="#index-G-_0028appending-Get_0029-command"> ¶</a></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><a class="index-entry-id" id="index-Exchange-hold-space-with-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-exchange-with-pattern-space"></a>
|
|
<a id="index-x-_0028eXchange_0029-command"></a><span><code class="code">x</code><a class="copiable-link" href="#index-x-_0028eXchange_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Exchange the contents of the hold and pattern spaces.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Programming-Commands">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Extended-Commands" accesskey="n" rel="next">Commands Specific to GNU <code class="command">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 class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Commands-for-sed-gurus"><span>3.6 Commands for <code class="command">sed</code> gurus<a class="copiable-link" href="#Commands-for-sed-gurus"> ¶</a></span></h3>
|
|
|
|
<p>In most cases, use of these commands indicates that you are
|
|
probably better off programming in something like <code class="command">awk</code>
|
|
or Perl. But occasionally one is committed to sticking
|
|
with <code class="command">sed</code>, and these commands can enable one to write
|
|
quite convoluted scripts.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Flow-of-control-in-scripts"></a>
|
|
<dl class="table">
|
|
<dt><code class="code">: <var class="var">label</var></code></dt>
|
|
<dd><p>[No addresses allowed.]
|
|
</p>
|
|
<a class="index-entry-id" id="index-_003a-_0028label_0029-command"></a>
|
|
<a class="index-entry-id" id="index-Labels_002c-in-scripts"></a>
|
|
<p>Specify the location of <var class="var">label</var> for branch commands.
|
|
In all other respects, a no-op.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Branch-to-a-label_002c-unconditionally"></a>
|
|
<a class="index-entry-id" id="index-Goto_002c-in-scripts"></a>
|
|
<a id="index-b-_0028branch_0029-command"></a><span><code class="code">b <var class="var">label</var></code><a class="copiable-link" href="#index-b-_0028branch_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Unconditionally branch to <var class="var">label</var>.
|
|
The <var class="var">label</var> may be omitted, in which case the next cycle is started.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Branch-to-a-label_002c-if-s_002f_002f_002f-succeeded"></a>
|
|
<a class="index-entry-id" id="index-Conditional-branch"></a>
|
|
<a id="index-t-_0028test-and-branch-if-successful_0029-command"></a><span><code class="code">t <var class="var">label</var></code><a class="copiable-link" href="#index-t-_0028test-and-branch-if-successful_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Branch to <var class="var">label</var> only if there has been a successful <code class="code">s</code>ubstitution
|
|
since the last input line was read or conditional branch was taken.
|
|
The <var class="var">label</var> may be omitted, in which case the next cycle is started.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Extended-Commands">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code> gurus</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Commands-Specific-to-GNU-sed"><span>3.7 Commands Specific to GNU <code class="command">sed</code><a class="copiable-link" href="#Commands-Specific-to-GNU-sed"> ¶</a></span></h3>
|
|
|
|
<p>These commands are specific to GNU <code class="command">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 class="command">sed</code> extensions or to do tasks that are required
|
|
quite often, yet are unsupported by standard <code class="command">sed</code>s.
|
|
</p>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Evaluate-Bourne_002dshell-commands"></a>
|
|
<a class="index-entry-id" id="index-Subprocesses-1"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands-1"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-subprocesses-1"></a>
|
|
<a id="index-e-_0028evaluate_0029-command"></a><span><code class="code">e [<var class="var">command</var>]</code><a class="copiable-link" href="#index-e-_0028evaluate_0029-command"> ¶</a></span></dt>
|
|
<dd><p>This command allows one to pipe input from a shell command
|
|
into pattern space. Without parameters, the <code class="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 class="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 class="sc">NUL</small> character.
|
|
</p>
|
|
<p>Note that, unlike the <code class="code">r</code> command, the output of the command will
|
|
be printed immediately; the <code class="code">r</code> command instead delays the output
|
|
to the end of the current cycle.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Printing-file-name"></a>
|
|
<a class="index-entry-id" id="index-File-name_002c-printing"></a>
|
|
<a id="index-F-_0028File-name_0029-command"></a><span><code class="code">F</code><a class="copiable-link" href="#index-F-_0028File-name_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Print out the file name of the current input file (with a trailing
|
|
newline).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">Q [<var class="var">exit-code</var>]</code></dt>
|
|
<dd><p>This command accepts only one address.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Q-_0028silent-Quit_0029-command"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-quitting-silently"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-returning-an-exit-code-1"></a>
|
|
<a class="index-entry-id" id="index-Quitting-1"></a>
|
|
<p>This command is the same as <code class="code">q</code>, but will not print the
|
|
contents of pattern space. Like <code class="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 class="option">-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-preformatted">:eat
|
|
$d <i class="i"><span class="r">Quit silently on the last line</span></i>
|
|
N <i class="i"><span class="r">Read another line, silently</span></i>
|
|
g <i class="i"><span class="r">Overwrite pattern space each time to save memory</span></i>
|
|
b eat
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Read-text-from-a-file-1"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-reading-a-file-a-line-at-a-time"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-R-command"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstdin-file-1"></a>
|
|
<a id="index-R-_0028read-line_0029-command"></a><span><code class="code">R <var class="var">filename</var></code><a class="copiable-link" href="#index-R-_0028read-line_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Queue a line of <var class="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 class="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 class="code">r</code> command, the special value <samp class="file">/dev/stdin</samp>
|
|
is supported for the file name, which reads a line from the
|
|
standard input.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-branch-if-s_002f_002f_002f-failed"></a>
|
|
<a class="index-entry-id" id="index-Branch-to-a-label_002c-if-s_002f_002f_002f-failed"></a>
|
|
<a class="index-entry-id" id="index-Conditional-branch-1"></a>
|
|
<a id="index-T-_0028test-and-branch-if-failed_0029-command"></a><span><code class="code">T <var class="var">label</var></code><a class="copiable-link" href="#index-T-_0028test-and-branch-if-failed_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Branch to <var class="var">label</var> only if there have been no successful
|
|
<code class="code">s</code>ubstitutions since the last input line was read or
|
|
conditional branch was taken. The <var class="var">label</var> may be omitted,
|
|
in which case the next cycle is started.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-checking-for-their-presence"></a>
|
|
<a class="index-entry-id" id="index-Requiring-GNU-sed"></a>
|
|
<a id="index-v-_0028version_0029-command"></a><span><code class="code">v <var class="var">version</var></code><a class="copiable-link" href="#index-v-_0028version_0029-command"> ¶</a></span></dt>
|
|
<dd><p>This command does nothing, but makes <code class="command">sed</code> fail if
|
|
GNU <code class="command">sed</code> extensions are not supported, simply because other
|
|
versions of <code class="command">sed</code> do not implement it. In addition, you
|
|
can specify the version of <code class="command">sed</code> that your script
|
|
requires, such as <code class="code">4.0.5</code>. The default is <code class="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 class="env">POSIXLY_CORRECT</code> is set in the environment.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Write-first-line-to-a-file"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-writing-first-line-to-a-file"></a>
|
|
<a id="index-W-_0028write-first-line_0029-command"></a><span><code class="code">W <var class="var">filename</var></code><a class="copiable-link" href="#index-W-_0028write-first-line_0029-command"> ¶</a></span></dt>
|
|
<dd><p>Write to the given filename the portion of the pattern space up to
|
|
the first newline. Everything said under the <code class="code">w</code> command about
|
|
file handling holds here too.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-emptying-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-Emptying-pattern-space"></a>
|
|
<a id="index-z-_0028Zap_0029-command"></a><span><code class="code">z</code><a class="copiable-link" href="#index-z-_0028Zap_0029-command"> ¶</a></span></dt>
|
|
<dd><p>This command empties the content of pattern space. It is
|
|
usually the same as ‘<samp class="samp">s/.*//</samp>’, but is more efficient
|
|
and works in the presence of invalid multibyte sequences
|
|
in the input stream. <small class="sc">POSIX</small> mandates that such sequences
|
|
are <em class="emph">not</em> matched by ‘<samp class="samp">.</samp>’, so that there is no portable
|
|
way to clear <code class="command">sed</code>’s buffers in the middle of the
|
|
script in most multibyte locales (including UTF-8 locales).
|
|
</p></dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Multiple-commands-syntax">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Previous: <a href="#Extended-Commands" accesskey="p" rel="prev">Commands Specific to GNU <code class="command">sed</code></a>, Up: <a href="#sed-scripts" accesskey="u" rel="up"><code class="command">sed</code> scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Multiple-commands-syntax-1"><span>3.8 Multiple commands syntax<a class="copiable-link" href="#Multiple-commands-syntax-1"> ¶</a></span></h3>
|
|
|
|
|
|
<p>There are several methods to specify multiple commands in a <code class="command">sed</code>
|
|
program.
|
|
</p>
|
|
<p>Using newlines is most natural when running a sed script from a file
|
|
(using the <samp class="option">-f</samp> option).
|
|
</p>
|
|
<p>On the command line, all <code class="command">sed</code> commands may be separated by newlines.
|
|
Alternatively, you may specify each command as an argument to an <samp class="option">-e</samp>
|
|
option:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 6 | sed '1d
|
|
3d
|
|
5d'
|
|
2
|
|
4
|
|
6
|
|
|
|
$ seq 6 | sed -e 1d -e 3d -e 5d
|
|
2
|
|
4
|
|
6
|
|
</pre></div></div>
|
|
|
|
<p>A semicolon (‘<samp class="samp">;</samp>’) may be used to separate most simple commands:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 6 | sed '1d;3d;5d'
|
|
2
|
|
4
|
|
6
|
|
</pre></div></div>
|
|
|
|
<p>The <code class="code">{</code>,<code class="code">}</code>,<code class="code">b</code>,<code class="code">t</code>,<code class="code">T</code>,<code class="code">:</code> commands can
|
|
be separated with a semicolon (this is a non-portable GNU <code class="command">sed</code> extension).
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 4 | sed '{1d;3d}'
|
|
2
|
|
4
|
|
|
|
$ seq 6 | sed '{1d;3d};5d'
|
|
2
|
|
4
|
|
6
|
|
</pre></div></div>
|
|
|
|
<p>Labels used in <code class="code">b</code>,<code class="code">t</code>,<code class="code">T</code>,<code class="code">:</code> commands are read
|
|
until a semicolon. Leading and trailing whitespace is ignored. In
|
|
the examples below the label is ‘<samp class="samp">x</samp>’. The first example works
|
|
with GNU <code class="command">sed</code>. The second is a portable equivalent. For more
|
|
information about branching and labels see <a class="pxref" href="#Branching-and-flow-control">Branching and Flow Control</a>.
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
|
|
<ul class="mini-toc">
|
|
<li><a href="#Commands-Requiring-a-newline" accesskey="1">Commands Requiring a newline</a></li>
|
|
</ul>
|
|
<div class="subsection-level-extent" id="Commands-Requiring-a-newline">
|
|
<h4 class="subsection"><span>3.8.1 Commands Requiring a newline<a class="copiable-link" href="#Commands-Requiring-a-newline"> ¶</a></span></h4>
|
|
|
|
<p>The following commands cannot be separated by a semicolon and
|
|
require a newline:
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code">a</code>,<code class="code">c</code>,<code class="code">i</code> (append/change/insert)</dt>
|
|
<dd>
|
|
<p>All characters following <code class="code">a</code>,<code class="code">c</code>,<code class="code">i</code> commands are taken
|
|
as the text to append/change/insert. Using a semicolon leads to
|
|
undesirable results:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 2 | sed '1aHello ; 2d'
|
|
1
|
|
Hello ; 2d
|
|
2
|
|
</pre></div></div>
|
|
|
|
<p>Separate the commands using <samp class="option">-e</samp> or a newline:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 2 | sed -e 1aHello -e 2d
|
|
1
|
|
Hello
|
|
|
|
$ seq 2 | sed '1aHello
|
|
2d'
|
|
1
|
|
Hello
|
|
</pre></div></div>
|
|
|
|
<p>Note that specifying the text to add (‘<samp class="samp">Hello</samp>’) immediately
|
|
after <code class="code">a</code>,<code class="code">c</code>,<code class="code">i</code> is itself a GNU <code class="command">sed</code> extension.
|
|
A portable, POSIX-compliant alternative is:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 2 | sed '1a\
|
|
Hello
|
|
2d'
|
|
1
|
|
Hello
|
|
</pre></div></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">#</code> (comment)</dt>
|
|
<dd>
|
|
<p>All characters following ‘<samp class="samp">#</samp>’ until the next newline are ignored.
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 3 | sed '# this is a comment ; 2d'
|
|
1
|
|
2
|
|
3
|
|
|
|
|
|
$ seq 3 | sed '# this is a comment
|
|
2d'
|
|
1
|
|
3
|
|
</pre></div></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">r</code>,<code class="code">R</code>,<code class="code">w</code>,<code class="code">W</code> (reading and writing files)</dt>
|
|
<dd>
|
|
<p>The <code class="code">r</code>,<code class="code">R</code>,<code class="code">w</code>,<code class="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">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
<p>Note that <code class="command">sed</code> silently ignores read/write errors in
|
|
<code class="code">r</code>,<code class="code">R</code>,<code class="code">w</code>,<code class="code">W</code> commands (such as missing files).
|
|
In the following example, <code class="command">sed</code> tries to read a file named
|
|
‘<samp class="samp"><samp class="file">hello.txt ; N</samp></samp>’. The file is missing, and the error is silently
|
|
ignored:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ echo x | sed '1rhello.txt ; N'
|
|
x
|
|
</pre></div></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">e</code> (command execution)</dt>
|
|
<dd>
|
|
<p>Any characters following the <code class="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">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">s///[we]</code> (substitute with <code class="code">e</code> or <code class="code">w</code> flags)</dt>
|
|
<dd>
|
|
<p>In a substitution command, the <code class="code">w</code> flag writes the substitution
|
|
result to a file, and the <code class="code">e</code> flag executes the substitution result
|
|
as a shell command. As with the <code class="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">
|
|
<div class="group"><pre class="example-preformatted">$ echo a | sed 's/a/b/w1.txt#foo'
|
|
b
|
|
|
|
$ ls -1
|
|
1.txt#foo
|
|
</pre></div></div>
|
|
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="chapter-level-extent" id="sed-addresses">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code> scripts</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Addresses_003a-selecting-lines"><span>4 Addresses: selecting lines<a class="copiable-link" href="#Addresses_003a-selecting-lines"> ¶</a></span></h2>
|
|
|
|
|
|
<ul class="mini-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-level-extent" id="Addresses-overview">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Addresses-overview-1"><span>4.1 Addresses overview<a class="copiable-link" href="#Addresses-overview-1"> ¶</a></span></h3>
|
|
|
|
<a class="index-entry-id" id="index-addresses_002c-numeric"></a>
|
|
<a class="index-entry-id" id="index-numeric-addresses"></a>
|
|
<p>Addresses determine on which line(s) the <code class="command">sed</code> command will be
|
|
executed. The following command replaces any first occurrence of ‘<samp class="samp">hello</samp>’
|
|
with ‘<samp class="samp">world</samp>’ only on line 144:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '144s/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
<p>If no address is specified, the command is performed on all lines.
|
|
The following command replaces ‘<samp class="samp">hello</samp>’ with ‘<samp class="samp">world</samp>’,
|
|
targeting every line of the input file.
|
|
However, note that it modifies only the first instance of ‘<samp class="samp">hello</samp>’
|
|
on each line.
|
|
Use the ‘<samp class="samp">g</samp>’ modifier to affect every instance on each affected line.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed 's/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
<a class="index-entry-id" id="index-addresses_002c-regular-expression"></a>
|
|
<a class="index-entry-id" id="index-regular-expression-addresses"></a>
|
|
<p>Addresses can contain regular expressions to match lines based
|
|
on content instead of line numbers. The following command replaces
|
|
‘<samp class="samp">hello</samp>’ with ‘<samp class="samp">world</samp>’ only on lines
|
|
containing the string ‘<samp class="samp">apple</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '/apple/s/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
<a class="index-entry-id" id="index-addresses_002c-range"></a>
|
|
<a class="index-entry-id" id="index-range-addresses"></a>
|
|
<p>An address range is specified with two addresses separated by a comma
|
|
(<code class="code">,</code>). Addresses can be numeric, regular expressions, or a mix of
|
|
both.
|
|
The following command replaces ‘<samp class="samp">hello</samp>’ with ‘<samp class="samp">world</samp>’
|
|
only on lines 4 to 17 (inclusive):
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '4,17s/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
<a class="index-entry-id" id="index-Excluding-lines"></a>
|
|
<a class="index-entry-id" id="index-Selecting-non_002dmatching-lines"></a>
|
|
<a class="index-entry-id" id="index-addresses_002c-negating"></a>
|
|
<a class="index-entry-id" id="index-addresses_002c-excluding"></a>
|
|
<p>Appending the <code class="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 class="code">!</code> character follows an address or an
|
|
address range, then only lines which do <em class="emph">not</em> match the addresses
|
|
will be selected. The following command replaces ‘<samp class="samp">hello</samp>’
|
|
with ‘<samp class="samp">world</samp>’ only on lines <em class="emph">not</em> containing the string
|
|
‘<samp class="samp">apple</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed '/apple/!s/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
<p>The following command replaces ‘<samp class="samp">hello</samp>’ with
|
|
‘<samp class="samp">world</samp>’ 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-preformatted">sed '4,17!s/hello/world/' input.txt > output.txt
|
|
</pre></div>
|
|
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Numeric-Addresses">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Selecting-lines-by-numbers"><span>4.2 Selecting lines by numbers<a class="copiable-link" href="#Selecting-lines-by-numbers"> ¶</a></span></h3>
|
|
<a class="index-entry-id" id="index-Addresses_002c-in-sed-scripts"></a>
|
|
<a class="index-entry-id" id="index-Line-selection"></a>
|
|
<a class="index-entry-id" id="index-Selecting-lines-to-process"></a>
|
|
|
|
<p>Addresses in a <code class="command">sed</code> script can be in any of the following forms:
|
|
</p><dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Line_002c-selecting-by-number"></a>
|
|
<a id="index-Address_002c-numeric"></a><span><code class="code"><var class="var">number</var></code><a class="copiable-link" href="#index-Address_002c-numeric"> ¶</a></span></dt>
|
|
<dd><p>Specifying a line number will match only that line in the input.
|
|
(Note that <code class="command">sed</code> counts lines continuously across all input files
|
|
unless <samp class="option">-i</samp> or <samp class="option">-s</samp> options are specified.)
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Last-line_002c-selecting"></a>
|
|
<a class="index-entry-id" id="index-Line_002c-selecting-last"></a>
|
|
<a id="index-Address_002c-last-line"></a><span><code class="code">$</code><a class="copiable-link" href="#index-Address_002c-last-line"> ¶</a></span></dt>
|
|
<dd><p>This address matches the last line of the last file of input, or
|
|
the last line of each file when the <samp class="option">-i</samp> or <samp class="option">-s</samp> options
|
|
are specified.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-n_007em-addresses"></a><span><code class="code"><var class="var">first</var>~<var class="var">step</var></code><a class="copiable-link" href="#index-GNU-extensions_002c-n_007em-addresses"> ¶</a></span></dt>
|
|
<dd><p>This GNU extension matches every <var class="var">step</var>th line
|
|
starting with line <var class="var">first</var>.
|
|
In particular, lines will be selected when there exists
|
|
a non-negative <var class="var">n</var> such that the current line-number equals
|
|
<var class="var">first</var> + (<var class="var">n</var> * <var class="var">step</var>).
|
|
Thus, one would use <code class="code">1~2</code> to select the odd-numbered lines and
|
|
<code class="code">0~2</code> for even-numbered lines;
|
|
to pick every third line starting with the second, ‘<samp class="samp">2~3</samp>’ would be used;
|
|
to pick every fifth line starting with the tenth, use ‘<samp class="samp">10~5</samp>’;
|
|
and ‘<samp class="samp">50~0</samp>’ is just an obscure way of saying <code class="code">50</code>.
|
|
</p>
|
|
<p>The following commands demonstrate the step address usage:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ 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-level-extent" id="Regexp-Addresses">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="selecting-lines-by-text-matching"><span>4.3 selecting lines by text matching<a class="copiable-link" href="#selecting-lines-by-text-matching"> ¶</a></span></h3>
|
|
|
|
<p>GNU <code class="command">sed</code> supports the following regular expression addresses.
|
|
The default regular expression is
|
|
<a class="ref" href="#BRE-syntax">Basic Regular Expression (BRE)</a>.
|
|
If <samp class="option">-E</samp> or <samp class="option">-r</samp> options are used, The regular expression should be
|
|
in <a class="ref" href="#ERE-syntax">Extended Regular Expression (ERE)</a> syntax.
|
|
See <a class="xref" href="#BRE-vs-ERE">Basic (BRE) and extended (ERE) regular expression</a>.
|
|
</p>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-Line_002c-selecting-by-regular-expression-match"></a>
|
|
<a id="index-Address_002c-as-a-regular-expression"></a><span><code class="code">/<var class="var">regexp</var>/</code><a class="copiable-link" href="#index-Address_002c-as-a-regular-expression"> ¶</a></span></dt>
|
|
<dd><p>This will select any line which matches the regular expression <var class="var">regexp</var>.
|
|
If <var class="var">regexp</var> itself includes any <code class="code">/</code> characters,
|
|
each must be escaped by a backslash (<code class="code">\</code>).
|
|
</p>
|
|
<p>The following command prints lines in <samp class="file">/etc/passwd</samp>
|
|
which end with ‘<samp class="samp">bash</samp>’<a class="footnote" id="DOCF5" href="#FOOT5"><sup>5</sup></a>:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -n '/bash$/p' /etc/passwd
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-empty-regular-expression"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-modifiers-and-the-empty-regular-expression"></a>
|
|
<p>The empty regular expression ‘<samp class="samp">//</samp>’ repeats the last regular
|
|
expression match (the same holds if the empty regular expression is
|
|
passed to the <code class="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><code class="code">\%<var class="var">regexp</var>%</code></dt>
|
|
<dd><p>(The <code class="code">%</code> may be replaced by any other single character.)
|
|
</p>
|
|
<a class="index-entry-id" id="index-Slash-character_002c-in-regular-expressions"></a>
|
|
<p>This also matches the regular expression <var class="var">regexp</var>,
|
|
but allows one to use a different delimiter than <code class="code">/</code>.
|
|
This is particularly useful if the <var class="var">regexp</var> itself contains
|
|
a lot of slashes, since it avoids the tedious escaping of every <code class="code">/</code>.
|
|
If <var class="var">regexp</var> itself includes any delimiter characters,
|
|
each must be escaped by a backslash (<code class="code">\</code>).
|
|
</p>
|
|
<p>The following commands are equivalent. They print lines
|
|
which start with ‘<samp class="samp">/home/alice/documents/</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -n '/^\/home\/alice\/documents\//p'
|
|
sed -n '\%^/home/alice/documents/%p'
|
|
sed -n '\;^/home/alice/documents/;p'
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-case-insensitive_002c-regular-expression"></a>
|
|
<a id="index-GNU-extensions_002c-I-modifier-1"></a><span><code class="code">/<var class="var">regexp</var>/I</code><a class="copiable-link" href="#index-GNU-extensions_002c-I-modifier-1"> ¶</a></span></dt>
|
|
<dt><code class="code">\%<var class="var">regexp</var>%I</code></dt>
|
|
<dd><p>The <code class="code">I</code> modifier to regular-expression matching is a GNU
|
|
extension which causes the <var class="var">regexp</var> to be matched in
|
|
a case-insensitive manner.
|
|
</p>
|
|
<p>In many other programming languages, a lower case <code class="code">i</code> is used
|
|
for case-insensitive regular expression matching. However, in <code class="command">sed</code>
|
|
the <code class="code">i</code> is used for the insert command (see <a class="pxref" href="#insert-command">insert command</a>).
|
|
</p>
|
|
<p>Observe the difference between the following examples.
|
|
</p>
|
|
<p>In this example, <code class="code">/b/I</code> is the address: regular expression with <code class="code">I</code>
|
|
modifier. <code class="code">d</code> is the delete command:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf "%s\n" a b c | sed '/b/Id'
|
|
a
|
|
c
|
|
</pre></div>
|
|
|
|
<p>Here, <code class="code">/b/</code> is the address: a regular expression.
|
|
<code class="code">i</code> is the insert command.
|
|
<code class="code">d</code> is the value to insert.
|
|
A line with ‘<samp class="samp">d</samp>’ is then inserted above the matched line:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf "%s\n" a b c | sed '/b/id'
|
|
a
|
|
d
|
|
b
|
|
c
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-M-modifier-1"></a><span><code class="code">/<var class="var">regexp</var>/M</code><a class="copiable-link" href="#index-GNU-extensions_002c-M-modifier-1"> ¶</a></span></dt>
|
|
<dt><code class="code">\%<var class="var">regexp</var>%M</code></dt>
|
|
<dd><p>The <code class="code">M</code> modifier to regular-expression matching is a GNU <code class="command">sed</code>
|
|
extension which directs GNU <code class="command">sed</code> to match the regular expression
|
|
in <cite class="cite">multi-line</cite> mode. The modifier causes <code class="code">^</code> and <code class="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 class="code">\`</code> and <code class="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>
|
|
|
|
|
|
<a class="index-entry-id" id="index-regex-addresses-and-pattern-space"></a>
|
|
<a class="index-entry-id" id="index-regex-addresses-and-input-lines"></a>
|
|
<p>Regex addresses operate on the content of the current
|
|
pattern space. If the pattern space is changed (for example with <code class="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 class="option">-n</samp>. The <code class="code">s/2/X/</code> command changes lines containing
|
|
‘<samp class="samp">2</samp>’ to ‘<samp class="samp">X</samp>’. The command <code class="code">/[0-9]/p</code> matches
|
|
lines with digits and prints them.
|
|
Because the second line is changed before the <code class="code">/[0-9]/</code> regex,
|
|
it will not match and will not be printed:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 3 | sed -n 's/2/X/ ; /[0-9]/p'
|
|
1
|
|
3
|
|
</pre></div></div>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Range-Addresses">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Range-Addresses-1"><span>4.4 Range Addresses<a class="copiable-link" href="#Range-Addresses-1"> ¶</a></span></h3>
|
|
|
|
<a class="index-entry-id" id="index-Range-of-lines"></a>
|
|
<a class="index-entry-id" id="index-Several-lines_002c-selecting"></a>
|
|
<p>An address range can be specified by specifying two addresses
|
|
separated by a comma (<code class="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-preformatted">$ seq 10 | sed -n '4,6p'
|
|
4
|
|
5
|
|
6
|
|
</pre></div>
|
|
|
|
<p>If the second address is a <var class="var">regexp</var>, then checking for the
|
|
ending match will start with the line <em class="emph">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-preformatted">$ seq 10 | sed -n '4,/[0-9]/p'
|
|
4
|
|
5
|
|
</pre></div>
|
|
|
|
<p>If the second address is a <var class="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-preformatted">$ seq 10 | sed -n '4,1p'
|
|
4
|
|
</pre></div>
|
|
|
|
<a class="anchor" id="Zero-Address-Regex-Range"></a><a class="index-entry-id" id="index-Special-addressing-forms"></a>
|
|
<a class="index-entry-id" id="index-Range-with-start-address-of-zero"></a>
|
|
<a class="index-entry-id" id="index-Zero_002c-as-range-start-address"></a>
|
|
<a class="index-entry-id" id="index-addr1_002c_002bN"></a>
|
|
<a class="index-entry-id" id="index-addr1_002c_007eN"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-special-two_002daddress-forms"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-0-address"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-0_002caddr2-addressing"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-addr1_002c_002bN-addressing"></a>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-addr1_002c_007eN-addressing"></a>
|
|
<p>GNU <code class="command">sed</code> also supports some special two-address forms; all these
|
|
are GNU extensions:
|
|
</p><dl class="table">
|
|
<dt><code class="code">0,/<var class="var">regexp</var>/</code></dt>
|
|
<dd><p>A line number of <code class="code">0</code> can be used in an address specification like
|
|
<code class="code">0,/<var class="var">regexp</var>/</code> so that <code class="command">sed</code> will try to match
|
|
<var class="var">regexp</var> in the first input line too. In other words,
|
|
<code class="code">0,/<var class="var">regexp</var>/</code> is similar to <code class="code">1,/<var class="var">regexp</var>/</code>,
|
|
except that if <var class="var">addr2</var> matches the very first line of input the
|
|
<code class="code">0,/<var class="var">regexp</var>/</code> form will consider it to end the range, whereas
|
|
the <code class="code">1,/<var class="var">regexp</var>/</code> form will match the beginning of its range and
|
|
hence make the range span up to the <em class="emph">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-preformatted">$ seq 10 | sed -n '1,/[0-9]/p'
|
|
1
|
|
2
|
|
|
|
$ seq 10 | sed -n '0,/[0-9]/p'
|
|
1
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code"><var class="var">addr1</var>,+<var class="var">N</var></code></dt>
|
|
<dd><p>Matches <var class="var">addr1</var> and the <var class="var">N</var> lines following <var class="var">addr1</var>.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ seq 10 | sed -n '6,+2p'
|
|
6
|
|
7
|
|
8
|
|
</pre></div>
|
|
|
|
<p><var class="var">addr1</var> can be a line number or a regular expression.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code"><var class="var">addr1</var>,~<var class="var">N</var></code></dt>
|
|
<dd><p>Matches <var class="var">addr1</var> and the lines following <var class="var">addr1</var>
|
|
until the next line whose input line number is a multiple of <var class="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-preformatted">$ seq 10 | sed -n '6,~4p'
|
|
6
|
|
7
|
|
8
|
|
</pre></div>
|
|
|
|
<p><var class="var">addr1</var> can be a line number or a regular expression.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Zero-Address">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Zero-Address-1"><span>4.5 Zero Address<a class="copiable-link" href="#Zero-Address-1"> ¶</a></span></h3>
|
|
<a class="index-entry-id" id="index-Zero-Address"></a>
|
|
<p>As a GNU <code class="command">sed</code> extension, <code class="code">0</code> address can be used in these cases:
|
|
</p><ol class="enumerate">
|
|
<li> In a regex range address as <code class="code">0,/<var class="var">regexp</var>/</code>
|
|
(see <a class="pxref" href="#Zero-Address-Regex-Range">Zero Address Regex Range</a>).
|
|
</li><li> With the <code class="code">r</code> command, inserting a file before the first line
|
|
(see <a class="pxref" href="#Adding-a-header-to-multiple-files">Adding a header to multiple files</a>).
|
|
</li><li> As the starting address in the <var class="var">first</var>~<var class="var">step</var> extension
|
|
(see <a class="pxref" href="#Numeric-Addresses">Selecting lines by numbers</a>).
|
|
</li></ol>
|
|
|
|
<p>Note that these are the only places where the <code class="code">0</code> address makes
|
|
sense; Commands which are given the <code class="code">0</code> address in any
|
|
other way will give an error.
|
|
</p>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
<div class="chapter-level-extent" id="sed-regular-expressions">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#advanced-sed" accesskey="n" rel="next">Advanced <code class="command">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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Regular-Expressions_003a-selecting-text"><span>5 Regular Expressions: selecting text<a class="copiable-link" href="#Regular-Expressions_003a-selecting-text"> ¶</a></span></h2>
|
|
|
|
|
|
<ul class="mini-toc">
|
|
<li><a href="#Regular-Expressions-Overview" accesskey="1">Overview of regular expression in <code class="command">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-level-extent" id="Regular-Expressions-Overview">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Overview-of-regular-expression-in-sed"><span>5.1 Overview of regular expression in <code class="command">sed</code><a class="copiable-link" href="#Overview-of-regular-expression-in-sed"> ¶</a></span></h3>
|
|
|
|
|
|
|
|
<p>To know how to use <code class="command">sed</code>, people should understand regular
|
|
expressions (<em class="dfn">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 class="dfn">ordinary</em>: they stand for
|
|
themselves in a pattern, and match the corresponding characters.
|
|
Regular expressions in <code class="command">sed</code> are specified between two
|
|
slashes.
|
|
</p>
|
|
<p>The following command prints lines containing the string ‘<samp class="samp">hello</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -n '/hello/p'
|
|
</pre></div>
|
|
|
|
<p>The above example is equivalent to this <code class="command">grep</code> command:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">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 class="dfn">special characters</em>, which do not stand for
|
|
themselves but instead are interpreted in some special way.
|
|
</p>
|
|
<p>The character <code class="code">^</code> (caret) in a regular expression matches the
|
|
beginning of the line. The character <code class="code">.</code> (dot) matches any single
|
|
character. The following <code class="command">sed</code> command matches and prints
|
|
lines which start with the letter ‘<samp class="samp">b</samp>’, followed by any single character,
|
|
followed by the letter ‘<samp class="samp">d</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf "%s\n" 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-level-extent" id="BRE-vs-ERE">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">Regular Expressions: selecting text</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression"><span>5.2 Basic (BRE) and extended (ERE) regular expression<a class="copiable-link" href="#Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression"> ¶</a></span></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 class="command">sed</code> (and similarly in <code class="command">grep</code>).
|
|
Use the POSIX-specified <samp class="option">-E</samp> option (<samp class="option">-r</samp>,
|
|
<samp class="option">--regexp-extended</samp>) to enable Extended Regular Expression (ERE) syntax.
|
|
</p>
|
|
<p>In GNU <code class="command">sed</code>, the only difference between basic and extended regular
|
|
expressions is in the behavior of a few special characters: ‘<samp class="samp">?</samp>’,
|
|
‘<samp class="samp">+</samp>’, parentheses, braces (‘<samp class="samp">{}</samp>’), and ‘<samp class="samp">|</samp>’.
|
|
</p>
|
|
<p>With basic (BRE) syntax, these characters do not have special meaning
|
|
unless prefixed with a backslash (‘<samp class="samp">\</samp>’); While with extended (ERE) syntax
|
|
it is reversed: these characters are special unless they are prefixed
|
|
with backslash (‘<samp class="samp">\</samp>’).
|
|
</p>
|
|
<table class="multitable">
|
|
<thead><tr><th width="28%">Desired pattern</th><th width="36%">Basic (BRE) Syntax</th><th width="35%">Extended (ERE) Syntax</th></tr></thead>
|
|
<tbody><tr><td width="28%">literal ‘<samp class="samp">+</samp>’ (plus sign)</td><td width="36%"><div class="example">
|
|
<pre class="example-preformatted">$ echo 'a+b=c' > foo
|
|
$ sed -n '/a+b/p' foo
|
|
a+b=c
|
|
</pre></div></td><td width="35%"><div class="example">
|
|
<pre class="example-preformatted">$ echo 'a+b=c' > foo
|
|
$ sed -E -n '/a\+b/p' foo
|
|
a+b=c
|
|
</pre></div></td></tr>
|
|
<tr><td width="28%">One or more ‘<samp class="samp">a</samp>’ characters followed by ‘<samp class="samp">b</samp>’
|
|
(plus sign as special meta-character)</td><td width="36%"><div class="example">
|
|
<pre class="example-preformatted">$ echo aab > foo
|
|
$ sed -n '/a\+b/p' foo
|
|
aab
|
|
</pre></div></td><td width="35%"><div class="example">
|
|
<pre class="example-preformatted">$ echo aab > foo
|
|
$ sed -E -n '/a+b/p' foo
|
|
aab
|
|
</pre></div></td></tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="BRE-syntax">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Overview-of-basic-regular-expression-syntax"><span>5.3 Overview of basic regular expression syntax<a class="copiable-link" href="#Overview-of-basic-regular-expression-syntax"> ¶</a></span></h3>
|
|
|
|
<p>Here is a brief description
|
|
of regular expression syntax as used in <code class="command">sed</code>.
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code"><var class="var">char</var></code></dt>
|
|
<dd><p>A single ordinary character matches itself.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-to-basic-regular-expressions"></a><span><code class="code">*</code><a class="copiable-link" href="#index-GNU-extensions_002c-to-basic-regular-expressions"> ¶</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 class="code">\</code>, a <code class="code">.</code>, a grouped regexp
|
|
(see below), or a bracket expression. As a GNU extension (disabled by
|
|
<samp class="option">--posix</samp>), a postfixed regular expression can also be followed
|
|
by <code class="code">*</code>; for example, <code class="code">a**</code> is equivalent to <code class="code">a*</code>.
|
|
In extended regular expression mode (<samp class="option">-E</samp>), <code class="code">**</code> is also
|
|
accepted, but that is not considered a GNU extension (POSIX leaves it
|
|
undefined), so <samp class="option">--posix</samp> does not affect it. POSIX
|
|
1003.1-2001 says that <code class="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 class="code">\*</code> in these contexts.
|
|
</p></dd>
|
|
<dt><code class="code">.</code></dt>
|
|
<dd><p>Matches any character, including newline.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">^</code></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 class="pxref" href="#Execution-Cycle">How <code class="code">sed</code> works</a>). So, it is a
|
|
useful simplification to think of <code class="code">^#include</code> as matching only
|
|
lines where ‘<samp class="samp">#include</samp>’ is the first thing on the line—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 class="code">s</code> command.
|
|
</p>
|
|
<p><code class="code">^</code> acts as a special character only at the beginning of the
|
|
regular expression or subexpression (that is, after <code class="code">\(</code> or
|
|
<code class="code">\|</code>). Portable scripts should avoid <code class="code">^</code> at the beginning of
|
|
a subexpression, though, as POSIX allows implementations that
|
|
treat <code class="code">^</code> as an ordinary character in that context.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">$</code></dt>
|
|
<dd><p>It is the same as <code class="code">^</code>, but refers to end of pattern space.
|
|
<code class="code">$</code> also acts as a special character only at the end
|
|
of the regular expression or subexpression (that is, before <code class="code">\)</code>
|
|
or <code class="code">\|</code>), and its use at the end of a subexpression is not
|
|
portable.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><code class="code">[<var class="var">list</var>]</code></dt>
|
|
<dt><code class="code">[^<var class="var">list</var>]</code></dt>
|
|
<dd><p>Matches any single character in <var class="var">list</var>: for example,
|
|
<code class="code">[aeiou]</code> matches all vowels. A list may include
|
|
sequences like <code class="code"><var class="var">char1</var>-<var class="var">char2</var></code>, which
|
|
matches any character between (inclusive) <var class="var">char1</var>
|
|
and <var class="var">char2</var>.
|
|
See <a class="xref" href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a>.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-to-basic-regular-expressions-1"></a><span><code class="code">\+</code><a class="copiable-link" href="#index-GNU-extensions_002c-to-basic-regular-expressions-1"> ¶</a></span></dt>
|
|
<dd><p>As <code class="code">*</code>, but matches one or more. It is a GNU extension.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-GNU-extensions_002c-to-basic-regular-expressions-2"></a><span><code class="code">\?</code><a class="copiable-link" href="#index-GNU-extensions_002c-to-basic-regular-expressions-2"> ¶</a></span></dt>
|
|
<dd><p>As <code class="code">*</code>, but only matches zero or one. It is a GNU extension.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\{<var class="var">i</var>\}</code></dt>
|
|
<dd><p>As <code class="code">*</code>, but matches exactly <var class="var">i</var> sequences (<var class="var">i</var> is a
|
|
decimal integer; for portability, keep it between 0 and 255
|
|
inclusive).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\{<var class="var">i</var>,<var class="var">j</var>\}</code></dt>
|
|
<dd><p>Matches between <var class="var">i</var> and <var class="var">j</var>, inclusive, sequences.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\{<var class="var">i</var>,\}</code></dt>
|
|
<dd><p>Matches more than or equal to <var class="var">i</var> sequences.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\(<var class="var">regexp</var>\)</code></dt>
|
|
<dd><p>Groups the inner <var class="var">regexp</var> as a whole, this is used to:
|
|
</p>
|
|
<ul class="itemize mark-bullet">
|
|
<li><a class="index-entry-id" id="index-GNU-extensions_002c-to-basic-regular-expressions-3"></a>
|
|
Apply postfix operators, like <code class="code">\(abcd\)*</code>:
|
|
this will search for zero or more whole sequences
|
|
of ‘<samp class="samp">abcd</samp>’, while <code class="code">abcd*</code> would search
|
|
for ‘<samp class="samp">abc</samp>’ followed by zero or more occurrences
|
|
of ‘<samp class="samp">d</samp>’. Note that support for <code class="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><a id="index-GNU-extensions_002c-to-basic-regular-expressions-4"></a><span><code class="code"><var class="var">regexp1</var>\|<var class="var">regexp2</var></code><a class="copiable-link" href="#index-GNU-extensions_002c-to-basic-regular-expressions-4"> ¶</a></span></dt>
|
|
<dd><p>Matches either <var class="var">regexp1</var> or <var class="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><code class="code"><var class="var">regexp1</var><var class="var">regexp2</var></code></dt>
|
|
<dd><p>Matches the concatenation of <var class="var">regexp1</var> and <var class="var">regexp2</var>.
|
|
Concatenation binds more tightly than <code class="code">\|</code>, <code class="code">^</code>, and
|
|
<code class="code">$</code>, but less tightly than the other regular expression
|
|
operators.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\<var class="var">digit</var></code></dt>
|
|
<dd><p>Matches the <var class="var">digit</var>-th <code class="code">\(…\)</code> parenthesized
|
|
subexpression in the regular expression. This is called a <em class="dfn">back
|
|
reference</em>. Subexpressions are implicitly numbered by counting
|
|
occurrences of <code class="code">\(</code> left-to-right.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\n</code></dt>
|
|
<dd><p>Matches the newline character.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\<var class="var">char</var></code></dt>
|
|
<dd><p>Matches <var class="var">char</var>, where <var class="var">char</var> is one of <code class="code">$</code>,
|
|
<code class="code">*</code>, <code class="code">.</code>, <code class="code">[</code>, <code class="code">\</code>, or <code class="code">^</code>.
|
|
Note that the only C-like
|
|
backslash sequences that you can portably assume to be
|
|
interpreted are <code class="code">\n</code> and <code class="code">\\</code>; in particular
|
|
<code class="code">\t</code> is not portable, and matches a ‘<samp class="samp">t</samp>’ under most
|
|
implementations of <code class="command">sed</code>, rather than a tab character.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
<a class="index-entry-id" id="index-Greedy-regular-expression-matching"></a>
|
|
<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 class="table">
|
|
<dt>‘<samp class="samp">abcdef</samp>’</dt>
|
|
<dd><p>Matches ‘<samp class="samp">abcdef</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">a*b</samp>’</dt>
|
|
<dd><p>Matches zero or more ‘<samp class="samp">a</samp>’s followed by a single
|
|
‘<samp class="samp">b</samp>’. For example, ‘<samp class="samp">b</samp>’ or ‘<samp class="samp">aaaaab</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">a\?b</samp>’</dt>
|
|
<dd><p>Matches ‘<samp class="samp">b</samp>’ or ‘<samp class="samp">ab</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">a\+b\+</samp>’</dt>
|
|
<dd><p>Matches one or more ‘<samp class="samp">a</samp>’s followed by one or more
|
|
‘<samp class="samp">b</samp>’s: ‘<samp class="samp">ab</samp>’ is the shortest possible match, but
|
|
other examples are ‘<samp class="samp">aaaab</samp>’ or ‘<samp class="samp">abbbbb</samp>’ or
|
|
‘<samp class="samp">aaaaaabbbbbbb</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">.*</samp>’</dt>
|
|
<dt>‘<samp class="samp">.\+</samp>’</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>‘<samp class="samp">^main.*(.*)</samp>’</dt>
|
|
<dd><p>This matches a string starting with ‘<samp class="samp">main</samp>’,
|
|
followed by an opening and closing
|
|
parenthesis. The ‘<samp class="samp">n</samp>’, ‘<samp class="samp">(</samp>’ and ‘<samp class="samp">)</samp>’ need not
|
|
be adjacent.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">^#</samp>’</dt>
|
|
<dd><p>This matches a string beginning with ‘<samp class="samp">#</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">\\$</samp>’</dt>
|
|
<dd><p>This matches a string ending with a single backslash. The
|
|
regexp contains two backslashes for escaping.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">\$</samp>’</dt>
|
|
<dd><p>Instead, this matches a string consisting of a single dollar sign,
|
|
because it is escaped.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">[a-zA-Z0-9]</samp>’</dt>
|
|
<dd><p>In the C locale, this matches any ASCII letters or digits.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">[^ <kbd class="kbd"><kbd class="key">TAB</kbd></kbd>]\+</samp>’</dt>
|
|
<dd><p>(Here <kbd class="kbd"><kbd class="key">TAB</kbd></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>‘<samp class="samp">^\(.*\)\n\1$</samp>’</dt>
|
|
<dd><p>This matches a string consisting of two equal substrings separated by
|
|
a newline.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">.\{9\}A$</samp>’</dt>
|
|
<dd><p>This matches nine characters followed by an ‘<samp class="samp">A</samp>’ at the end of a line.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">^.\{15\}A</samp>’</dt>
|
|
<dd><p>This matches the start of a string that contains 16 characters,
|
|
the last of which is an ‘<samp class="samp">A</samp>’.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="ERE-syntax">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Overview-of-extended-regular-expression-syntax"><span>5.4 Overview of extended regular expression syntax<a class="copiable-link" href="#Overview-of-extended-regular-expression-syntax"> ¶</a></span></h3>
|
|
<a class="index-entry-id" id="index-Extended-regular-expressions_002c-syntax"></a>
|
|
|
|
<p>The only difference between basic and extended regular expressions is in
|
|
the behavior of a few characters: ‘<samp class="samp">?</samp>’, ‘<samp class="samp">+</samp>’, parentheses,
|
|
braces (‘<samp class="samp">{}</samp>’), and ‘<samp class="samp">|</samp>’. 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 class="emph">to match a literal character</em>. ‘<samp class="samp">|</samp>’
|
|
is special here because ‘<samp class="samp">\|</samp>’ is a GNU extension – standard
|
|
basic regular expressions do not provide its functionality.
|
|
</p>
|
|
<p>Examples:
|
|
</p><dl class="table">
|
|
<dt><code class="code">abc?</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">abc\?</samp>’ when using extended regular expressions. It matches
|
|
the literal string ‘<samp class="samp">abc?</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">c\+</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">c+</samp>’ when using extended regular expressions. It matches
|
|
one or more ‘<samp class="samp">c</samp>’s.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">a\{3,\}</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">a{3,}</samp>’ when using extended regular expressions. It matches
|
|
three or more ‘<samp class="samp">a</samp>’s.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\(abc\)\{2,3\}</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">(abc){2,3}</samp>’ when using extended regular expressions. It
|
|
matches either ‘<samp class="samp">abcabc</samp>’ or ‘<samp class="samp">abcabcabc</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\(abc*\)\1</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">(abc*)\1</samp>’ when using extended regular expressions.
|
|
Backreferences must still be escaped when using extended regular
|
|
expressions.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">a\|b</code></dt>
|
|
<dd><p>becomes ‘<samp class="samp">a|b</samp>’ when using extended regular expressions. It matches
|
|
‘<samp class="samp">a</samp>’ or ‘<samp class="samp">b</samp>’.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Character-Classes-and-Bracket-Expressions">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Character-Classes-and-Bracket-Expressions-1"><span>5.5 Character Classes and Bracket Expressions<a class="copiable-link" href="#Character-Classes-and-Bracket-Expressions-1"> ¶</a></span></h3>
|
|
|
|
|
|
<a class="index-entry-id" id="index-bracket-expression"></a>
|
|
<a class="index-entry-id" id="index-character-class"></a>
|
|
<p>A <em class="dfn">bracket expression</em> is a list of characters enclosed by ‘<samp class="samp">[</samp>’ and
|
|
‘<samp class="samp">]</samp>’.
|
|
It matches any single character in that list;
|
|
if the first character of the list is the caret ‘<samp class="samp">^</samp>’,
|
|
then it matches any character <strong class="strong">not</strong> in the list.
|
|
For example, the following command replaces the strings
|
|
‘<samp class="samp">gray</samp>’ or ‘<samp class="samp">grey</samp>’ with ‘<samp class="samp">blue</samp>’:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed 's/gr[ae]y/blue/'
|
|
</pre></div>
|
|
|
|
<p>Bracket expressions can be used in both
|
|
<a class="ref" href="#BRE-syntax">basic</a> and <a class="ref" href="#ERE-syntax">extended</a>
|
|
regular expressions (that is, with or without the <samp class="option">-E</samp>/<samp class="option">-r</samp>
|
|
options).
|
|
</p>
|
|
<a class="index-entry-id" id="index-range-expression"></a>
|
|
<p>Within a bracket expression, a <em class="dfn">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, ‘<samp class="samp">[a-d]</samp>’ is equivalent to ‘<samp class="samp">[abcd]</samp>’.
|
|
</p>
|
|
|
|
<p>Finally, certain named classes of characters are predefined within
|
|
bracket expressions, as follows.
|
|
</p>
|
|
<p>These named classes must be used <em class="emph">inside</em> brackets
|
|
themselves. Correct usage:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ echo 1 | sed 's/[[:digit:]]/X/'
|
|
X
|
|
</pre></div>
|
|
|
|
<p>Incorrect usage is rejected by newer <code class="command">sed</code> versions.
|
|
Older versions accepted it but treated it as a single bracket expression
|
|
(which is equivalent to ‘<samp class="samp">[dgit:]</samp>’,
|
|
that is, only the characters <var class="var">d/g/i/t/:</var>):
|
|
</p><div class="example">
|
|
<pre class="example-preformatted"># 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>
|
|
|
|
|
|
<a class="index-entry-id" id="index-classes-of-characters"></a>
|
|
<a class="index-entry-id" id="index-character-classes"></a>
|
|
<a class="index-entry-id" id="index-named-character-classes"></a>
|
|
<dl class="table">
|
|
<dt><a class="index-entry-id" id="index-alphanumeric-characters"></a>
|
|
<a id="index-alnum-character-class"></a><span>‘<samp class="samp">[:alnum:]</samp>’<a class="copiable-link" href="#index-alnum-character-class"> ¶</a></span></dt>
|
|
<dd><p>Alphanumeric characters:
|
|
‘<samp class="samp">[:alpha:]</samp>’ and ‘<samp class="samp">[:digit:]</samp>’; in the ‘<samp class="samp">C</samp>’ locale and ASCII
|
|
character encoding, this is the same as ‘<samp class="samp">[0-9A-Za-z]</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-alphabetic-characters"></a>
|
|
<a id="index-alpha-character-class"></a><span>‘<samp class="samp">[:alpha:]</samp>’<a class="copiable-link" href="#index-alpha-character-class"> ¶</a></span></dt>
|
|
<dd><p>Alphabetic characters:
|
|
‘<samp class="samp">[:lower:]</samp>’ and ‘<samp class="samp">[:upper:]</samp>’; in the ‘<samp class="samp">C</samp>’ locale and ASCII
|
|
character encoding, this is the same as ‘<samp class="samp">[A-Za-z]</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-blank-characters"></a>
|
|
<a id="index-blank-character-class"></a><span>‘<samp class="samp">[:blank:]</samp>’<a class="copiable-link" href="#index-blank-character-class"> ¶</a></span></dt>
|
|
<dd><p>Blank characters:
|
|
space and tab.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-control-characters"></a>
|
|
<a id="index-cntrl-character-class"></a><span>‘<samp class="samp">[:cntrl:]</samp>’<a class="copiable-link" href="#index-cntrl-character-class"> ¶</a></span></dt>
|
|
<dd><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><a class="index-entry-id" id="index-digit-characters"></a>
|
|
<a class="index-entry-id" id="index-numeric-characters"></a>
|
|
<a id="index-digit-character-class"></a><span>‘<samp class="samp">[:digit:]</samp>’<a class="copiable-link" href="#index-digit-character-class"> ¶</a></span></dt>
|
|
<dd><p>Digits: <code class="code">0 1 2 3 4 5 6 7 8 9</code>.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-graphic-characters"></a>
|
|
<a id="index-graph-character-class"></a><span>‘<samp class="samp">[:graph:]</samp>’<a class="copiable-link" href="#index-graph-character-class"> ¶</a></span></dt>
|
|
<dd><p>Graphical characters:
|
|
‘<samp class="samp">[:alnum:]</samp>’ and ‘<samp class="samp">[:punct:]</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-lower_002dcase-letters"></a>
|
|
<a id="index-lower-character-class"></a><span>‘<samp class="samp">[:lower:]</samp>’<a class="copiable-link" href="#index-lower-character-class"> ¶</a></span></dt>
|
|
<dd><p>Lower-case letters; in the ‘<samp class="samp">C</samp>’ locale and ASCII character
|
|
encoding, this is
|
|
<code class="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><a class="index-entry-id" id="index-printable-characters"></a>
|
|
<a id="index-print-character-class"></a><span>‘<samp class="samp">[:print:]</samp>’<a class="copiable-link" href="#index-print-character-class"> ¶</a></span></dt>
|
|
<dd><p>Printable characters:
|
|
‘<samp class="samp">[:alnum:]</samp>’, ‘<samp class="samp">[:punct:]</samp>’, and space.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-punctuation-characters"></a>
|
|
<a id="index-punct-character-class"></a><span>‘<samp class="samp">[:punct:]</samp>’<a class="copiable-link" href="#index-punct-character-class"> ¶</a></span></dt>
|
|
<dd><p>Punctuation characters; in the ‘<samp class="samp">C</samp>’ locale and ASCII character
|
|
encoding, this is
|
|
<code class="code">! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~</code>.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-space-characters"></a>
|
|
<a class="index-entry-id" id="index-whitespace-characters"></a>
|
|
<a id="index-space-character-class"></a><span>‘<samp class="samp">[:space:]</samp>’<a class="copiable-link" href="#index-space-character-class"> ¶</a></span></dt>
|
|
<dd><p>Space characters: in the ‘<samp class="samp">C</samp>’ locale, this is
|
|
tab, newline, vertical tab, form feed, carriage return, and space.
|
|
</p>
|
|
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-upper_002dcase-letters"></a>
|
|
<a id="index-upper-character-class"></a><span>‘<samp class="samp">[:upper:]</samp>’<a class="copiable-link" href="#index-upper-character-class"> ¶</a></span></dt>
|
|
<dd><p>Upper-case letters: in the ‘<samp class="samp">C</samp>’ locale and ASCII character
|
|
encoding, this is
|
|
<code class="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><a class="index-entry-id" id="index-xdigit-class"></a>
|
|
<a class="index-entry-id" id="index-hexadecimal-digits"></a>
|
|
<a id="index-xdigit-character-class"></a><span>‘<samp class="samp">[:xdigit:]</samp>’<a class="copiable-link" href="#index-xdigit-character-class"> ¶</a></span></dt>
|
|
<dd><p>Hexadecimal digits:
|
|
<code class="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 class="table">
|
|
<dt>‘<samp class="samp">]</samp>’</dt>
|
|
<dd><p>ends the bracket expression if it’s not the first list item.
|
|
So, if you want to make the ‘<samp class="samp">]</samp>’ character a list item,
|
|
you must put it first.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">-</samp>’</dt>
|
|
<dd><p>represents the range if it’s not first or last in a list or the ending point
|
|
of a range.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">^</samp>’</dt>
|
|
<dd><p>represents the characters not in the list.
|
|
If you want to make the ‘<samp class="samp">^</samp>’
|
|
character a list item, place it anywhere but first.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<p>TODO: incorporate this paragraph (copied verbatim from BRE section).
|
|
</p>
|
|
<a class="index-entry-id" id="index-POSIXLY_005fCORRECT-behavior_002c-bracket-expressions"></a>
|
|
<p>The characters <code class="code">$</code>, <code class="code">*</code>, <code class="code">.</code>, <code class="code">[</code>, and <code class="code">\</code>
|
|
are normally not special within <var class="var">list</var>. For example, <code class="code">[\*]</code>
|
|
matches either ‘<samp class="samp">\</samp>’ or ‘<samp class="samp">*</samp>’, because the <code class="code">\</code> is not
|
|
special here. However, strings like <code class="code">[.ch.]</code>, <code class="code">[=a=]</code>, and
|
|
<code class="code">[:space:]</code> are special within <var class="var">list</var> and represent collating
|
|
symbols, equivalence classes, and character classes, respectively, and
|
|
<code class="code">[</code> is therefore special within <var class="var">list</var> when it is followed by
|
|
<code class="code">.</code>, <code class="code">=</code>, or <code class="code">:</code>. Also, when not in
|
|
<code class="env">POSIXLY_CORRECT</code> mode, special escapes like <code class="code">\n</code> and
|
|
<code class="code">\t</code> are recognized within <var class="var">list</var>. See <a class="xref" href="#Escapes">Escape Sequences - specifying special characters</a>.
|
|
</p>
|
|
|
|
|
|
<dl class="table">
|
|
<dt>‘<samp class="samp">[.</samp>’</dt>
|
|
<dd><p>represents the open collating symbol.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">.]</samp>’</dt>
|
|
<dd><p>represents the close collating symbol.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">[=</samp>’</dt>
|
|
<dd><p>represents the open equivalence class.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">=]</samp>’</dt>
|
|
<dd><p>represents the close equivalence class.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">[:</samp>’</dt>
|
|
<dd><p>represents the open character class symbol, and should be followed by a
|
|
valid character class name.
|
|
</p>
|
|
</dd>
|
|
<dt>‘<samp class="samp">:]</samp>’</dt>
|
|
<dd><p>represents the close character class symbol.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="regexp-extensions">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="regular-expression-extensions"><span>5.6 regular expression extensions<a class="copiable-link" href="#regular-expression-extensions"> ¶</a></span></h3>
|
|
|
|
<p>The following sequences have special meaning inside regular expressions
|
|
(used in <a class="ref" href="#Regexp-Addresses">addresses</a> and the <code class="code">s</code> command).
|
|
</p>
|
|
<p>These can be used in both
|
|
<a class="ref" href="#BRE-syntax">basic</a> and <a class="ref" href="#ERE-syntax">extended</a>
|
|
regular expressions (that is, with or without the <samp class="option">-E</samp>/<samp class="option">-r</samp>
|
|
options).
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code">\w</code></dt>
|
|
<dd><p>Matches any “word” character. A “word” character is any
|
|
letter or digit or the underscore character.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\w/X/g'
|
|
XXX %-= XXX.
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\W</code></dt>
|
|
<dd><p>Matches any “non-word” character.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\W/X/g'
|
|
abcXXXXXdefX
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\b</code></dt>
|
|
<dd><p>Matches a word boundary; that is it matches if the character
|
|
to the left is a “word” character and the character to the
|
|
right is a “non-word” character, or vice-versa.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\b/X/g'
|
|
XabcX %-= XdefX.
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\B</code></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 “word” characters or both “non-word”
|
|
characters.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\B/X/g'
|
|
aXbXc X%X-X=X dXeXf.X
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\s</code></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-preformatted">$ echo "abc %-= def." | sed 's/\s/X/g'
|
|
abcX%-=Xdef.
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\S</code></dt>
|
|
<dd><p>Matches non-whitespace characters.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\S/X/g'
|
|
XXX XXX XXXX
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\<</code></dt>
|
|
<dd><p>Matches the beginning of a word.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\</X/g'
|
|
Xabc %-= Xdef.
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\></code></dt>
|
|
<dd><p>Matches the end of a word.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "abc %-= def." | sed 's/\>/X/g'
|
|
abcX %-= defX.
|
|
</pre></div>
|
|
|
|
|
|
</dd>
|
|
<dt><code class="code">\`</code></dt>
|
|
<dd><p>Matches only at the start of pattern space. This is different
|
|
from <code class="code">^</code> in multi-line mode.
|
|
</p>
|
|
<p>Compare the following two examples:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf "a\nb\nc\n" | sed 'N;N;s/^/X/gm'
|
|
Xa
|
|
Xb
|
|
Xc
|
|
|
|
$ printf "a\nb\nc\n" | sed 'N;N;s/\`/X/gm'
|
|
Xa
|
|
b
|
|
c
|
|
</pre></div>
|
|
|
|
</dd>
|
|
<dt><code class="code">\'</code></dt>
|
|
<dd><p>Matches only at the end of pattern space. This is different
|
|
from <code class="code">$</code> in multi-line mode.
|
|
</p>
|
|
|
|
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Back_002dreferences-and-Subexpressions">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Back_002dreferences-and-Subexpressions-1"><span>5.7 Back-references and Subexpressions<a class="copiable-link" href="#Back_002dreferences-and-Subexpressions-1"> ¶</a></span></h3>
|
|
<a class="index-entry-id" id="index-subexpression"></a>
|
|
<a class="index-entry-id" id="index-back_002dreference"></a>
|
|
|
|
<p><em class="dfn">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. ‘<samp class="samp">\1</samp>’). The
|
|
part of the regular expression they refer to is called a
|
|
<em class="dfn">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 class="var">replacement</var> part
|
|
of the <code class="command">s</code> command (see <a class="pxref" href="#Regexp-Addresses">Regular
|
|
Expression Addresses</a> and <a class="ref" href="#The-_0022s_0022-Command">The <code class="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 ‘<samp class="samp">.</samp>’ - any single
|
|
character (being surrounded by parentheses makes it a
|
|
subexpression). The back-reference ‘<samp class="samp">\1</samp>’ 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 ‘<samp class="samp">o</samp>’, followed by the same character as the
|
|
first.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ 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-preformatted">$ sed -E -n '/^(.)(.)(.)\3\2\1$/p' /usr/share/dict/words
|
|
redder
|
|
</pre></div>
|
|
|
|
<p>In the <code class="command">s</code> command, back-references can be
|
|
used in the <var class="var">replacement</var> part to refer back to subexpressions in
|
|
the <var class="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 class="var">replacement</var> part prints the words in a different order:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ echo "James Bond" | 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, ‘<samp class="samp">a(.)|b\1</samp>’ will not match ‘<samp class="samp">ba</samp>’. When multiple
|
|
regular expressions are given with <samp class="option">-e</samp> or from a file
|
|
(‘<samp class="samp">-f <var class="var">file</var></samp>’), back-references are local to each expression.
|
|
</p>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Escapes">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Escape-Sequences-_002d-specifying-special-characters"><span>5.8 Escape Sequences - specifying special characters<a class="copiable-link" href="#Escape-Sequences-_002d-specifying-special-characters"> ¶</a></span></h3>
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-special-escapes"></a>
|
|
<p>Until this chapter, we have only encountered escapes of the form
|
|
‘<samp class="samp">\^</samp>’, which tell <code class="command">sed</code> not to interpret the circumflex
|
|
as a special character, but rather to take it literally. For
|
|
example, ‘<samp class="samp">\*</samp>’ matches a single asterisk rather than zero
|
|
or more backslashes.
|
|
</p>
|
|
<a class="index-entry-id" id="index-POSIXLY_005fCORRECT-behavior_002c-escapes"></a>
|
|
<p>This chapter introduces another kind of escape<a class="footnote" id="DOCF6" href="#FOOT6"><sup>6</sup></a>—that
|
|
is, escapes that are applied to a character or sequence of characters
|
|
that ordinarily are taken literally, and that <code class="command">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 class="command">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 class="table">
|
|
<dt><code class="code">\a</code></dt>
|
|
<dd><p>Produces or matches a <small class="sc">BEL</small> character, that is an “alert” (<small class="sc">ASCII</small> 7).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\f</code></dt>
|
|
<dd><p>Produces or matches a form feed (<small class="sc">ASCII</small> 12).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\n</code></dt>
|
|
<dd><p>Produces or matches a newline (<small class="sc">ASCII</small> 10).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\r</code></dt>
|
|
<dd><p>Produces or matches a carriage return (<small class="sc">ASCII</small> 13).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\t</code></dt>
|
|
<dd><p>Produces or matches a horizontal tab (<small class="sc">ASCII</small> 9).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\v</code></dt>
|
|
<dd><p>Produces or matches a so called “vertical tab” (<small class="sc">ASCII</small> 11).
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\c<var class="var">x</var></code></dt>
|
|
<dd><p>Produces or matches <kbd class="kbd"><small class="sc">CONTROL</small>-<var class="var">x</var></kbd>, where <var class="var">x</var> is
|
|
any character. The precise effect of ‘<samp class="samp">\c<var class="var">x</var></samp>’ is as follows:
|
|
if <var class="var">x</var> is a lower case letter, it is converted to upper case.
|
|
Then bit 6 of the character (hex 40) is inverted. Thus ‘<samp class="samp">\cz</samp>’ becomes
|
|
hex 1A, but ‘<samp class="samp">\c{</samp>’ becomes hex 3B, while ‘<samp class="samp">\c;</samp>’ becomes hex 7B.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\d<var class="var">xxx</var></code></dt>
|
|
<dd><p>Produces or matches a character whose decimal <small class="sc">ASCII</small> value is <var class="var">xxx</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\o<var class="var">xxx</var></code></dt>
|
|
<dd><p>Produces or matches a character whose octal <small class="sc">ASCII</small> value is <var class="var">xxx</var>.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">\x<var class="var">xx</var></code></dt>
|
|
<dd><p>Produces or matches a character whose hexadecimal <small class="sc">ASCII</small> value is <var class="var">xx</var>.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
<p>‘<samp class="samp">\b</samp>’ (backspace) was omitted because of the conflict with
|
|
the existing “word boundary” meaning.
|
|
</p>
|
|
<ul class="mini-toc">
|
|
<li><a href="#Escaping-Precedence" accesskey="1">Escaping Precedence</a></li>
|
|
</ul>
|
|
<div class="subsection-level-extent" id="Escaping-Precedence">
|
|
<h4 class="subsection"><span>5.8.1 Escaping Precedence<a class="copiable-link" href="#Escaping-Precedence"> ¶</a></span></h4>
|
|
|
|
<p>GNU <code class="command">sed</code> processes escape sequences <em class="emph">before</em> passing
|
|
the text onto the regular-expression matching of the <code class="command">s///</code> command
|
|
and address matching. Thus the following two commands are equivalent
|
|
(‘<samp class="samp">0x5e</samp>’ is the hexadecimal <small class="sc">ASCII</small> value of the character ‘<samp class="samp">^</samp>’):
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ echo 'a^c' | sed 's/^/b/'
|
|
ba^c
|
|
|
|
$ echo 'a^c' | sed 's/\x5e/b/'
|
|
ba^c
|
|
</pre></div></div>
|
|
|
|
<p>As are the following (‘<samp class="samp">0x5b</samp>’,‘<samp class="samp">0x5d</samp>’ are the hexadecimal
|
|
<small class="sc">ASCII</small> values of ‘<samp class="samp">[</samp>’,‘<samp class="samp">]</samp>’, respectively):
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ echo abc | sed 's/[a]/x/'
|
|
xbc
|
|
$ echo abc | sed 's/\x5ba\x5d/x/'
|
|
xbc
|
|
</pre></div></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">
|
|
<div class="group"><pre class="example-preformatted">$ echo 'a^c' | sed 's/\^/b/'
|
|
abc
|
|
|
|
$ echo 'a^c' | sed 's/\\\x5e/b/'
|
|
a^c
|
|
</pre></div></div>
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
<div class="section-level-extent" id="Locale-Considerations">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Multibyte-characters-and-Locale-Considerations"><span>5.9 Multibyte characters and Locale Considerations<a class="copiable-link" href="#Multibyte-characters-and-Locale-Considerations"> ¶</a></span></h3>
|
|
|
|
<p>GNU <code class="command">sed</code> processes valid multibyte characters in multibyte locales
|
|
(e.g. <code class="code">UTF-8</code>). <a class="footnote" id="DOCF7" href="#FOOT7"><sup>7</sup></a>
|
|
</p>
|
|
<p>The following example uses the Greek letter Capital Sigma
|
|
(Σ,
|
|
Unicode code point <code class="code">0x03A3</code>). In a <code class="code">UTF-8</code> locale,
|
|
<code class="command">sed</code> correctly processes the Sigma as one character despite
|
|
it being 2 octets (bytes):
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ locale | grep LANG
|
|
LANG=en_US.UTF-8
|
|
|
|
$ printf 'a\u03A3b'
|
|
aΣb
|
|
|
|
$ printf 'a\u03A3b' | sed 's/./X/g'
|
|
XXX
|
|
|
|
$ printf 'a\u03A3b' | od -tx1 -An
|
|
61 ce a3 62
|
|
</pre></div></div>
|
|
|
|
<p>To force <code class="command">sed</code> to process octets separately, use the <code class="code">C</code> locale
|
|
(also known as the <code class="code">POSIX</code> locale):
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf 'a\u03A3b' | LC_ALL=C sed 's/./X/g'
|
|
XXXX
|
|
</pre></div>
|
|
|
|
<ul class="mini-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-level-extent" id="Invalid-multibyte-characters">
|
|
<h4 class="subsection"><span>5.9.1 Invalid multibyte characters<a class="copiable-link" href="#Invalid-multibyte-characters"> ¶</a></span></h4>
|
|
|
|
<p><code class="command">sed</code>’s regular expressions <em class="emph">do not</em> match
|
|
invalid multibyte sequences in a multibyte locale.
|
|
</p>
|
|
<p>In the following examples, the ascii value <code class="code">0xCE</code> is
|
|
an incomplete multibyte character (shown here as �).
|
|
The regular expression ‘<samp class="samp">.</samp>’ does not match it:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ printf 'a\xCEb\n'
|
|
a�e
|
|
|
|
$ printf 'a\xCEb\n' | sed 's/./X/g'
|
|
X�X
|
|
|
|
$ printf 'a\xCEc\n' | sed 's/./X/g' | od -tx1c -An
|
|
58 ce 58 0a
|
|
X X \n
|
|
</pre></div></div>
|
|
|
|
<p>Similarly, the ’catch-all’ regular expression ‘<samp class="samp">.*</samp>’ does not
|
|
match the entire line:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ printf 'a\xCEc\n' | sed 's/.*//' | od -tx1c -An
|
|
ce 63 0a
|
|
c \n
|
|
</pre></div></div>
|
|
|
|
<p>GNU <code class="command">sed</code> offers the special <code class="command">z</code> command to clear the
|
|
current pattern space regardless of invalid multibyte characters
|
|
(i.e. it works like <code class="code">s/.*//</code> but also removes invalid multibyte
|
|
characters):
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ printf 'a\xCEc\n' | sed 'z' | od -tx1c -An
|
|
0a
|
|
\n
|
|
</pre></div></div>
|
|
|
|
<p>Alternatively, force the <code class="code">C</code> locale to process
|
|
each octet separately (every octet is a valid character in the <code class="code">C</code>
|
|
locale):
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ printf 'a\xCEc\n' | LC_ALL=C sed 's/.*//' | od -tx1c -An
|
|
0a
|
|
\n
|
|
</pre></div></div>
|
|
|
|
|
|
<p><code class="command">sed</code>’s inability to process invalid multibyte characters
|
|
can be used to detect such invalid sequences in a file.
|
|
In the following examples, the <code class="code">\xCE\xCE</code> is an invalid
|
|
multibyte sequence, while <code class="code">\xCE\A3</code> is a valid multibyte sequence
|
|
(of the Greek Sigma character).
|
|
</p>
|
|
<p>The following <code class="command">sed</code> program removes all valid
|
|
characters using <code class="code">s/.//g</code>. Any content left in the pattern space
|
|
(the invalid characters) are added to the hold space using the
|
|
<code class="code">H</code> command. On the last line (<code class="code">$</code>), the hold space is retrieved
|
|
(<code class="code">x</code>), newlines are removed (<code class="code">s/\n//g</code>), and any remaining
|
|
octets are printed unambiguously (<code class="code">l</code>). Thus, any invalid
|
|
multibyte sequences are printed as octal values:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ printf 'ab\nc\n\xCE\xCEde\n\xCE\xA3f\n' > invalid.txt
|
|
|
|
$ cat invalid.txt
|
|
ab
|
|
c
|
|
��de
|
|
Σf
|
|
|
|
$ sed -n 's/.//g ; H ; ${x;s/\n//g;l}' invalid.txt
|
|
\316\316$
|
|
</pre></div></div>
|
|
|
|
<p>With a few more commands, <code class="command">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 class="code">C</code> locale
|
|
and using octal escape sequences:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ sed -n 's/.//g;=;l' invalid.txt | paste - - | awk '$2!="$"'
|
|
3 \316\316$
|
|
|
|
$ LC_ALL=C sed '3s/\o316\o316//' invalid.txt > fixed.txt
|
|
</pre></div>
|
|
|
|
</div>
|
|
<div class="subsection-level-extent" id="Upper_002fLower-case-conversion">
|
|
<h4 class="subsection"><span>5.9.2 Upper/Lower case conversion<a class="copiable-link" href="#Upper_002fLower-case-conversion"> ¶</a></span></h4>
|
|
|
|
|
|
<p>GNU <code class="command">sed</code>’s substitute command (<code class="code">s</code>) supports upper/lower
|
|
case conversions using <code class="code">\U</code>,<code class="code">\L</code> codes.
|
|
These conversions support multibyte characters:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">$ printf 'ABC\u03a3\n'
|
|
ABCΣ
|
|
|
|
$ printf 'ABC\u03a3\n' | sed 's/.*/\L&/'
|
|
abcσ
|
|
</pre></div>
|
|
|
|
<p>See <a class="xref" href="#The-_0022s_0022-Command">The <code class="code">s</code> Command</a>.
|
|
</p>
|
|
|
|
</div>
|
|
<div class="subsection-level-extent" id="Multibyte-regexp-character-classes">
|
|
<h4 class="subsection"><span>5.9.3 Multibyte regexp character classes<a class="copiable-link" href="#Multibyte-regexp-character-classes"> ¶</a></span></h4>
|
|
|
|
|
|
<p>In other locales, the sorting sequence is not specified, and
|
|
‘<samp class="samp">[a-d]</samp>’ might be equivalent to ‘<samp class="samp">[abcd]</samp>’ or to
|
|
‘<samp class="samp">[aBbCcDd]</samp>’, 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 ‘<samp class="samp">C</samp>’ locale by setting the
|
|
<code class="env">LC_ALL</code> environment variable to the value ‘<samp class="samp">C</samp>’.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted"># 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 class="env">LC_CTYPE</code> locale;
|
|
for example, ‘<samp class="samp">[[:alnum:]]</samp>’ 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-preformatted"># 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-level-extent" id="advanced-sed">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Advanced-sed_003a-cycles-and-buffers"><span>6 Advanced <code class="command">sed</code>: cycles and buffers<a class="copiable-link" href="#Advanced-sed_003a-cycles-and-buffers"> ¶</a></span></h2>
|
|
|
|
|
|
<ul class="mini-toc">
|
|
<li><a href="#Execution-Cycle" accesskey="1">How <code class="command">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-level-extent" id="Execution-Cycle">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code>: cycles and buffers</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="How-sed-Works"><span>6.1 How <code class="command">sed</code> Works<a class="copiable-link" href="#How-sed-Works"> ¶</a></span></h3>
|
|
|
|
<a class="index-entry-id" id="index-Buffer-spaces_002c-pattern-and-hold"></a>
|
|
<a class="index-entry-id" id="index-Spaces_002c-pattern-and-hold"></a>
|
|
<a class="index-entry-id" id="index-Pattern-space_002c-definition"></a>
|
|
<a class="index-entry-id" id="index-Hold-space_002c-definition"></a>
|
|
<p><code class="command">sed</code> maintains two data buffers: the active <em class="emph">pattern</em> space,
|
|
and the auxiliary <em class="emph">hold</em> space. Both are initially empty.
|
|
</p>
|
|
<p><code class="command">sed</code> operates by performing the following cycle on each
|
|
line of input: first, <code class="command">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 class="option">-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 class="footnote" id="DOCF8" href="#FOOT8"><sup>8</sup></a> Then the next cycle starts for the next
|
|
input line.
|
|
</p>
|
|
<p>Unless special commands (like ‘<samp class="samp">D</samp>’) are used, the pattern space is
|
|
deleted between two cycles. The hold space, on the other hand, keeps
|
|
its data between cycles (see commands ‘<samp class="samp">h</samp>’, ‘<samp class="samp">H</samp>’, ‘<samp class="samp">x</samp>’,
|
|
‘<samp class="samp">g</samp>’, ‘<samp class="samp">G</samp>’ to move data between both buffers).
|
|
</p>
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Hold-and-Pattern-Buffers">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code> Works</a>, Up: <a href="#advanced-sed" accesskey="u" rel="up">Advanced <code class="command">sed</code>: cycles and buffers</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Hold-and-Pattern-Buffers-1"><span>6.2 Hold and Pattern Buffers<a class="copiable-link" href="#Hold-and-Pattern-Buffers-1"> ¶</a></span></h3>
|
|
|
|
<p>TODO
|
|
</p>
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Multiline-techniques">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code>: cycles and buffers</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Multiline-techniques-_002d-using-D_002cG_002cH_002cN_002cP-to-process-multiple-lines"><span>6.3 Multiline techniques - using D,G,H,N,P to process multiple lines<a class="copiable-link" href="#Multiline-techniques-_002d-using-D_002cG_002cH_002cN_002cP-to-process-multiple-lines"> ¶</a></span></h3>
|
|
|
|
<p>Multiple lines can be processed as one buffer using the
|
|
<code class="code">D</code>,<code class="code">G</code>,<code class="code">H</code>,<code class="code">N</code>,<code class="code">P</code>. They are similar to
|
|
their lowercase counterparts (<code class="code">d</code>,<code class="code">g</code>,
|
|
<code class="code">h</code>,<code class="code">n</code>,<code class="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 class="table">
|
|
<dt><code class="code">D</code></dt>
|
|
<dd><p><em class="emph">deletes</em> line from the pattern space until the first newline,
|
|
and restarts the cycle.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">G</code></dt>
|
|
<dd><p><em class="emph">appends</em> line from the hold space to the pattern space, with a
|
|
newline before it.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">H</code></dt>
|
|
<dd><p><em class="emph">appends</em> line from the pattern space to the hold space, with a
|
|
newline before it.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">N</code></dt>
|
|
<dd><p><em class="emph">appends</em> line from the input file to the pattern space.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">P</code></dt>
|
|
<dd><p><em class="emph">prints</em> line from the pattern space until the first newline.
|
|
</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
|
|
<p>The following example illustrates the operation of <code class="code">N</code> and
|
|
<code class="code">D</code> commands:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ seq 6 | sed -n 'N;l;D'
|
|
1\n2$
|
|
2\n3$
|
|
3\n4$
|
|
4\n5$
|
|
5\n6$
|
|
</pre></div></div>
|
|
|
|
<ol class="enumerate">
|
|
<li> <code class="command">sed</code> starts by reading the first line into the pattern space
|
|
(i.e. ‘<samp class="samp">1</samp>’).
|
|
</li><li> At the beginning of every cycle, the <code class="code">N</code>
|
|
command appends a newline and the next line to the pattern space
|
|
(i.e. ‘<samp class="samp">1</samp>’, ‘<samp class="samp">\n</samp>’, ‘<samp class="samp">2</samp>’ in the first cycle).
|
|
</li><li> The <code class="code">l</code> command prints the content of the pattern space
|
|
unambiguously.
|
|
</li><li> The <code class="code">D</code> command then removes the content of pattern
|
|
space up to the first newline (leaving ‘<samp class="samp">2</samp>’ at the end of
|
|
the first cycle).
|
|
</li><li> At the next cycle the <code class="code">N</code> command appends a
|
|
newline and the next input line to the pattern space
|
|
(e.g. ‘<samp class="samp">2</samp>’, ‘<samp class="samp">\n</samp>’, ‘<samp class="samp">3</samp>’).
|
|
</li></ol>
|
|
|
|
|
|
<a class="index-entry-id" id="index-processing-paragraphs"></a>
|
|
<a class="index-entry-id" id="index-paragraphs_002c-processing"></a>
|
|
<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-preformatted">sed '/./{H;$!d} ; x ; s/REGEXP/REPLACEMENT/'
|
|
</pre></div>
|
|
|
|
<ol class="enumerate">
|
|
<li> The first expression, <code class="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 class="code">x</code> and <code class="code">s</code> are executed only on empty
|
|
lines (i.e. paragraph separators). The <code class="code">x</code> command fetches the
|
|
accumulated lines from the hold space back to the pattern space. The
|
|
<code class="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">
|
|
<div class="group"><pre class="example-preformatted">$ 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-->/ ; s/$/\n<--END/' input.txt
|
|
|
|
START-->
|
|
a a a aa aaa
|
|
aaaa aaaa aa
|
|
aaaa aaa aaa
|
|
<--END
|
|
|
|
START-->
|
|
bbbb bbb bbb
|
|
bb bb bbb bb
|
|
bbbbbbbb bbb
|
|
<--END
|
|
|
|
START-->
|
|
ccc ccc cccc
|
|
cccc ccccc c
|
|
cc cc cc cc
|
|
<--END
|
|
</pre></div></div>
|
|
|
|
<p>For more annotated examples, see <a class="pxref" href="#Text-search-across-multiple-lines">Text search across multiple lines</a>
|
|
and <a class="ref" href="#Line-length-adjustment">Line length adjustment</a>.
|
|
</p>
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Branching-and-flow-control">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code>: cycles and buffers</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Branching-and-Flow-Control"><span>6.4 Branching and Flow Control<a class="copiable-link" href="#Branching-and-Flow-Control"> ¶</a></span></h3>
|
|
|
|
<p>The branching commands <code class="code">b</code>, <code class="code">t</code>, and <code class="code">T</code> enable
|
|
changing the flow of <code class="command">sed</code> programs.
|
|
</p>
|
|
<p>By default, <code class="command">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 class="xref" href="#Execution-Cycle">How <code class="command">sed</code> Works</a> and <a class="ref" href="#Addresses-overview">Addresses overview</a>.
|
|
</p>
|
|
<p><code class="command">sed</code> does not support a typical <code class="code">if/then</code> construct.
|
|
Instead, some commands can be used as conditionals or to change the
|
|
default flow control:
|
|
</p>
|
|
<dl class="table">
|
|
<dt><code class="code">d</code></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><code class="code">D</code></dt>
|
|
<dd><p>delete the contents of the pattern space <em class="emph">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><code class="code">[addr]X</code></dt>
|
|
<dt><code class="code">[addr]{ X ; X ; X }</code></dt>
|
|
<dt><code class="code">/regexp/X</code></dt>
|
|
<dt><code class="code">/regexp/{ X ; X ; X }</code></dt>
|
|
<dd><p>Addresses and regular expressions can be used as an <code class="code">if/then</code>
|
|
conditional: If <var class="var">[addr]</var> matches the current pattern space,
|
|
execute the command(s).
|
|
For example: The command <code class="code">/^#/d</code> means:
|
|
<em class="emph">if</em> the current pattern matches the regular expression <code class="code">^#</code> (a line
|
|
starting with a hash), <em class="emph">then</em> execute the <code class="code">d</code> command:
|
|
delete the line without printing it, and restart the program cycle
|
|
immediately.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">b</code></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><code class="code">t</code></dt>
|
|
<dd><p>branch conditionally (that is: jump to a label) <em class="emph">only if</em> a
|
|
<code class="code">s///</code> command has succeeded since the last input line was read
|
|
or another conditional branch was taken.
|
|
</p>
|
|
</dd>
|
|
<dt><code class="code">T</code></dt>
|
|
<dd><p>similar but opposite to the <code class="code">t</code> command: branch only if
|
|
there has been <em class="emph">no</em> successful substitutions since the last
|
|
input line was read.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
|
|
<p>The following two <code class="command">sed</code> programs are equivalent. The first
|
|
(contrived) example uses the <code class="code">b</code> command to skip the <code class="code">s///</code>
|
|
command on lines containing ‘<samp class="samp">1</samp>’. The second example uses an
|
|
address with negation (‘<samp class="samp">!</samp>’) to perform substitution only on
|
|
desired lines. The <code class="code">y///</code> command is still executed on all
|
|
lines:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
|
|
<ul class="mini-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-level-extent" id="Branching-and-Cycles">
|
|
<h4 class="subsection"><span>6.4.1 Branching and Cycles<a class="copiable-link" href="#Branching-and-Cycles"> ¶</a></span></h4>
|
|
<a class="index-entry-id" id="index-labels"></a>
|
|
<a class="index-entry-id" id="index-omitting-labels"></a>
|
|
<a class="index-entry-id" id="index-cycle_002c-restarting"></a>
|
|
<a class="index-entry-id" id="index-restarting-a-cycle"></a>
|
|
<p>The <code class="code">b</code>,<code class="code">t</code> and <code class="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. ‘<samp class="samp">:x</samp>’). 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 class="command">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 class="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">
|
|
<div class="group"><pre class="example-preformatted">$ seq 3 | sed b
|
|
1
|
|
2
|
|
3
|
|
</pre></div></div>
|
|
|
|
<a class="index-entry-id" id="index-infinite-loop_002c-branching"></a>
|
|
<a class="index-entry-id" id="index-branching_002c-infinite-loop"></a>
|
|
<p>The following example is an infinite-loop - it doesn’t terminate and
|
|
doesn’t print anything. The <code class="code">b</code> command jumps to the ‘<samp class="samp">x</samp>’
|
|
label, and a new cycle is never started:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
<a class="index-entry-id" id="index-branching-and-n_002c-N"></a>
|
|
<a class="index-entry-id" id="index-n_002c-and-branching"></a>
|
|
<a class="index-entry-id" id="index-N_002c-and-branching"></a>
|
|
<p>Branching is often complemented with the <code class="code">n</code> or <code class="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 class="code">n</code>
|
|
prints the current pattern space then empties it, while <code class="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">
|
|
<div class="group"><pre class="example-preformatted">$ seq 3 | sed ':x ; n ; bx'
|
|
1
|
|
2
|
|
3
|
|
|
|
$ seq 3 | sed ':x ; N ; bx'
|
|
1
|
|
2
|
|
3
|
|
</pre></div></div>
|
|
|
|
<ul class="itemize mark-bullet">
|
|
<li>Both examples do not inf-loop, despite never starting a new cycle.
|
|
|
|
</li><li>In the first example, the <code class="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 class="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 class="code">N</code> command terminates the <code class="command">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 class="command">sed</code>,
|
|
because it uses the non-POSIX-standard behavior of <code class="code">N</code>.
|
|
See the “<code class="code">N</code> command on the last line” paragraph
|
|
in <a class="ref" href="#Reporting-Bugs">Reporting Bugs</a>.
|
|
|
|
</li><li>To further examine the difference between the two examples,
|
|
try the following commands:
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">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></div>
|
|
|
|
</li></ul>
|
|
|
|
|
|
|
|
</div>
|
|
<div class="subsection-level-extent" id="Branching-example_003a-joining-lines">
|
|
<h4 class="subsection"><span>6.4.2 Branching example: joining lines<a class="copiable-link" href="#Branching-example_003a-joining-lines"> ¶</a></span></h4>
|
|
|
|
<a class="index-entry-id" id="index-joining-lines-with-branching"></a>
|
|
<a class="index-entry-id" id="index-branching_002c-joining-lines"></a>
|
|
<a class="index-entry-id" id="index-quoted_002dprintable-lines_002c-joining"></a>
|
|
<a class="index-entry-id" id="index-joining-quoted_002dprintable-lines"></a>
|
|
<a class="index-entry-id" id="index-t_002c-joining-lines-with"></a>
|
|
<a class="index-entry-id" id="index-b_002c-joining-lines-with"></a>
|
|
<a class="index-entry-id" id="index-b_002c-versus-t"></a>
|
|
<a class="index-entry-id" id="index-t_002c-versus-b"></a>
|
|
<p>As a real-world example of using branching, consider the case of
|
|
<a class="uref" 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 class="dfn">soft line break</em>
|
|
consisting of a single ‘<samp class="samp">=</samp>’ character at the end of the line:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
<p>The following program uses an address match ‘<samp class="samp">/=$/</samp>’ as a
|
|
conditional: If the current pattern space ends with a ‘<samp class="samp">=</samp>’, it
|
|
reads the next input line using <code class="code">N</code>, replaces all ‘<samp class="samp">=</samp>’
|
|
characters which are followed by a newline, and unconditionally
|
|
branches (<code class="code">b</code>) to the beginning of the program without restarting
|
|
a new cycle. If the pattern space does not ends with ‘<samp class="samp">=</samp>’, the
|
|
default action is performed: the pattern space is printed and a new
|
|
cycle is started:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
<p>Here’s an alternative program with a slightly different approach: On
|
|
all lines except the last, <code class="code">N</code> appends the line to the pattern
|
|
space. A substitution command then removes soft line breaks
|
|
(‘<samp class="samp">=</samp>’ at the end of a line, i.e. followed by a newline) by replacing
|
|
them with an empty string.
|
|
<em class="emph">If</em> the substitution was successful (meaning the pattern space contained
|
|
a line which should be joined), the conditional branch command <code class="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 class="code">t</code> command will <em class="emph">not</em> branch. Then, <code class="code">P</code> will
|
|
print the pattern space content until the first newline, and <code class="code">D</code>
|
|
will delete the pattern space content until the first new line.
|
|
(To learn more about <code class="code">N</code>, <code class="code">P</code> and <code class="code">D</code> commands
|
|
see <a class="pxref" href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a>).
|
|
</p>
|
|
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
<p>For more line-joining examples see <a class="pxref" href="#Joining-lines">Joining lines</a>.
|
|
</p>
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="chapter-level-extent" id="Examples">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Limitations" accesskey="n" rel="next">GNU <code class="command">sed</code>’s Limitations and Non-limitations</a>, Previous: <a href="#advanced-sed" accesskey="p" rel="prev">Advanced <code class="command">sed</code>: cycles and buffers</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Some-Sample-Scripts"><span>7 Some Sample Scripts<a class="copiable-link" href="#Some-Sample-Scripts"> ¶</a></span></h2>
|
|
|
|
<p>Here are some <code class="command">sed</code> scripts to guide you in the art of mastering
|
|
<code class="command">sed</code>.
|
|
</p>
|
|
|
|
<ul class="mini-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 class="command">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-level-extent" id="Joining-lines">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Joining-lines-1"><span>7.1 Joining lines<a class="copiable-link" href="#Joining-lines-1"> ¶</a></span></h3>
|
|
|
|
<p>This section uses <code class="code">N</code>, <code class="code">D</code> and <code class="code">P</code> commands to process
|
|
multiple lines, and the <code class="code">b</code> and <code class="code">t</code> commands for branching.
|
|
See <a class="xref" href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a> and <a class="ref" 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-preformatted">$ 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-preformatted">$ 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">
|
|
<div class="group"><pre class="example-preformatted">$ 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: <abcdef@gnu.org>
|
|
From: John Doe <jdoe@gnu.org>
|
|
To: Jane Smith <jsmith@gnu.org>
|
|
|
|
$ 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: <abcdef@gnu.org>
|
|
From: John Doe <jdoe@gnu.org>
|
|
To: Jane Smith <jsmith@gnu.org>
|
|
|
|
# A portable (non-gnu) variation:
|
|
# sed -e :a -e '$!N;s/\n */ /;ta' -e 'P;D'
|
|
</pre></div></div>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Centering-lines">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Centering-Lines"><span>7.2 Centering Lines<a class="copiable-link" href="#Centering-Lines"> ¶</a></span></h3>
|
|
|
|
<p>This script centers all lines of a file on a 80 columns width.
|
|
To change that width, the number in <code class="code">\{…\}</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—this is a common
|
|
technique.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -f
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Put 80 spaces in the buffer
|
|
1 {
|
|
x
|
|
s/^$/ /
|
|
s/^.*$/&&&&&&&&/
|
|
x
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># delete leading and trailing spaces
|
|
y/<kbd class="kbd"><kbd class="key">TAB</kbd></kbd>/ /
|
|
s/^ *//
|
|
s/ *$//
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># add a newline and 80 spaces to end of line
|
|
G
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># keep first 81 chars (80 + a newline)
|
|
s/^\(.\{81\}\).*$/\1/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># \2 matches half of the spaces, which are moved to the beginning
|
|
s/^\(.*\)\n\(.*\)\2/\2\1/
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Increment-a-number">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Increment-a-Number"><span>7.3 Increment a Number<a class="copiable-link" href="#Increment-a-Number"> ¶</a></span></h3>
|
|
|
|
<p>This script is one of a few that demonstrate how to do arithmetic
|
|
in <code class="command">sed</code>. This is indeed possible,<a class="footnote" 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’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’t have this limitation, the
|
|
algorithm used in <a class="ref" href="#cat-_002dn">Numbering lines</a>, is faster.
|
|
It works by replacing trailing nines with an underscore, then
|
|
using multiple <code class="code">s</code> commands to increment the last digit,
|
|
and then again substituting underscores with zeros.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -f
|
|
|
|
/[^0-9]/ d
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># replace all trailing 9s by _ (any other character except digits, could
|
|
# be used)
|
|
:d
|
|
s/9\(_*\)$/_\1/
|
|
td
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># incr last digit only. The first line adds a most-significant
|
|
# digit of 1 if we have to add a digit.
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">:n
|
|
y/_/0/
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Rename-files-to-lower-case">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Print-bash-environment" accesskey="n" rel="next">Print <code class="command">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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Rename-Files-to-Lower-Case"><span>7.4 Rename Files to Lower Case<a class="copiable-link" href="#Rename-Files-to-Lower-Case"> ¶</a></span></h3>
|
|
|
|
<p>This is a pretty strange use of <code class="command">sed</code>. We transform text, and
|
|
transform it to be shell commands, then just feed them to shell.
|
|
Don’t worry, even worse hacks are done when using <code class="command">sed</code>; I have
|
|
seen a script converting the output of <code class="command">date</code> into a <code class="command">bc</code>
|
|
program!
|
|
</p>
|
|
<p>The main body of this is the <code class="command">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">
|
|
<div class="group"><pre class="example-preformatted">#! /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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">help()
|
|
{
|
|
cat << eof
|
|
Usage: $0 [-n] [-r] [-h] files...
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">-n do nothing, only see what would be done
|
|
-R recursive (use find)
|
|
-h this message
|
|
files files to remap to lower case
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">Examples:
|
|
$0 -n * (see if everything is ok, then...)
|
|
$0 *
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
$0 -R .
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">eof
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">apply_cmd='sh'
|
|
finder='echo "$@" | tr " " "\n"'
|
|
files_only=
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">while :
|
|
do
|
|
case "$1" in
|
|
-n) apply_cmd='cat' ;;
|
|
-R) finder='find "$@" -type f';;
|
|
-h) help ; exit 1 ;;
|
|
*) break ;;
|
|
esac
|
|
shift
|
|
done
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">if [ -z "$1" ]; then
|
|
echo Usage: $0 [-h] [-n] [-r] files...
|
|
exit 1
|
|
fi
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">LOWER='abcdefghijklmnopqrstuvwxyz'
|
|
UPPER='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">case `basename $0` in
|
|
*upper*) TO=$UPPER; FROM=$LOWER ;;
|
|
*) FROM=$UPPER; TO=$LOWER ;;
|
|
esac
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
eval $finder | sed -n '
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># remove all trailing slashes
|
|
s/\/*$//
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># add ./ if there is no path, only a filename
|
|
/\//! s/^/.\//
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># save path+filename
|
|
h
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># remove path
|
|
s/.*\///
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># do conversion only on filename
|
|
y/'$FROM'/'$TO'/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># now line contains original path+file, while
|
|
# hold space contains the new filename
|
|
x
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># add converted file name to line, which now contains
|
|
# path/file-name\nconverted-file-name
|
|
G
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># check if converted file name is equal to original file name,
|
|
# if it is, do not print anything
|
|
/^.*\/\(.*\)\n\1/b
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># escape special characters for the shell
|
|
s/["$`\\]/\\&/g
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># now, transform path/fromfile\n, into
|
|
# mv path/fromfile path/tofile and print it
|
|
s/^\(.*\/\)\(.*\)\n\(.*\)$/mv "\1\2" "\1\3"/p
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
' | $apply_cmd
|
|
</pre></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Print-bash-environment">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Print-bash-Environment"><span>7.5 Print <code class="command">bash</code> Environment<a class="copiable-link" href="#Print-bash-Environment"> ¶</a></span></h3>
|
|
|
|
<p>This script strips the definition of the shell functions
|
|
from the output of the <code class="command">set</code> Bourne-shell command.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/bin/sh
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">set | sed -n '
|
|
:x
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># if no occurrence of ‘<samp class="samp">=()</samp>’ print and load next line
|
|
/=()/! { p; b; }
|
|
/ () $/! { p; b; }
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># possible start of functions section
|
|
# save the line in case this is a var like FOO="() "
|
|
h
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># if the next line has a brace, we quit because
|
|
# nothing comes after functions
|
|
n
|
|
/^{/ q
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># print the old line
|
|
x; p
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># work on the new line now
|
|
x; bx
|
|
'
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Reverse-chars-of-lines">
|
|
<div class="nav-panel">
|
|
<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 class="command">bash</code> Environment</a>, Up: <a href="#Examples" accesskey="u" rel="up">Some Sample Scripts</a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Reverse-Characters-of-Lines"><span>7.6 Reverse Characters of Lines<a class="copiable-link" href="#Reverse-Characters-of-Lines"> ¶</a></span></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 class="code">tx</code> command before the definition of the label.
|
|
This is often needed to reset the flag that is tested by
|
|
the <code class="code">t</code> command.
|
|
</p>
|
|
<p>Imaginative readers will find uses for this script. An example
|
|
is reversing the output of <code class="command">banner</code>.<a class="footnote" id="DOCF10" href="#FOOT10"><sup>10</sup></a>
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -f
|
|
|
|
/../! b
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Reverse a line. Begin embedding the line between two newlines
|
|
s/^.*$/\
|
|
&\
|
|
/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Remove the newline markers
|
|
s/\n//g
|
|
</pre></div></div>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Text-search-across-multiple-lines">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Text-search-across-multiple-lines-1"><span>7.7 Text search across multiple lines<a class="copiable-link" href="#Text-search-across-multiple-lines-1"> ¶</a></span></h3>
|
|
|
|
<p>This section uses <code class="code">N</code> and <code class="code">D</code> commands to search for
|
|
consecutive words spanning multiple lines. See <a class="xref" 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 class="command">grep</code>
|
|
and similarly with GNU <code class="command">sed</code>:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ cat two-cities-dup1.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,
|
|
|
|
$ grep -E '\b(\w+)\s+\1\b' two-cities-dup1.txt
|
|
it was the the age of wisdom,
|
|
|
|
$ grep -n -E '\b(\w+)\s+\1\b' two-cities-dup1.txt
|
|
3:it was the the age of wisdom,
|
|
|
|
$ sed -En '/\b(\w+)\s+\1\b/p' two-cities-dup1.txt
|
|
it was the the age of wisdom,
|
|
|
|
$ sed -En '/\b(\w+)\s+\1\b/{=;p}' two-cities-dup1.txt
|
|
3
|
|
it was the the age of wisdom,
|
|
</pre></div></div>
|
|
|
|
<ul class="itemize mark-bullet">
|
|
<li>The regular expression ‘<samp class="samp">\b\w+\s+</samp>’ searches for word-boundary (‘<samp class="samp">\b</samp>’),
|
|
followed by one-or-more word-characters (‘<samp class="samp">\w+</samp>’), followed by whitespace
|
|
(‘<samp class="samp">\s+</samp>’). See <a class="xref" href="#regexp-extensions">regular expression extensions</a>.
|
|
|
|
</li><li>Adding parentheses around the ‘<samp class="samp">(\w+)</samp>’ expression creates a subexpression.
|
|
The regular expression pattern ‘<samp class="samp">(PATTERN)\s+\1</samp>’ defines a subexpression
|
|
(in the parentheses) followed by a back-reference, separated by whitespace.
|
|
A successful match means the <var class="var">PATTERN</var> was repeated twice in succession.
|
|
See <a class="xref" href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a>.
|
|
|
|
</li><li>The word-boundery expression (‘<samp class="samp">\b</samp>’) at both ends ensures partial
|
|
words are not matched (e.g. ‘<samp class="samp">the then</samp>’ is not a desired match).
|
|
|
|
</li><li>The <samp class="option">-E</samp> option enables extended regular expression syntax, alleviating
|
|
the need to add backslashes before the parenthesis. See <a class="xref" 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 class="command">grep</code> and <code class="command">sed</code> operate line-by-line.
|
|
</p>
|
|
<p>By using <code class="command">N</code> and <code class="command">D</code> commands, <code class="command">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-preformatted">$ 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 class="itemize mark-bullet">
|
|
<li>The <code class="command">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 ‘<samp class="samp">\s+</samp>’ for word separator which matches
|
|
both spaces and newlines.
|
|
|
|
</li><li>The regular expression matches, the entire pattern space is printed
|
|
with <code class="command">p</code>. No lines are printed by default due to the <samp class="option">-n</samp> option.
|
|
|
|
</li><li>The <code class="command">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 class="command">coreutils</code> manual for an alternative solution using
|
|
<code class="command">tr -s</code> and <code class="command">uniq</code> at
|
|
<a class="url" 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-level-extent" id="Line-length-adjustment">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Line-length-adjustment-1"><span>7.8 Line length adjustment<a class="copiable-link" href="#Line-length-adjustment-1"> ¶</a></span></h3>
|
|
|
|
<p>This section uses <code class="code">N</code> and <code class="code">P</code> commands to read and write
|
|
lines, and the <code class="code">b</code> command for branching.
|
|
See <a class="xref" href="#Multiline-techniques">Multiline techniques - using D,G,H,N,P to process multiple lines</a> and <a class="ref" 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">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
<p class="exdent">The following sed program wraps lines at 40 characters:
|
|
</p><div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
|
|
<p class="exdent">The wrapped output:
|
|
</p><div class="example">
|
|
<div class="group"><pre class="example-preformatted">$ 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></div>
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="Adding-a-header-to-multiple-files">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Adding-a-header-to-multiple-files-1"><span>7.9 Adding a header to multiple files<a class="copiable-link" href="#Adding-a-header-to-multiple-files-1"> ¶</a></span></h3>
|
|
|
|
<p>GNU <code class="command">sed</code> can be used to safely modify multiple files at once.
|
|
</p>
|
|
<p class="exdent">Add a single line to the beginning of source code files:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">sed -i '1i/* Copyright (C) FOO BAR */' *.c
|
|
</pre></div>
|
|
|
|
<p class="exdent">Adding a few lines is possible using ‘<samp class="samp">\n</samp>’ in the text:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">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 class="code">0rFILE</code>.
|
|
A typical use case is adding a license notice header to all files:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">## Create the header file:
|
|
$ cat<<'EOF'>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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
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 class="file">.sh</samp>,<samp class="file">.py</samp>,<samp class="file">.pl</samp> files)
|
|
the license notice typically appears <em class="emph">after</em> the first line (the
|
|
’shebang’ ‘<samp class="samp">#!</samp>’ line). The <code class="code">1rFILE</code> command will add <samp class="file">FILE</samp>
|
|
<em class="emph">after</em> the first line:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">## Create the header file:
|
|
$ cat<<'EOF'>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 <https://www.gnu.org/licenses/>.
|
|
##
|
|
##
|
|
EOF
|
|
|
|
## Add the file at the beginning of all source code files:
|
|
$ sed -i '1rLIC.TXT' *.py *.sh
|
|
</pre></div>
|
|
|
|
<p>The above <code class="command">sed</code> commands can be combined with <code class="command">find</code>
|
|
to locate files in all subdirectories, <code class="command">xargs</code> to run additional
|
|
commands on selected files and <code class="command">grep</code> to filter out files that already
|
|
contain a copyright notice:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">find \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \) \
|
|
| xargs grep -Li copyright \
|
|
| xargs -r sed -i '0rLIC.TXT'
|
|
</pre></div>
|
|
|
|
<p class="exdent">Or a slightly safe version (handling files with spaces and newlines):
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">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 class="code">0</code> address with <code class="code">r</code> command requires GNU <code class="command">sed</code>
|
|
version 4.9 or later. See <a class="xref" href="#Zero-Address">Zero Address</a>.
|
|
</p>
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="tac">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Reverse-Lines-of-Files"><span>7.10 Reverse Lines of Files<a class="copiable-link" href="#Reverse-Lines-of-Files"> ¶</a></span></h3>
|
|
|
|
<p>This one begins a series of totally useless (yet interesting)
|
|
scripts emulating various Unix commands. This, in particular,
|
|
is a <code class="command">tac</code> workalike.
|
|
</p>
|
|
<p>Note that on implementations other than GNU <code class="command">sed</code>
|
|
this script might easily overflow internal buffers.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
# reverse all lines of input, i.e. first line became last, ...
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># on the last line we're done -- print everything
|
|
$ p
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># store everything on the buffer again
|
|
h
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="cat-_002dn">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Numbering-Lines"><span>7.11 Numbering Lines<a class="copiable-link" href="#Numbering-Lines"> ¶</a></span></h3>
|
|
|
|
<p>This script replaces ‘<samp class="samp">cat -n</samp>’; in fact it formats its output
|
|
exactly like GNU <code class="command">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">
|
|
<div class="group"><pre class="example-preformatted">#! /bin/sh
|
|
sed -e "=" $@ | sed -e '
|
|
s/^/ /
|
|
N
|
|
s/^ *\(......\)\n/\1 /
|
|
'
|
|
</pre></div></div>
|
|
|
|
<p>It uses <code class="command">sed</code> to print the line number, then groups lines two
|
|
by two using <code class="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 class="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-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Prime the pump on the first line
|
|
x
|
|
/^$/ s/^.*$/1/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Add the correct line number before the pattern
|
|
G
|
|
h
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Format it and print it
|
|
s/^/ /
|
|
s/^ *\(......\)\n/\1 /p
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># separate changing/unchanged digits with an x
|
|
s/.9*$/x&/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># keep changing digits in hold space
|
|
h
|
|
s/^.*x//
|
|
y/0123456789/1234567890/
|
|
x
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># keep unchanged digits in pattern space
|
|
s/x.*$//
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># compose the new number, remove the newline implicitly added by G
|
|
G
|
|
s/\n//
|
|
h
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="cat-_002db">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Numbering-Non_002dblank-Lines"><span>7.12 Numbering Non-blank Lines<a class="copiable-link" href="#Numbering-Non_002dblank-Lines"> ¶</a></span></h3>
|
|
|
|
<p>Emulating ‘<samp class="samp">cat -b</samp>’ is almost the same as ‘<samp class="samp">cat -n</samp>’—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 class="command">sed</code>
|
|
scripts properly...
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">/^$/ {
|
|
p
|
|
b
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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&/
|
|
h
|
|
s/^.*x//
|
|
y/0123456789/1234567890/
|
|
x
|
|
s/x.*$//
|
|
G
|
|
s/\n//
|
|
h
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="wc-_002dc">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Counting-Characters"><span>7.13 Counting Characters<a class="copiable-link" href="#Counting-Characters"> ¶</a></span></h3>
|
|
|
|
<p>This script shows another way to do arithmetic with <code class="command">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 class="command">sed</code>. ‘<samp class="samp">a</samp>’s are units, ‘<samp class="samp">b</samp>’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 class="code">s</code> commands<a class="footnote" id="DOCF11" href="#FOOT11"><sup>11</sup></a>: first we
|
|
convert units, removing ‘<samp class="samp">a</samp>’s from the number; then we
|
|
rotate letters so that tens become ‘<samp class="samp">a</samp>’s, and so on
|
|
until no more letters remain.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Add n+1 a's to hold space (+1 is for the newline)
|
|
s/./a/g
|
|
H
|
|
x
|
|
s/\n/a/
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">: done
|
|
$! {
|
|
h
|
|
b
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
# On the last line, convert back to decimal
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">: loop
|
|
/a/! s/[b-h]*/&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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">: next
|
|
y/bcdefgh/abcdefg/
|
|
/[a-h]/ b loop
|
|
p
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="wc-_002dw">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Counting-Words"><span>7.14 Counting Words<a class="copiable-link" href="#Counting-Words"> ¶</a></span></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 ‘<samp class="samp">a</samp>’
|
|
(in the previous script each letter was changed to an ‘<samp class="samp">a</samp>’).
|
|
</p>
|
|
<p>It is interesting that real <code class="command">wc</code> programs have optimized
|
|
loops for ‘<samp class="samp">wc -c</samp>’, so they are much slower at counting
|
|
words rather than characters. This script’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 class="command">sed</code> scripts.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Convert words to a's
|
|
s/[ <kbd class="kbd"><kbd class="key">TAB</kbd></kbd>][ <kbd class="kbd"><kbd class="key">TAB</kbd></kbd>]*/ /g
|
|
s/^/ /
|
|
s/ [^ ][^ ]*/a /g
|
|
s/ //g
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Append them to hold space
|
|
H
|
|
x
|
|
s/\n//
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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]*/&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></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="wc-_002dl">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Counting-Lines"><span>7.15 Counting Lines<a class="copiable-link" href="#Counting-Lines"> ¶</a></span></h3>
|
|
|
|
<p>No strange things are done now, because <code class="command">sed</code> gives us
|
|
‘<samp class="samp">wc -l</samp>’ functionality for free!!! Look:
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
$=
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="head">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Printing-the-First-Lines"><span>7.16 Printing the First Lines<a class="copiable-link" href="#Printing-the-First-Lines"> ¶</a></span></h3>
|
|
|
|
<p>This script is probably the simplest useful <code class="command">sed</code> script.
|
|
It displays the first 10 lines of input; the number of displayed
|
|
lines is right before the <code class="code">q</code> command.
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">#!/usr/bin/sed -f
|
|
10q
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="tail">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Printing-the-Last-Lines"><span>7.17 Printing the Last Lines<a class="copiable-link" href="#Printing-the-Last-Lines"> ¶</a></span></h3>
|
|
|
|
<p>Printing the last <var class="var">n</var> lines rather than the first is more complex
|
|
but indeed possible. <var class="var">n</var> is encoded in the second line, before
|
|
the bang character.
|
|
</p>
|
|
<p>This script is similar to the <code class="command">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-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">1! {; H; g; }
|
|
1,10 !s/[^\n]*\n//
|
|
$p
|
|
h
|
|
</pre></div></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 class="code">D</code> command but does not
|
|
restart the loop).
|
|
</p>
|
|
<p>The “sliding window” technique is a very powerful way to write
|
|
efficient and complex <code class="command">sed</code> scripts, because commands like
|
|
<code class="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 class="code">N</code>, <code class="code">P</code>
|
|
and <code class="code">D</code> commands, here is an implementation of <code class="command">tail</code>
|
|
using a simple “sliding window.”
|
|
</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 class="code">N</code> and <code class="code">D</code> to slide pattern
|
|
space by one line:
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -f
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">1h
|
|
2,10 {; H; g; }
|
|
$q
|
|
1,9d
|
|
N
|
|
D
|
|
</pre></div></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-level-extent" id="uniq">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Make-Duplicate-Lines-Unique"><span>7.18 Make Duplicate Lines Unique<a class="copiable-link" href="#Make-Duplicate-Lines-Unique"> ¶</a></span></h3>
|
|
|
|
<p>This is an example of the art of using the <code class="code">N</code>, <code class="code">P</code>
|
|
and <code class="code">D</code> commands, probably the most difficult to master.
|
|
</p>
|
|
<div class="example">
|
|
<div class="group"><pre class="example-preformatted">#!/usr/bin/sed -f
|
|
h
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">: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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># If the <code class="code">N</code> command had added the last line, print and exit
|
|
$b
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># The lines are different; print the first and go
|
|
# back working on the second.
|
|
P
|
|
D
|
|
</pre></div></div>
|
|
|
|
<p>As you can see, we maintain a 2-line window using <code class="code">P</code> and <code class="code">D</code>.
|
|
This technique is often used in advanced <code class="command">sed</code> scripts.
|
|
</p>
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="uniq-_002dd">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Print-Duplicated-Lines-of-Input"><span>7.19 Print Duplicated Lines of Input<a class="copiable-link" href="#Print-Duplicated-Lines-of-Input"> ¶</a></span></h3>
|
|
|
|
<p>This script prints only duplicated lines, like ‘<samp class="samp">uniq -d</samp>’.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">$b
|
|
N
|
|
/^\(.*\)\n\1$/ {
|
|
# Print the first of the duplicated lines
|
|
s/.*\n//
|
|
p
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"> # Loop until we get a different line
|
|
:b
|
|
$b
|
|
N
|
|
/^\(.*\)\n\1$/ {
|
|
s/.*\n//
|
|
bb
|
|
}
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># The last line cannot be followed by duplicates
|
|
$b
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Found a different one. Leave it alone in the pattern space
|
|
# and go back to the top, hunting its duplicates
|
|
D
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="uniq-_002du">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Remove-All-Duplicated-Lines"><span>7.20 Remove All Duplicated Lines<a class="copiable-link" href="#Remove-All-Duplicated-Lines"> ¶</a></span></h3>
|
|
|
|
<p>This script prints only unique lines, like ‘<samp class="samp">uniq -u</samp>’.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -f
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Search for a duplicate line --- until that, print what you find.
|
|
$b
|
|
N
|
|
/^\(.*\)\n\1$/ ! {
|
|
P
|
|
D
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted">:c
|
|
# Got two equal lines in pattern space. At the
|
|
# end of the file we simply exit
|
|
$d
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Else, we keep reading lines with <code class="code">N</code> until we
|
|
# find a different one
|
|
s/.*\n//
|
|
N
|
|
/^\(.*\)\n\1$/ {
|
|
bc
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># Remove the last instance of the duplicate line
|
|
# and go back to the top
|
|
D
|
|
</pre></div></div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="section-level-extent" id="cat-_002ds">
|
|
<div class="nav-panel">
|
|
<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> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h3 class="section" id="Squeezing-Blank-Lines"><span>7.21 Squeezing Blank Lines<a class="copiable-link" href="#Squeezing-Blank-Lines"> ¶</a></span></h3>
|
|
|
|
<p>As a final example, here are three scripts, of increasing complexity
|
|
and speed, that implement the same function as ‘<samp class="samp">cat -s</samp>’, 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-preformatted">#!/usr/bin/sed -f
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># on empty lines, join with next
|
|
# Note there is a star in the regexp
|
|
:x
|
|
/^\n*$/ {
|
|
N
|
|
bx
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># now, squeeze all '\n', this can be also done by:
|
|
# s/^\(\n\)*/\1/
|
|
s/\n*/\
|
|
/
|
|
</pre></div></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-preformatted">#!/usr/bin/sed -f
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># delete all leading empty lines
|
|
1,/^./{
|
|
/./!d
|
|
}
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># on an empty line we remove it and all the following
|
|
# empty lines, but one
|
|
:x
|
|
/./!{
|
|
N
|
|
s/^\n$//
|
|
tx
|
|
}
|
|
</pre></div></div>
|
|
|
|
<p>This removes leading and trailing blank lines. It is also the
|
|
fastest. Note that loops are completely done with <code class="code">n</code> and
|
|
<code class="code">b</code>, without relying on <code class="command">sed</code> to restart the
|
|
script automatically at the end of a line.
|
|
</p>
|
|
<div class="example">
|
|
<pre class="example-preformatted">#!/usr/bin/sed -nf
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># delete all (leading) blanks
|
|
/./!d
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># get here: so there is a non empty
|
|
:x
|
|
# print it
|
|
p
|
|
# get next
|
|
n
|
|
# got chars? print it again, etc...
|
|
/./bx
|
|
</pre></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
</pre><div class="group"><pre class="example-preformatted"># 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></div><pre class="example-preformatted">
|
|
|
|
bx
|
|
</pre></div>
|
|
|
|
<hr>
|
|
</div>
|
|
</div>
|
|
<div class="chapter-level-extent" id="Limitations">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Other-Resources" accesskey="n" rel="next">Other Resources for Learning About <code class="command">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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="GNU-sed_0027s-Limitations-and-Non_002dlimitations"><span>8 GNU <code class="command">sed</code>’s Limitations and Non-limitations<a class="copiable-link" href="#GNU-sed_0027s-Limitations-and-Non_002dlimitations"> ¶</a></span></h2>
|
|
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-unlimited-line-length"></a>
|
|
<a class="index-entry-id" id="index-Portability_002c-line-length-limitations"></a>
|
|
<p>For those who want to write portable <code class="command">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 class="sc">POSIX</small> standard specifies that conforming <code class="command">sed</code>
|
|
implementations shall support at least 8192 byte line lengths.
|
|
GNU <code class="command">sed</code> has no built-in limit on line length;
|
|
as long as it can <code class="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-level-extent" id="Other-Resources">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="#Reporting-Bugs" accesskey="n" rel="next">Reporting Bugs</a>, Previous: <a href="#Limitations" accesskey="p" rel="prev">GNU <code class="command">sed</code>’s Limitations and Non-limitations</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Other-Resources-for-Learning-About-sed"><span>9 Other Resources for Learning About <code class="command">sed</code><a class="copiable-link" href="#Other-Resources-for-Learning-About-sed"> ¶</a></span></h2>
|
|
|
|
<p>For up to date information about GNU <code class="command">sed</code> please
|
|
visit <a class="uref" href="https://www.gnu.org/software/sed/">https://www.gnu.org/software/sed/</a>.
|
|
</p>
|
|
<p>Send general questions and suggestions to <a class="email" href="mailto:sed-devel@gnu.org">sed-devel@gnu.org</a>.
|
|
Visit the mailing list archives for past discussions at
|
|
<a class="uref" href="https://lists.gnu.org/archive/html/sed-devel/">https://lists.gnu.org/archive/html/sed-devel/</a>.
|
|
</p>
|
|
<a class="index-entry-id" id="index-Additional-reading-about-sed"></a>
|
|
<p>The following resources provide information about <code class="command">sed</code>
|
|
(both GNU <code class="command">sed</code> and other variations). Note these not maintained by
|
|
GNU <code class="command">sed</code> developers.
|
|
</p>
|
|
<ul class="itemize mark-bullet">
|
|
<li>sed <code class="code">$HOME</code>: <a class="uref" href="http://sed.sf.net">http://sed.sf.net</a>
|
|
|
|
</li><li>sed FAQ: <a class="uref" href="http://sed.sf.net/sedfaq.html">http://sed.sf.net/sedfaq.html</a>
|
|
|
|
</li><li>seder’s grabbag: <a class="uref" href="http://sed.sf.net/grabbag">http://sed.sf.net/grabbag</a>
|
|
|
|
</li><li>The <code class="code">sed-users</code> mailing list maintained by Sven Guckes:
|
|
<a class="uref" href="http://groups.yahoo.com/group/sed-users/">http://groups.yahoo.com/group/sed-users/</a>
|
|
(note this is <em class="emph">not</em> the GNU <code class="command">sed</code> mailing list).
|
|
|
|
</li></ul>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="chapter-level-extent" id="Reporting-Bugs">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="chapter" id="Reporting-Bugs-1"><span>10 Reporting Bugs<a class="copiable-link" href="#Reporting-Bugs-1"> ¶</a></span></h2>
|
|
|
|
<a class="index-entry-id" id="index-Bugs_002c-reporting"></a>
|
|
<p>Email bug reports to <a class="email" href="mailto:bug-sed@gnu.org">bug-sed@gnu.org</a>.
|
|
Also, please include the output of ‘<samp class="samp">sed --version</samp>’ 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-preformatted"><i class="i"><i class="i"><span class="r">while building frobme-1.3.4</span></i></i>
|
|
$ configure
|
|
error→ sed: file sedscr line 1: Unknown option to 's'
|
|
</pre></div>
|
|
|
|
<p>If GNU <code class="command">sed</code> doesn’t configure your favorite package, take a
|
|
few extra minutes to identify the specific problem and make a stand-alone
|
|
test case. Unlike other programs such as C compilers, making such test
|
|
cases for <code class="command">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 class="command">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 class="command">sed</code> as “try to configure
|
|
frobme-1.3.4”. Yes, that is in principle enough information to look
|
|
for the bug, but that is not a very practical prospect.
|
|
</p>
|
|
<p>Here are a few commonly reported bugs that are not bugs.
|
|
</p>
|
|
<dl class="table">
|
|
<dd><a class="anchor" id="N_005fcommand_005flast_005fline"></a></dd>
|
|
<dt><a class="index-entry-id" id="index-Non_002dbugs_002c-N-command-on-the-last-line"></a>
|
|
<a id="index-Portability_002c-N-command-on-the-last-line"></a><span><code class="code">N</code> command on the last line<a class="copiable-link" href="#index-Portability_002c-N-command-on-the-last-line"> ¶</a></span></dt>
|
|
<dd>
|
|
<p>Most versions of <code class="command">sed</code> exit without printing anything when
|
|
the <code class="command">N</code> command is issued on the last line of a file.
|
|
GNU <code class="command">sed</code> prints pattern space before exiting unless of course
|
|
the <code class="command">-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-preformatted">$ seq 3 | sed N
|
|
1
|
|
2
|
|
3
|
|
</pre></div>
|
|
<p>To force POSIX-conforming behavior:
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">$ seq 3 | sed --posix N
|
|
1
|
|
2
|
|
</pre></div>
|
|
|
|
<p>For example, the behavior of
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">sed N foo bar
|
|
</pre></div>
|
|
<p>would depend on whether foo has an even or an odd number of
|
|
lines<a class="footnote" 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 class="code">sed</code> would force you to write
|
|
something like
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">/foo/{ $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N }
|
|
</pre></div>
|
|
<p>instead of just
|
|
</p><div class="example">
|
|
<pre class="example-preformatted">/foo/{ N;N;N;N;N;N;N;N;N; }
|
|
</pre></div>
|
|
|
|
<a class="index-entry-id" id="index-POSIXLY_005fCORRECT-behavior_002c-N-command"></a>
|
|
<p>In any case, the simplest workaround is to use <code class="code">$d;N</code> in
|
|
scripts that rely on the traditional behavior, or to set
|
|
the <code class="code">POSIXLY_CORRECT</code> variable to a non-empty value.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-Non_002dbugs_002c-regex-syntax-clashes"></a>
|
|
<a id="index-GNU-extensions_002c-to-basic-regular-expressions-5"></a><span>Regex syntax clashes (problems with backslashes)<a class="copiable-link" href="#index-GNU-extensions_002c-to-basic-regular-expressions-5"> ¶</a></span></dt>
|
|
<dd><p><code class="command">sed</code> uses the <small class="sc">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 class="command">sed</code> are <code class="code">\|</code>,
|
|
<code class="code">\+</code>, <code class="code">\?</code>, <code class="code">\`</code>, <code class="code">\'</code>, <code class="code">\<</code>,
|
|
<code class="code">\></code>, <code class="code">\b</code>, <code class="code">\B</code>, <code class="code">\w</code>, and <code class="code">\W</code>.
|
|
</p>
|
|
<p>As in all GNU programs that use <small class="sc">POSIX</small> basic regular
|
|
expressions, <code class="command">sed</code> interprets these escape sequences as special
|
|
characters. So, <code class="code">x\+</code> matches one or more occurrences of ‘<samp class="samp">x</samp>’.
|
|
<code class="code">abc\|def</code> matches either ‘<samp class="samp">abc</samp>’ or ‘<samp class="samp">def</samp>’.
|
|
</p>
|
|
<p>This syntax may cause problems when running scripts written for other
|
|
<code class="command">sed</code>s. Some <code class="command">sed</code> programs have been written with the
|
|
assumption that <code class="code">\|</code> and <code class="code">\+</code> match the literal characters
|
|
<code class="code">|</code> and <code class="code">+</code>. Such scripts must be modified by removing the
|
|
spurious backslashes if they are to be used with modern implementations
|
|
of <code class="command">sed</code>, like
|
|
GNU <code class="command">sed</code>.
|
|
</p>
|
|
<p>On the other hand, some scripts use s|abc\|def||g to remove occurrences
|
|
of <em class="emph">either</em> <code class="code">abc</code> or <code class="code">def</code>. While this worked until
|
|
<code class="command">sed</code> 4.0.x, newer versions interpret this as removing the
|
|
string <code class="code">abc|def</code>. This is again undefined behavior according to
|
|
POSIX, and this interpretation is arguably more robust: older
|
|
<code class="command">sed</code>s, for example, required that the regex matcher parsed
|
|
<code class="code">\/</code> as <code class="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>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-special-escapes-1"></a>
|
|
<p>In addition, this version of <code class="command">sed</code> supports several escape characters
|
|
(some of which are multi-character) to insert non-printable characters
|
|
in scripts (<code class="code">\a</code>, <code class="code">\c</code>, <code class="code">\d</code>, <code class="code">\o</code>, <code class="code">\r</code>,
|
|
<code class="code">\t</code>, <code class="code">\v</code>, <code class="code">\x</code>). These can cause similar problems
|
|
with scripts written for other <code class="command">sed</code>s.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-in_002dplace-editing-1"></a>
|
|
<a class="index-entry-id" id="index-Non_002dbugs_002c-in_002dplace-editing"></a>
|
|
<a id="index-In_002dplace-editing"></a><span><samp class="option">-i</samp> clobbers read-only files<a class="copiable-link" href="#index-In_002dplace-editing"> ¶</a></span></dt>
|
|
<dd>
|
|
<p>In short, ‘<samp class="samp">sed -i</samp>’ will let you delete the contents of
|
|
a read-only file, and in general the <samp class="option">-i</samp> option
|
|
(see <a class="pxref" 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. ‘<samp class="samp">sed -i</samp>’
|
|
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’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 class="command">sed</code> does not let you use <samp class="option">-i</samp> on a writable file
|
|
in a read-only directory, and will break hard or symbolic links when
|
|
<samp class="option">-i</samp> is used on such a file.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-0-address-1"></a>
|
|
<a class="index-entry-id" id="index-Non_002dbugs_002c-0-address"></a>
|
|
<a id="index-0-address"></a><span><code class="code">0a</code> does not work (gives an error)<a class="copiable-link" href="#index-0-address"> ¶</a></span></dt>
|
|
<dd>
|
|
<p>There is no line 0. 0 is a special address that is only used to treat
|
|
addresses like <code class="code">0,/<var class="var">RE</var>/</code> as active when the script starts: if
|
|
you write <code class="code">1,/abc/d</code> and the first line includes the string ‘<samp class="samp">abc</samp>’,
|
|
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 ‘<samp class="samp">abc</samp>’, and this
|
|
is obtained with <code class="code">0,/abc/d</code>.
|
|
</p>
|
|
</dd>
|
|
<dt><a id="index-Non_002dbugs_002c-localization_002drelated"></a><span><code class="code">[a-z]</code> is case insensitive<a class="copiable-link" href="#index-Non_002dbugs_002c-localization_002drelated"> ¶</a></span></dt>
|
|
<dd>
|
|
<p>You are encountering problems with locales. POSIX mandates that <code class="code">[a-z]</code>
|
|
uses the current locale’s collation order – in C parlance, that means using
|
|
<code class="code">strcoll(3)</code> instead of <code class="code">strcmp(3)</code>. Some locales have a
|
|
case-insensitive collation order, others don’t.
|
|
</p>
|
|
<p>Another problem is that <code class="code">[a-z]</code> tries to use collation symbols.
|
|
This only happens if you are on the GNU system, using
|
|
GNU libc’s regular expression matcher instead of compiling the
|
|
one supplied with GNU sed. In a Danish locale, for example,
|
|
the regular expression <code class="code">^[a-z]$</code> matches the string ‘<samp class="samp">aa</samp>’,
|
|
because this is a single collating symbol that comes after ‘<samp class="samp">a</samp>’
|
|
and before ‘<samp class="samp">b</samp>’; ‘<samp class="samp">ll</samp>’ behaves similarly in Spanish
|
|
locales, or ‘<samp class="samp">ij</samp>’ in Dutch locales.
|
|
</p>
|
|
<p>To work around these problems, which may cause bugs in shell scripts, set
|
|
the <code class="env">LC_COLLATE</code> and <code class="env">LC_CTYPE</code> environment variables to ‘<samp class="samp">C</samp>’.
|
|
</p>
|
|
</dd>
|
|
<dt><a class="index-entry-id" id="index-GNU-extensions_002c-emptying-pattern-space-1"></a>
|
|
<a class="index-entry-id" id="index-Emptying-pattern-space-1"></a>
|
|
<a id="index-Non_002dbugs_002c-localization_002drelated-1"></a><span><code class="code">s/.*//</code> does not clear pattern space<a class="copiable-link" href="#index-Non_002dbugs_002c-localization_002drelated-1"> ¶</a></span></dt>
|
|
<dd>
|
|
<p>This happens if your input stream includes invalid multibyte
|
|
sequences. <small class="sc">POSIX</small> mandates that such sequences
|
|
are <em class="emph">not</em> matched by ‘<samp class="samp">.</samp>’, so that ‘<samp class="samp">s/.*//</samp>’ will not clear
|
|
pattern space as you would expect. In fact, there is no way to clear
|
|
sed’s buffers in the middle of the script in most multibyte locales
|
|
(including UTF-8 locales). For this reason, GNU <code class="command">sed</code> provides a <code class="code">z</code>
|
|
command (for “zap”) as an extension.
|
|
</p>
|
|
<p>To work around these problems, which may cause bugs in shell scripts, set
|
|
the <code class="env">LC_COLLATE</code> and <code class="env">LC_CTYPE</code> environment variables to ‘<samp class="samp">C</samp>’.
|
|
</p></dd>
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="appendix-level-extent" id="GNU-Free-Documentation-License">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="appendix" id="GNU-Free-Documentation-License-1"><span>Appendix A GNU Free Documentation License<a class="copiable-link" href="#GNU-Free-Documentation-License-1"> ¶</a></span></h2>
|
|
|
|
<div class="center">Version 1.3, 3 November 2008
|
|
</div>
|
|
|
|
<div class="display">
|
|
<pre class="display-preformatted">Copyright © 2000–2026 Free Software Foundation, Inc.
|
|
<a class="uref" 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 class="enumerate" start="0">
|
|
<li> PREAMBLE
|
|
|
|
<p>The purpose of this License is to make a manual, textbook, or other
|
|
functional and useful document <em class="dfn">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 “copyleft”, 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 “Document”, below,
|
|
refers to any such manual or work. Any member of the public is a
|
|
licensee, and is addressed as “you”. You accept the license if you
|
|
copy, modify or distribute the work in a way requiring permission
|
|
under copyright law.
|
|
</p>
|
|
<p>A “Modified Version” 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 “Secondary Section” 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’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 “Invariant Sections” 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 “Cover Texts” 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 “Transparent” 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 “Transparent” is called “Opaque”.
|
|
</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 “Title Page” 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, “Title Page” means
|
|
the text near the most prominent appearance of the work’s title,
|
|
preceding the beginning of the body of the text.
|
|
</p>
|
|
<p>The “publisher” means any person or entity that distributes copies
|
|
of the Document to the public.
|
|
</p>
|
|
<p>A section “Entitled XYZ” 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 “Acknowledgements”,
|
|
“Dedications”, “Endorsements”, or “History”.) To “Preserve the Title”
|
|
of such a section when you modify the Document means that it remains a
|
|
section “Entitled XYZ” 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’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 class="enumerate" 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’s license notice.
|
|
|
|
</li><li> Include an unaltered copy of this License.
|
|
|
|
</li><li> Preserve the section Entitled “History”, 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 “History” 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 “History” 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 “Acknowledgements” or “Dedications”, 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 “Endorsements”. Such a section
|
|
may not be included in the Modified Version.
|
|
|
|
</li><li> Do not retitle any existing section to be Entitled “Endorsements” 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’s license notice.
|
|
These titles must be distinct from any other section titles.
|
|
</p>
|
|
<p>You may add a section Entitled “Endorsements”, provided it contains
|
|
nothing but endorsements of your Modified Version by various
|
|
parties—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 “History”
|
|
in the various original documents, forming one section Entitled
|
|
“History”; likewise combine any sections Entitled “Acknowledgements”,
|
|
and any sections Entitled “Dedications”. You must delete all
|
|
sections Entitled “Endorsements.”
|
|
</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 “aggregate” if the copyright
|
|
resulting from the compilation is not used to limit the legal rights
|
|
of the compilation’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’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 “Acknowledgements”,
|
|
“Dedications”, or “History”, 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 class="uref" 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 “or any later version” 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’s public statement of acceptance of a
|
|
version permanently authorizes you to choose that version for the
|
|
Document.
|
|
</p>
|
|
</li><li> RELICENSING
|
|
|
|
<p>“Massive Multiauthor Collaboration Site” (or “MMC Site”) 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
|
|
“Massive Multiauthor Collaboration” (or “MMC”) contained in the
|
|
site means any set of copyrightable works thus published on the MMC
|
|
site.
|
|
</p>
|
|
<p>“CC-BY-SA” 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>“Incorporate” means to publish or republish a Document, in whole or
|
|
in part, as part of another Document.
|
|
</p>
|
|
<p>An MMC is “eligible for relicensing” 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>
|
|
|
|
<h3 class="heading" id="ADDENDUM_003a-How-to-use-this-License-for-your-documents"><span>ADDENDUM: How to use this License for your documents<a class="copiable-link" href="#ADDENDUM_003a-How-to-use-this-License-for-your-documents"> ¶</a></span></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 smallexample">
|
|
<div class="group"><pre class="example-preformatted"> Copyright (C) <var class="var">year</var> <var class="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></div>
|
|
|
|
<p>If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts,
|
|
replace the “with…Texts.” line with this:
|
|
</p>
|
|
<div class="example smallexample">
|
|
<div class="group"><pre class="example-preformatted"> with the Invariant Sections being <var class="var">list their titles</var>, with
|
|
the Front-Cover Texts being <var class="var">list</var>, and with the Back-Cover Texts
|
|
being <var class="var">list</var>.
|
|
</pre></div></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-level-extent" id="Concept-Index">
|
|
<div class="nav-panel">
|
|
<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 class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="unnumbered" id="Concept-Index-1"><span>Concept Index<a class="copiable-link" href="#Concept-Index-1"> ¶</a></span></h2>
|
|
|
|
<p>This is a general index of all issues discussed in this manual, with the
|
|
exception of the <code class="command">sed</code> commands and command-line options.
|
|
</p>
|
|
<div class="printindex cp-printindex">
|
|
<table class="cp-letters-header-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-1"><b>-</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-2"><b>;</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-3"><b>0</b></a>
|
|
|
|
<br>
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-A"><b>A</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-B"><b>B</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-C"><b>C</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-D"><b>D</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-E"><b>E</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-F"><b>F</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-G"><b>G</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-H"><b>H</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-I"><b>I</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-J"><b>J</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-L"><b>L</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-M"><b>M</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-N"><b>N</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-O"><b>O</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-P"><b>P</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-Q"><b>Q</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-R"><b>R</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-S"><b>S</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-T"><b>T</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-U"><b>U</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-V"><b>V</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-W"><b>W</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-X"><b>X</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-Z"><b>Z</b></a>
|
|
|
|
</td></tr></table>
|
|
<table class="cp-entries-printindex" border="0">
|
|
<tr><td></td><th class="entries-header-printindex">Index Entry</th><th class="sections-header-printindex">Section</th></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_symbol-1">-</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002de_002c-example">-e, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002de_002c-example-1">-e, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dexpression_002c-example">–expression, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002df_002c-example">-f, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002df_002c-example-1">-f, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dfile_002c-example">–file, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002di_002c-example">-i, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dn_002c-example">-n, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002ds_002c-example">-s, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_symbol-2">;</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_003b_002c-command-separator">;, command separator</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_symbol-3">0</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-0-address"><code class="code">0</code> address</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-A">A</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-a_002c-and-semicolons"><code class="command">a</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Additional-reading-about-sed">Additional reading about <code class="command">sed</code></a></td><td class="printindex-index-section"><a href="#Other-Resources">Other Resources</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addr1_002c_002bN"><var class="var">addr1</var>,+N</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addr1_002c_007eN"><var class="var">addr1</var>,~N</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-address-range_002c-example">address range, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Address_002c-as-a-regular-expression">Address, as a regular expression</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Address_002c-last-line">Address, last line</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Address_002c-numeric">Address, numeric</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-excluding">addresses, excluding</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Addresses_002c-in-sed-scripts">Addresses, in <code class="command">sed</code> scripts</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-negating">addresses, negating</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-numeric">addresses, numeric</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-range">addresses, range</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-regular-expression">addresses, regular expression</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-addresses_002c-syntax">addresses, syntax</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-alphabetic-characters">alphabetic characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-alphanumeric-characters">alphanumeric characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Append-hold-space-to-pattern-space">Append hold space to pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Append-next-input-line-to-pattern-space">Append next input line to pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Append-pattern-space-to-hold-space">Append pattern space to hold space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Appending-text-after-a-line">Appending text after a line</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-B">B</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-b_002c-joining-lines-with">b, joining lines with</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-b_002c-versus-t">b, versus t</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-back_002dreference">back-reference</a></td><td class="printindex-index-section"><a href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Backreferences_002c-in-regular-expressions">Backreferences, in regular expressions</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-blank-characters">blank characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-bracket-expression">bracket expression</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Branch-to-a-label_002c-if-s_002f_002f_002f-failed">Branch to a label, if <code class="code">s///</code> failed</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Branch-to-a-label_002c-if-s_002f_002f_002f-succeeded">Branch to a label, if <code class="code">s///</code> succeeded</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Branch-to-a-label_002c-unconditionally">Branch to a label, unconditionally</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-branching-and-n_002c-N">branching and n, N</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-branching_002c-infinite-loop">branching, infinite loop</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-branching_002c-joining-lines">branching, joining lines</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Buffer-spaces_002c-pattern-and-hold">Buffer spaces, pattern and hold</a></td><td class="printindex-index-section"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Bugs_002c-reporting">Bugs, reporting</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-C">C</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-c_002c-and-semicolons"><code class="command">c</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-case-insensitive_002c-regular-expression">case insensitive, regular expression</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Case_002dinsensitive-matching">Case-insensitive matching</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Caveat-_002d_002d_002d-_0023n-on-first-line">Caveat — #n on first line</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-character-class">character class</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-character-classes">character classes</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-classes-of-characters">classes of characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Command-groups">Command groups</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Comments_002c-in-scripts">Comments, in scripts</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Conditional-branch">Conditional branch</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Conditional-branch-1">Conditional branch</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-control-characters">control characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Copy-hold-space-into-pattern-space">Copy hold space into pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Copy-pattern-space-into-hold-space">Copy pattern space into hold space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-cycle_002c-restarting">cycle, restarting</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-D">D</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-d_002c-example"><code class="command">d</code>, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Delete-first-line-from-pattern-space">Delete first line from pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-digit-characters">digit characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Disabling-autoprint_002c-from-command-line">Disabling autoprint, from command line</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-E">E</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-e_002c-and-semicolons"><code class="command">e</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-empty-regular-expression">empty regular expression</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Emptying-pattern-space">Emptying pattern space</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Emptying-pattern-space-1">Emptying pattern space</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Evaluate-Bourne_002dshell-commands">Evaluate Bourne-shell commands</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution">Evaluate Bourne-shell commands, after substitution</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-example_002c-address-range">example, address range</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-example_002c-regular-expression">example, regular expression</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Exchange-hold-space-with-pattern-space">Exchange hold space with pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Excluding-lines">Excluding lines</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-exit-status">exit status</a></td><td class="printindex-index-section"><a href="#Exit-status">Exit status</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-exit-status_002c-example">exit status, example</a></td><td class="printindex-index-section"><a href="#Exit-status">Exit status</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Extended-regular-expressions_002c-choosing">Extended regular expressions, choosing</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Extended-regular-expressions_002c-syntax">Extended regular expressions, syntax</a></td><td class="printindex-index-section"><a href="#ERE-syntax">ERE syntax</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-F">F</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-File-name_002c-printing">File name, printing</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Files-to-be-processed-as-input">Files to be processed as input</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Flow-of-control-in-scripts">Flow of control in scripts</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-G">G</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Global-substitution">Global substitution</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstderr-file">GNU extensions, <samp class="file">/dev/stderr</samp> file</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstderr-file-1">GNU extensions, <samp class="file">/dev/stderr</samp> file</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstdin-file">GNU extensions, <samp class="file">/dev/stdin</samp> file</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstdin-file-1">GNU extensions, <samp class="file">/dev/stdin</samp> file</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file">GNU extensions, <samp class="file">/dev/stdout</samp> file</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file-1">GNU extensions, <samp class="file">/dev/stdout</samp> file</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-_002fdev_002fstdout-file-2">GNU extensions, <samp class="file">/dev/stdout</samp> file</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-0-address">GNU extensions, <code class="code">0</code> address</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-0-address-1">GNU extensions, <code class="code">0</code> address</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-0_002caddr2-addressing">GNU extensions, 0,<var class="var">addr2</var> addressing</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-addr1_002c_002bN-addressing">GNU extensions, <var class="var">addr1</var>,+<var class="var">N</var> addressing</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-addr1_002c_007eN-addressing">GNU extensions, <var class="var">addr1</var>,~<var class="var">N</var> addressing</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-branch-if-s_002f_002f_002f-failed">GNU extensions, branch if <code class="code">s///</code> failed</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-case-modifiers-in-s-commands">GNU extensions, case modifiers in <code class="code">s</code> commands</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-checking-for-their-presence">GNU extensions, checking for their presence</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-debug">GNU extensions, debug</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-disabling">GNU extensions, disabling</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-emptying-pattern-space">GNU extensions, emptying pattern space</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-emptying-pattern-space-1">GNU extensions, emptying pattern space</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands">GNU extensions, evaluating Bourne-shell commands</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands-1">GNU extensions, evaluating Bourne-shell commands</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-extended-regular-expressions">GNU extensions, extended regular expressions</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-g-and-number-modifier">GNU extensions, <code class="code">g</code> and <var class="var">number</var> modifier</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-I-modifier">GNU extensions, <code class="code">I</code> modifier</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-I-modifier-1">GNU extensions, <code class="code">I</code> modifier</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-in_002dplace-editing">GNU extensions, in-place editing</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-in_002dplace-editing-1">GNU extensions, in-place editing</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-M-modifier">GNU extensions, <code class="code">M</code> modifier</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-M-modifier-1">GNU extensions, <code class="code">M</code> modifier</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-modifiers-and-the-empty-regular-expression">GNU extensions, modifiers and the empty regular expression</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-n_007em-addresses">GNU extensions, ‘<samp class="samp"><var class="var">n</var>~<var class="var">m</var></samp>’ addresses</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-quitting-silently">GNU extensions, quitting silently</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-R-command">GNU extensions, <code class="code">R</code> command</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><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 class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-returning-an-exit-code">GNU extensions, returning an exit code</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-returning-an-exit-code-1">GNU extensions, returning an exit code</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-setting-line-length">GNU extensions, setting line length</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-special-escapes">GNU extensions, special escapes</a></td><td class="printindex-index-section"><a href="#Escapes">Escapes</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-special-escapes-1">GNU extensions, special escapes</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-special-two_002daddress-forms">GNU extensions, special two-address forms</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-subprocesses">GNU extensions, subprocesses</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-subprocesses-1">GNU extensions, subprocesses</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-1">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-2">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-3">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-4">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-to-basic-regular-expressions-5">GNU extensions, to basic regular expressions</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands">GNU extensions, two addresses supported by most commands</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-1">GNU extensions, two addresses supported by most commands</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-2">GNU extensions, two addresses supported by most commands</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-two-addresses-supported-by-most-commands-3">GNU extensions, two addresses supported by most commands</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-unlimited-line-length">GNU extensions, unlimited line length</a></td><td class="printindex-index-section"><a href="#Limitations">Limitations</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-GNU-extensions_002c-writing-first-line-to-a-file">GNU extensions, writing first line to a file</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Goto_002c-in-scripts">Goto, in scripts</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-graphic-characters">graphic characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Greedy-regular-expression-matching">Greedy regular expression matching</a></td><td class="printindex-index-section"><a href="#BRE-syntax">BRE syntax</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Grouping-commands">Grouping commands</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-H">H</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-hexadecimal-digits">hexadecimal digits</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-appending-from-pattern-space">Hold space, appending from pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-appending-to-pattern-space">Hold space, appending to pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-copy-into-pattern-space">Hold space, copy into pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-copying-pattern-space-into">Hold space, copying pattern space into</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-definition">Hold space, definition</a></td><td class="printindex-index-section"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Hold-space_002c-exchange-with-pattern-space">Hold space, exchange with pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-I">I</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-i_002c-and-semicolons"><code class="command">i</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-In_002dplace-editing">In-place editing</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-In_002dplace-editing_002c-activating">In-place editing, activating</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names">In-place editing, Perl-style backup file names</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-infinite-loop_002c-branching">infinite loop, branching</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Inserting-text-before-a-line">Inserting text before a line</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-J">J</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-joining-lines-with-branching">joining lines with branching</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-joining-quoted_002dprintable-lines">joining quoted-printable lines</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-L">L</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-labels">labels</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Labels_002c-in-scripts">Labels, in scripts</a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Last-line_002c-selecting">Last line, selecting</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line-length_002c-setting">Line length, setting</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line-length_002c-setting-1">Line length, setting</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line-number_002c-printing">Line number, printing</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line-selection">Line selection</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line_002c-selecting-by-number">Line, selecting by number</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line_002c-selecting-by-regular-expression-match">Line, selecting by regular expression match</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Line_002c-selecting-last">Line, selecting last</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-List-pattern-space">List pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-lower_002dcase-letters">lower-case letters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-M">M</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Mixing-g-and-number-modifiers-in-the-s-command">Mixing <code class="code">g</code> and <var class="var">number</var> modifiers in the <code class="code">s</code> command</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-multiple-files">multiple files</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-multiple-sed-commands">multiple <code class="command">sed</code> commands</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-N">N</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-n_002c-and-branching">n, and branching</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-N_002c-and-branching">N, and branching</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-named-character-classes">named character classes</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-newline_002c-command-separator">newline, command separator</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Next-input-line_002c-append-to-pattern-space">Next input line, append to pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Next-input-line_002c-replace-pattern-space-with">Next input line, replace pattern space with</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-0-address">Non-bugs, <code class="code">0</code> address</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-in_002dplace-editing">Non-bugs, in-place editing</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-localization_002drelated">Non-bugs, localization-related</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-localization_002drelated-1">Non-bugs, localization-related</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-N-command-on-the-last-line">Non-bugs, <code class="code">N</code> command on the last line</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Non_002dbugs_002c-regex-syntax-clashes">Non-bugs, regex syntax clashes</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-numeric-addresses">numeric addresses</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-numeric-characters">numeric characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-O">O</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-omitting-labels">omitting labels</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-output">output</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-output_002c-suppressing">output, suppressing</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-P">P</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-p_002c-example">p, example</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-paragraphs_002c-processing">paragraphs, processing</a></td><td class="printindex-index-section"><a href="#Multiline-techniques">Multiline techniques</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-parameters_002c-script">parameters, script</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Parenthesized-substrings">Parenthesized substrings</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Pattern-space_002c-definition">Pattern space, definition</a></td><td class="printindex-index-section"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Portability_002c-comments">Portability, comments</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Portability_002c-line-length-limitations">Portability, line length limitations</a></td><td class="printindex-index-section"><a href="#Limitations">Limitations</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Portability_002c-N-command-on-the-last-line">Portability, <code class="code">N</code> command on the last line</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-bracket-expressions"><code class="code">POSIXLY_CORRECT</code> behavior, bracket expressions</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-enabling"><code class="code">POSIXLY_CORRECT</code> behavior, enabling</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-escapes"><code class="code">POSIXLY_CORRECT</code> behavior, escapes</a></td><td class="printindex-index-section"><a href="#Escapes">Escapes</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-POSIXLY_005fCORRECT-behavior_002c-N-command"><code class="code">POSIXLY_CORRECT</code> behavior, <code class="code">N</code> command</a></td><td class="printindex-index-section"><a href="#Reporting-Bugs">Reporting Bugs</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Print-first-line-from-pattern-space">Print first line from pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-printable-characters">printable characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Printing-file-name">Printing file name</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Printing-line-number">Printing line number</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Printing-text-unambiguously">Printing text unambiguously</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-processing-paragraphs">processing paragraphs</a></td><td class="printindex-index-section"><a href="#Multiline-techniques">Multiline techniques</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-punctuation-characters">punctuation characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-Q">Q</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Q_002c-example">Q, example</a></td><td class="printindex-index-section"><a href="#Exit-status">Exit status</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-q_002c-example"><code class="command">q</code>, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Quitting">Quitting</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Quitting-1">Quitting</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-quoted_002dprintable-lines_002c-joining">quoted-printable lines, joining</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-R">R</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-r_002c-and-semicolons"><code class="command">r</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-R_002c-and-semicolons"><code class="command">R</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-range-addresses">range addresses</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-range-expression">range expression</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Range-of-lines">Range of lines</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Range-with-start-address-of-zero">Range with start address of zero</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Read-next-input-line">Read next input line</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Read-text-from-a-file">Read text from a file</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Read-text-from-a-file-1">Read text from a file</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-regex-addresses-and-input-lines">regex addresses and input lines</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-regex-addresses-and-pattern-space">regex addresses and pattern space</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-regular-expression-addresses">regular expression addresses</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-regular-expression_002c-example">regular expression, example</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Replace-hold-space-with-copy-of-pattern-space">Replace hold space with copy of pattern space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Replace-pattern-space-with-copy-of-hold-space">Replace pattern space with copy of hold space</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Replacing-all-text-matching-regexp-in-a-line">Replacing all text matching regexp in a line</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Replacing-only-nth-match-of-regexp-in-a-line">Replacing only <var class="var">n</var>th match of regexp in a line</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Replacing-selected-lines-with-other-text">Replacing selected lines with other text</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Requiring-GNU-sed">Requiring GNU <code class="command">sed</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-restarting-a-cycle">restarting a cycle</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-S">S</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Sandbox-mode">Sandbox mode</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-script-parameter">script parameter</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Script-structure">Script structure</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Script_002c-from-a-file">Script, from a file</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Script_002c-from-command-line">Script, from command line</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-sed-commands-syntax"><code class="command">sed</code> commands syntax</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-sed-commands_002c-multiple"><code class="command">sed</code> commands, multiple</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-sed-script-structure"><code class="command">sed</code> script structure</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Selecting-lines-to-process">Selecting lines to process</a></td><td class="printindex-index-section"><a href="#Numeric-Addresses">Numeric Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Selecting-non_002dmatching-lines">Selecting non-matching lines</a></td><td class="printindex-index-section"><a href="#Addresses-overview">Addresses overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-semicolons_002c-command-separator">semicolons, command separator</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Several-lines_002c-selecting">Several lines, selecting</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Slash-character_002c-in-regular-expressions">Slash character, in regular expressions</a></td><td class="printindex-index-section"><a href="#Regexp-Addresses">Regexp Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-space-characters">space characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Spaces_002c-pattern-and-hold">Spaces, pattern and hold</a></td><td class="printindex-index-section"><a href="#Execution-Cycle">Execution Cycle</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Special-addressing-forms">Special addressing forms</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-standard-input">standard input</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Standard-input_002c-processing-as-input">Standard input, processing as input</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-standard-output">standard output</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-stdin">stdin</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-stdout">stdout</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Stream-editor">Stream editor</a></td><td class="printindex-index-section"><a href="#Introduction">Introduction</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-subexpression">subexpression</a></td><td class="printindex-index-section"><a href="#Back_002dreferences-and-Subexpressions">Back-references and Subexpressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Subprocesses">Subprocesses</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Subprocesses-1">Subprocesses</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Substitution-of-text_002c-options">Substitution of text, options</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-suppressing-output">suppressing output</a></td><td class="printindex-index-section"><a href="#Overview">Overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-syntax_002c-addresses">syntax, addresses</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-syntax_002c-sed-commands">syntax, <code class="command">sed</code> commands</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-T">T</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-t_002c-joining-lines-with">t, joining lines with</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-t_002c-versus-b">t, versus b</a></td><td class="printindex-index-section"><a href="#Branching-and-flow-control">Branching and flow control</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-appending">Text, appending</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-deleting">Text, deleting</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-insertion">Text, insertion</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-printing">Text, printing</a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-printing-after-substitution">Text, printing after substitution</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Text_002c-writing-to-a-file-after-substitution">Text, writing to a file after substitution</a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Transliteration">Transliteration</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-U">U</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Unbuffered-I_002fO_002c-choosing">Unbuffered I/O, choosing</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-upper_002dcase-letters">upper-case letters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Usage-summary_002c-printing">Usage summary, printing</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-V">V</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Version_002c-printing">Version, printing</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-W">W</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-w_002c-and-semicolons"><code class="command">w</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-W_002c-and-semicolons"><code class="command">W</code>, and semicolons</a></td><td class="printindex-index-section"><a href="#sed-script-overview">sed script overview</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-whitespace-characters">whitespace characters</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Working-on-separate-files">Working on separate files</a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Write-first-line-to-a-file">Write first line to a file</a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Write-to-a-file">Write to a file</a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-X">X</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-xdigit-class">xdigit class</a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Concept-Index_cp_letter-Z">Z</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Zero-Address">Zero Address</a></td><td class="printindex-index-section"><a href="#Zero-Address">Zero Address</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Zero_002c-as-range-start-address">Zero, as range start address</a></td><td class="printindex-index-section"><a href="#Range-Addresses">Range Addresses</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
</table>
|
|
<table class="cp-letters-footer-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-1"><b>-</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-2"><b>;</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-3"><b>0</b></a>
|
|
|
|
<br>
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-A"><b>A</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-B"><b>B</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-C"><b>C</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-D"><b>D</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-E"><b>E</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-F"><b>F</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-G"><b>G</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-H"><b>H</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-I"><b>I</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-J"><b>J</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-L"><b>L</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-M"><b>M</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-N"><b>N</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-O"><b>O</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-P"><b>P</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-Q"><b>Q</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-R"><b>R</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-S"><b>S</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-T"><b>T</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-U"><b>U</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-V"><b>V</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-W"><b>W</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-X"><b>X</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Concept-Index_cp_letter-Z"><b>Z</b></a>
|
|
|
|
</td></tr></table>
|
|
</div>
|
|
|
|
<hr>
|
|
</div>
|
|
<div class="unnumbered-level-extent" id="Command-and-Option-Index">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Previous: <a href="#Concept-Index" accesskey="p" rel="prev">Concept Index</a>, Up: <a href="#Top" accesskey="u" rel="up">GNU <code class="command">sed</code></a> [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<h2 class="unnumbered" id="Command-and-Option-Index-1"><span>Command and Option Index<a class="copiable-link" href="#Command-and-Option-Index-1"> ¶</a></span></h2>
|
|
|
|
<p>This is an alphabetical list of all <code class="command">sed</code> commands and command-line
|
|
options.
|
|
</p>
|
|
<div class="printindex fn-printindex">
|
|
<table class="fn-letters-header-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-1"><b>-</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-2"><b>:</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-3"><b>{</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-4"><b>#</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-5"><b>=</b></a>
|
|
|
|
<br>
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-A"><b>A</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-B"><b>B</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-C"><b>C</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-D"><b>D</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-E"><b>E</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-F"><b>F</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-G"><b>G</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-H"><b>H</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-I"><b>I</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-L"><b>L</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-N"><b>N</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-P"><b>P</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Q"><b>Q</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-R"><b>R</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-S"><b>S</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-T"><b>T</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-U"><b>U</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-V"><b>V</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-W"><b>W</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-X"><b>X</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Y"><b>Y</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Z"><b>Z</b></a>
|
|
|
|
</td></tr></table>
|
|
<table class="fn-entries-printindex" border="0">
|
|
<tr><td></td><th class="entries-header-printindex">Index Entry</th><th class="sections-header-printindex">Section</th></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_symbol-1">-</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dbinary"><code>--binary</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002ddebug"><code>--debug</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dexpression"><code>--expression</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dfile"><code>--file</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dfollow_002dsymlinks"><code>--follow-symlinks</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dhelp"><code>--help</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002din_002dplace"><code>--in-place</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dline_002dlength"><code>--line-length</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dnull_002ddata"><code>--null-data</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dposix"><code>--posix</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dquiet"><code>--quiet</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dregexp_002dextended"><code>--regexp-extended</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dsandbox"><code>--sandbox</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dseparate"><code>--separate</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dsilent"><code>--silent</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dunbuffered"><code>--unbuffered</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dversion"><code>--version</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002d_002dzero_002dterminated"><code>--zero-terminated</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002db"><code>-b</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002de"><code>-e</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dE"><code>-E</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002df"><code>-f</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002di"><code>-i</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dl"><code>-l</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dn"><code>-n</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dn_002c-forcing-from-within-a-script"><code>-n, forcing from within a script</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dr"><code>-r</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002ds"><code>-s</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002du"><code>-u</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_002dz"><code>-z</code></a></td><td class="printindex-index-section"><a href="#Command_002dLine-Options">Command-Line Options</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_symbol-2">:</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_003a-_0028label_0029-command"><code>: (label) command</code></a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_symbol-3">{</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_007b_007d-command-grouping"><code>{} command grouping</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_symbol-4">#</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_0023-_0028comments_0029"><code># (comments)</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_symbol-5">=</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-_003d-_0028print-line-number_0029-command"><code>= (print line number) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-A">A</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-a-_0028append-text-lines_0029-command"><code>a (append text lines) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-alnum-character-class"><code>alnum <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-alpha-character-class"><code>alpha <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-B">B</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-b-_0028branch_0029-command"><code>b (branch) command</code></a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-blank-character-class"><code>blank <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-C">C</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-c-_0028change-to-text-lines_0029-command"><code>c (change to text lines) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-cntrl-character-class"><code>cntrl <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-D">D</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-D-_0028delete-first-line_0029-command"><code>D (delete first line) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-d-_0028delete_0029-command"><code>d (delete) command</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-digit-character-class"><code>digit <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-E">E</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-e-_0028evaluate_0029-command"><code>e (evaluate) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-F">F</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-F-_0028File-name_0029-command"><code>F (File name) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-G">G</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-G-_0028appending-Get_0029-command"><code>G (appending Get) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-g-_0028get_0029-command"><code>g (get) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-graph-character-class"><code>graph <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-H">H</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-H-_0028append-Hold_0029-command"><code>H (append Hold) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-h-_0028hold_0029-command"><code>h (hold) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-I">I</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-i-_0028insert-text-lines_0029-command"><code>i (insert text lines) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-L">L</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-l-_0028list-unambiguously_0029-command"><code>l (list unambiguously) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-lower-character-class"><code>lower <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-N">N</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-N-_0028append-Next-line_0029-command"><code>N (append Next line) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-n-_0028next_002dline_0029-command"><code>n (next-line) command</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-P">P</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-P-_0028print-first-line_0029-command"><code>P (print first line) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-p-_0028print_0029-command"><code>p (print) command</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-print-character-class"><code>print <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-punct-character-class"><code>punct <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-Q">Q</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-q-_0028quit_0029-command"><code>q (quit) command</code></a></td><td class="printindex-index-section"><a href="#Common-Commands">Common Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-Q-_0028silent-Quit_0029-command"><code>Q (silent Quit) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-R">R</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-r-_0028read-file_0029-command"><code>r (read file) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-R-_0028read-line_0029-command"><code>R (read line) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-S">S</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-s-command_002c-option-flags"><code>s command, option flags</code></a></td><td class="printindex-index-section"><a href="#The-_0022s_0022-Command">The "s" Command</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-space-character-class"><code>space <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-T">T</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-T-_0028test-and-branch-if-failed_0029-command"><code>T (test and branch if failed) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-t-_0028test-and-branch-if-successful_0029-command"><code>t (test and branch if successful) command</code></a></td><td class="printindex-index-section"><a href="#Programming-Commands">Programming Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-U">U</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-upper-character-class"><code>upper <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-V">V</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-v-_0028version_0029-command"><code>v (version) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-W">W</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-w-_0028write-file_0029-command"><code>w (write file) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-W-_0028write-first-line_0029-command"><code>W (write first line) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-X">X</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-x-_0028eXchange_0029-command"><code>x (eXchange) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-xdigit-character-class"><code>xdigit <span class="r">character class</span></code></a></td><td class="printindex-index-section"><a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-Y">Y</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-y-_0028transliterate_0029-command"><code>y (transliterate) command</code></a></td><td class="printindex-index-section"><a href="#Other-Commands">Other Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
<tr><th id="Command-and-Option-Index_fn_letter-Z">Z</th></tr>
|
|
<tr><td></td><td class="printindex-index-entry"><a href="#index-z-_0028Zap_0029-command"><code>z (Zap) command</code></a></td><td class="printindex-index-section"><a href="#Extended-Commands">Extended Commands</a></td></tr>
|
|
<tr><td colspan="3"><hr></td></tr>
|
|
</table>
|
|
<table class="fn-letters-footer-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-1"><b>-</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-2"><b>:</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-3"><b>{</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-4"><b>#</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_symbol-5"><b>=</b></a>
|
|
|
|
<br>
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-A"><b>A</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-B"><b>B</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-C"><b>C</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-D"><b>D</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-E"><b>E</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-F"><b>F</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-G"><b>G</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-H"><b>H</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-I"><b>I</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-L"><b>L</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-N"><b>N</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-P"><b>P</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Q"><b>Q</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-R"><b>R</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-S"><b>S</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-T"><b>T</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-U"><b>U</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-V"><b>V</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-W"><b>W</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-X"><b>X</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Y"><b>Y</b></a>
|
|
|
|
<a class="summary-letter-printindex" href="#Command-and-Option-Index_fn_letter-Z"><b>Z</b></a>
|
|
|
|
</td></tr></table>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="footnotes-segment">
|
|
<hr>
|
|
<h4 class="footnotes-heading">Footnotes</h4>
|
|
|
|
<h5 class="footnote-body-heading"><a id="FOOT1" href="#DOCF1">(1)</a></h5>
|
|
<p>This applies to commands such as <code class="code">=</code>,
|
|
<code class="code">a</code>, <code class="code">c</code>, <code class="code">i</code>, <code class="code">l</code>, <code class="code">p</code>. You can
|
|
still write to the standard output by using the <code class="code">w</code>
|
|
<a class="index-entry-id" id="index-GNU-extensions_002c-_002fdev_002fstdout-file"></a>
|
|
or <code class="code">W</code> commands together with the <samp class="file">/dev/stdout</samp>
|
|
special file</p>
|
|
<h5 class="footnote-body-heading"><a id="FOOT2" href="#DOCF2">(2)</a></h5>
|
|
<p>Note that GNU <code class="command">sed</code> creates the backup
|
|
file whether or not any output is actually changed.</p>
|
|
<h5 class="footnote-body-heading"><a id="FOOT3" href="#DOCF3">(3)</a></h5>
|
|
<p>This is equivalent to <code class="code">p</code> unless the <samp class="option">-i</samp>
|
|
option is being used.</p>
|
|
<h5 class="footnote-body-heading"><a id="FOOT4" href="#DOCF4">(4)</a></h5>
|
|
<p>This is equivalent to <code class="code">p</code> unless the <samp class="option">-i</samp>
|
|
option is being used.</p>
|
|
<h5 class="footnote-body-heading"><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-preformatted">grep 'bash$' /etc/passwd
|
|
awk -F: '$7 == "/bin/bash"' /etc/passwd
|
|
</pre></div>
|
|
<h5 class="footnote-body-heading"><a id="FOOT6" href="#DOCF6">(6)</a></h5>
|
|
<p>All
|
|
the escapes introduced here are GNU
|
|
extensions, with the exception of <code class="code">\n</code>. In basic regular
|
|
expression mode, setting <code class="code">POSIXLY_CORRECT</code> disables them inside
|
|
bracket expressions.</p>
|
|
<h5 class="footnote-body-heading"><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 class="footnote-body-heading"><a id="FOOT8" href="#DOCF8">(8)</a></h5>
|
|
<p>Actually,
|
|
if <code class="command">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 “least expected surprise”
|
|
even though it does not make commands like ‘<samp class="samp">sed -n p</samp>’ exactly
|
|
identical to <code class="command">cat</code>.</p>
|
|
<h5 class="footnote-body-heading"><a id="FOOT9" href="#DOCF9">(9)</a></h5>
|
|
<p><code class="command">sed</code> guru Greg
|
|
Ubben wrote an implementation of the <code class="command">dc</code> <small class="sc">RPN</small> calculator!
|
|
It is distributed together with sed.</p>
|
|
<h5 class="footnote-body-heading"><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-preformatted">#! /bin/sh
|
|
|
|
banner -w $1 $2 $3 $4 |
|
|
sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
|
|
~/sedscripts/reverseline.sed
|
|
</pre></div>
|
|
<h5 class="footnote-body-heading"><a id="FOOT11" href="#DOCF11">(11)</a></h5>
|
|
<p>Some implementations
|
|
have a limit of 199 commands per script</p>
|
|
<h5 class="footnote-body-heading"><a id="FOOT12" href="#DOCF12">(12)</a></h5>
|
|
<p>which is the actual “bug” that prompted the
|
|
change in behavior</p>
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|