HTML5 50
Sed.html Guest on 24th July 2020 12:58:50 PM
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <!-- This file documents version 4.8 of
  4. GNU sed, a stream editor.
  5.  
  6. Copyright (C) 1998-2020 Free Software Foundation, Inc.
  7.  
  8. Permission is granted to copy, distribute and/or modify this document
  9. under the terms of the GNU Free Documentation License, Version 1.3
  10. or any later version published by the Free Software Foundation;
  11. with no Invariant Sections, no Front-Cover Texts, and no
  12. Back-Cover Texts.  A copy of the license is included in the
  13. section entitled "GNU Free Documentation License". -->
  14. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>sed, a stream editor</title>
  17.  
  18. <meta name="description" content="sed, a stream editor">
  19. <meta name="keywords" content="sed, a stream editor">
  20. <meta name="resource-type" content="document">
  21. <meta name="distribution" content="global">
  22. <meta name="Generator" content="makeinfo">
  23. <link href="#Top" rel="start" title="Top">
  24. <link href="#Concept-Index" rel="index" title="Concept Index">
  25. <link href="#SEC_Contents" rel="contents" title="Table of Contents">
  26. <link href="/manual" rel="up" title="(dir)">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.indentedblock {margin-right: 0em}
  31. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  32. blockquote.smallquotation {font-size: smaller}
  33. div.display {margin-left: 3.2em}
  34. div.example {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nolinebreak {white-space: nowrap}
  49. span.roman {font-family: initial; font-weight: normal}
  50. span.sansserif {font-family: sans-serif; font-weight: normal}
  51. ul.no-bullet {list-style: none}
  52. -->
  53. <link rel="stylesheet" type="text/css" href="/software/gnulib/manual.css">
  54.  
  55.  
  56. </head>
  57.  
  58. <body lang="en">
  59. <h1 class="settitle" align="center">sed, a stream editor</h1>
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. <a name="SEC_Contents"></a>
  78. <h2 class="contents-heading">Table of Contents</h2>
  79.  
  80. <div class="contents">
  81.  
  82. <ul class="no-bullet">
  83.   <li><a name="toc-Introduction-1" href="#Introduction">1 Introduction</a></li>
  84.   <li><a name="toc-Running-sed" href="#Invoking-sed">2 Running sed</a>
  85.   <ul class="no-bullet">
  86.     <li><a name="toc-Overview-1" href="#Overview">2.1 Overview</a></li>
  87.     <li><a name="toc-Command_002dLine-Options-1" href="#Command_002dLine-Options">2.2 Command-Line Options</a></li>
  88.     <li><a name="toc-Exit-status-1" href="#Exit-status">2.3 Exit status</a></li>
  89.   </ul></li>
  90.   <li><a name="toc-sed-scripts-1" href="#sed-scripts">3 <code>sed</code> scripts</a>
  91.   <ul class="no-bullet">
  92.     <li><a name="toc-sed-script-overview-1" href="#sed-script-overview">3.1 <code>sed</code> script overview</a></li>
  93.     <li><a name="toc-sed-commands-summary" href="#sed-commands-list">3.2 <code>sed</code> commands summary</a></li>
  94.     <li><a name="toc-The-s-Command" href="#The-_0022s_0022-Command">3.3 The <code>s</code> Command</a></li>
  95.     <li><a name="toc-Often_002dUsed-Commands" href="#Common-Commands">3.4 Often-Used Commands</a></li>
  96.     <li><a name="toc-Less-Frequently_002dUsed-Commands" href="#Other-Commands">3.5 Less Frequently-Used Commands</a></li>
  97.     <li><a name="toc-Commands-for-sed-gurus" href="#Programming-Commands">3.6 Commands for <code>sed</code> gurus</a></li>
  98.     <li><a name="toc-Commands-Specific-to-GNU-sed" href="#Extended-Commands">3.7 Commands Specific to GNU <code>sed</code></a></li>
  99.     <li><a name="toc-Multiple-commands-syntax-1" href="#Multiple-commands-syntax">3.8 Multiple commands syntax</a>
  100.     <ul class="no-bullet">
  101.       <li><a name="toc-Commands-Requiring-a-newline" href="#Commands-Requiring-a-newline">3.8.1 Commands Requiring a newline</a></li>
  102.     </ul></li>
  103.   </ul></li>
  104.   <li><a name="toc-Addresses_003a-selecting-lines" href="#sed-addresses">4 Addresses: selecting lines</a>
  105.   <ul class="no-bullet">
  106.     <li><a name="toc-Addresses-overview-1" href="#Addresses-overview">4.1 Addresses overview</a></li>
  107.     <li><a name="toc-Selecting-lines-by-numbers" href="#Numeric-Addresses">4.2 Selecting lines by numbers</a></li>
  108.     <li><a name="toc-selecting-lines-by-text-matching" href="#Regexp-Addresses">4.3 selecting lines by text matching</a></li>
  109.     <li><a name="toc-Range-Addresses-1" href="#Range-Addresses">4.4 Range Addresses</a></li>
  110.   </ul></li>
  111.   <li><a name="toc-Regular-Expressions_003a-selecting-text" href="#sed-regular-expressions">5 Regular Expressions: selecting text</a>
  112.   <ul class="no-bullet">
  113.     <li><a name="toc-Overview-of-regular-expression-in-sed" href="#Regular-Expressions-Overview">5.1 Overview of regular expression in <code>sed</code></a></li>
  114.     <li><a name="toc-Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression" href="#BRE-vs-ERE">5.2 Basic (BRE) and extended (ERE) regular expression</a></li>
  115.     <li><a name="toc-Overview-of-basic-regular-expression-syntax" href="#BRE-syntax">5.3 Overview of basic regular expression syntax</a></li>
  116.     <li><a name="toc-Overview-of-extended-regular-expression-syntax" href="#ERE-syntax">5.4 Overview of extended regular expression syntax</a></li>
  117.     <li><a name="toc-Character-Classes-and-Bracket-Expressions-1" href="#Character-Classes-and-Bracket-Expressions">5.5 Character Classes and Bracket Expressions</a></li>
  118.     <li><a name="toc-regular-expression-extensions" href="#regexp-extensions">5.6 regular expression extensions</a></li>
  119.     <li><a name="toc-Back_002dreferences-and-Subexpressions-1" href="#Back_002dreferences-and-Subexpressions">5.7 Back-references and Subexpressions</a></li>
  120.     <li><a name="toc-Escape-Sequences-_002d-specifying-special-characters" href="#Escapes">5.8 Escape Sequences - specifying special characters</a>
  121.     <ul class="no-bullet">
  122.       <li><a name="toc-Escaping-Precedence" href="#Escaping-Precedence">5.8.1 Escaping Precedence</a></li>
  123.     </ul></li>
  124.     <li><a name="toc-Multibyte-characters-and-Locale-Considerations" href="#Locale-Considerations">5.9 Multibyte characters and Locale Considerations</a>
  125.     <ul class="no-bullet">
  126.       <li><a name="toc-Invalid-multibyte-characters" href="#Invalid-multibyte-characters">5.9.1 Invalid multibyte characters</a></li>
  127.       <li><a name="toc-Upper_002fLower-case-conversion" href="#Upper_002fLower-case-conversion">5.9.2 Upper/Lower case conversion</a></li>
  128.       <li><a name="toc-Multibyte-regexp-character-classes" href="#Multibyte-regexp-character-classes">5.9.3 Multibyte regexp character classes</a></li>
  129.     </ul></li>
  130.   </ul></li>
  131.   <li><a name="toc-Advanced-sed_003a-cycles-and-buffers" href="#advanced-sed">6 Advanced <code>sed</code>: cycles and buffers</a>
  132.   <ul class="no-bullet">
  133.     <li><a name="toc-How-sed-Works" href="#Execution-Cycle">6.1 How <code>sed</code> Works</a></li>
  134.     <li><a name="toc-Hold-and-Pattern-Buffers-1" href="#Hold-and-Pattern-Buffers">6.2 Hold and Pattern Buffers</a></li>
  135.     <li><a name="toc-Multiline-techniques-_002d-using-D_002cG_002cH_002cN_002cP-to-process-multiple-lines" href="#Multiline-techniques">6.3 Multiline techniques - using D,G,H,N,P to process multiple lines</a></li>
  136.     <li><a name="toc-Branching-and-Flow-Control" href="#Branching-and-flow-control">6.4 Branching and Flow Control</a>
  137.     <ul class="no-bullet">
  138.       <li><a name="toc-Branching-and-Cycles" href="#Branching-and-Cycles">6.4.1 Branching and Cycles</a></li>
  139.       <li><a name="toc-Branching-example_003a-joining-lines" href="#Branching-example_003a-joining-lines">6.4.2 Branching example: joining lines</a></li>
  140.     </ul></li>
  141.   </ul></li>
  142.   <li><a name="toc-Some-Sample-Scripts" href="#Examples">7 Some Sample Scripts</a>
  143.   <ul class="no-bullet">
  144.     <li><a name="toc-Joining-lines-1" href="#Joining-lines">7.1 Joining lines</a></li>
  145.     <li><a name="toc-Centering-Lines" href="#Centering-lines">7.2 Centering Lines</a></li>
  146.     <li><a name="toc-Increment-a-Number" href="#Increment-a-number">7.3 Increment a Number</a></li>
  147.     <li><a name="toc-Rename-Files-to-Lower-Case" href="#Rename-files-to-lower-case">7.4 Rename Files to Lower Case</a></li>
  148.     <li><a name="toc-Print-bash-Environment" href="#Print-bash-environment">7.5 Print <code>bash</code> Environment</a></li>
  149.     <li><a name="toc-Reverse-Characters-of-Lines" href="#Reverse-chars-of-lines">7.6 Reverse Characters of Lines</a></li>
  150.     <li><a name="toc-Text-search-across-multiple-lines-1" href="#Text-search-across-multiple-lines">7.7 Text search across multiple lines</a></li>
  151.     <li><a name="toc-Line-length-adjustment-1" href="#Line-length-adjustment">7.8 Line length adjustment</a></li>
  152.     <li><a name="toc-Reverse-Lines-of-Files" href="#tac">7.9 Reverse Lines of Files</a></li>
  153.     <li><a name="toc-Numbering-Lines" href="#cat-_002dn">7.10 Numbering Lines</a></li>
  154.     <li><a name="toc-Numbering-Non_002dblank-Lines" href="#cat-_002db">7.11 Numbering Non-blank Lines</a></li>
  155.     <li><a name="toc-Counting-Characters" href="#wc-_002dc">7.12 Counting Characters</a></li>
  156.     <li><a name="toc-Counting-Words" href="#wc-_002dw">7.13 Counting Words</a></li>
  157.     <li><a name="toc-Counting-Lines" href="#wc-_002dl">7.14 Counting Lines</a></li>
  158.     <li><a name="toc-Printing-the-First-Lines" href="#head">7.15 Printing the First Lines</a></li>
  159.     <li><a name="toc-Printing-the-Last-Lines" href="#tail">7.16 Printing the Last Lines</a></li>
  160.     <li><a name="toc-Make-Duplicate-Lines-Unique" href="#uniq">7.17 Make Duplicate Lines Unique</a></li>
  161.     <li><a name="toc-Print-Duplicated-Lines-of-Input" href="#uniq-_002dd">7.18 Print Duplicated Lines of Input</a></li>
  162.     <li><a name="toc-Remove-All-Duplicated-Lines" href="#uniq-_002du">7.19 Remove All Duplicated Lines</a></li>
  163.     <li><a name="toc-Squeezing-Blank-Lines" href="#cat-_002ds">7.20 Squeezing Blank Lines</a></li>
  164.   </ul></li>
  165.   <li><a name="toc-GNU-sed_0027s-Limitations-and-Non_002dlimitations" href="#Limitations">8 GNU <code>sed</code>&rsquo;s Limitations and Non-limitations</a></li>
  166.   <li><a name="toc-Other-Resources-for-Learning-About-sed" href="#Other-Resources">9 Other Resources for Learning About <code>sed</code></a></li>
  167.   <li><a name="toc-Reporting-Bugs-1" href="#Reporting-Bugs">10 Reporting Bugs</a></li>
  168.   <li><a name="toc-GNU-Free-Documentation-License-1" href="#GNU-Free-Documentation-License">Appendix A GNU Free Documentation License</a></li>
  169.   <li><a name="toc-Concept-Index-1" href="#Concept-Index">Concept Index</a></li>
  170.   <li><a name="toc-Command-and-Option-Index-1" href="#Command-and-Option-Index">Command and Option Index</a></li>
  171. </ul>
  172. </div>
  173.  
  174.  
  175. <a name="Top"></a>
  176. <div class="header">
  177. <p>
  178. Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a>, Up: <a href="/manual" accesskey="u" rel="up">(dir)</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  179. </div>
  180. <a name="GNU-sed"></a>
  181. <h1 class="top">GNU <code>sed</code></h1>
  182.  
  183. <p>This file documents version 4.8 of
  184. GNU <code>sed</code>, a stream editor.
  185. </p>
  186. <p>Copyright &copy; 1998&ndash;2020 Free Software Foundation, Inc.
  187. </p>
  188. <p>Permission is granted to copy, distribute and/or modify this document
  189. under the terms of the GNU Free Documentation License, Version 1.3
  190. or any later version published by the Free Software Foundation;
  191. with no Invariant Sections, no Front-Cover Texts, and no
  192. Back-Cover Texts.  A copy of the license is included in the
  193. section entitled &ldquo;GNU Free Documentation License&rdquo;.
  194.  
  195.  
  196. <table class="menu" border="0" cellspacing="0">
  197. <tr><td align="left" valign="top">&bull; <a href="#Introduction" accesskey="1">Introduction</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Introduction
  198. </td></tr>
  199. <tr><td align="left" valign="top">&bull; <a href="#Invoking-sed" accesskey="2">Invoking sed</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Invocation
  200. </td></tr>
  201. <tr><td align="left" valign="top">&bull; <a href="#sed-scripts" accesskey="3">sed scripts</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top"><code>sed</code> scripts
  202. </td></tr>
  203. <tr><td align="left" valign="top">&bull; <a href="#sed-addresses" accesskey="4">sed addresses</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Addresses: selecting lines
  204. </td></tr>
  205. <tr><td align="left" valign="top">&bull; <a href="#sed-regular-expressions" accesskey="5">sed regular expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Regular expressions: selecting text
  206. </td></tr>
  207. <tr><td align="left" valign="top">&bull; <a href="#advanced-sed" accesskey="6">advanced sed</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Advanced <code>sed</code>: cycles and buffers
  208. </td></tr>
  209. <tr><td align="left" valign="top">&bull; <a href="#Examples" accesskey="7">Examples</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Some sample scripts
  210. </td></tr>
  211. <tr><td align="left" valign="top">&bull; <a href="#Limitations" accesskey="8">Limitations</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Limitations and (non-)limitations of GNU <code>sed</code>
  212. </td></tr>
  213. <tr><td align="left" valign="top">&bull; <a href="#Other-Resources" accesskey="9">Other Resources</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Other resources for learning about <code>sed</code>
  214. </td></tr>
  215. <tr><td align="left" valign="top">&bull; <a href="#Reporting-Bugs">Reporting Bugs</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Reporting bugs
  216. </td></tr>
  217. <tr><td align="left" valign="top">&bull; <a href="#GNU-Free-Documentation-License">GNU Free Documentation License</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Copying and sharing this manual
  218. </td></tr>
  219. <tr><td align="left" valign="top">&bull; <a href="#Concept-Index">Concept Index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">A menu with all the topics in this manual.
  220. </td></tr>
  221. <tr><td align="left" valign="top">&bull; <a href="#Command-and-Option-Index">Command and Option Index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">A menu with all <code>sed</code> commands and
  222.                                command-line options.
  223. </td></tr>
  224.  
  225.  
  226. <hr>
  227. <a name="Introduction"></a>
  228. <div class="header">
  229. <p>
  230. Next: <a href="#Invoking-sed" accesskey="n" rel="next">Invoking sed</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  231. </div>
  232. <a name="Introduction-1"></a>
  233. <h2 class="chapter">1 Introduction</h2>
  234.  
  235. <a name="index-Stream-editor"></a>
  236. <p><code>sed</code> is a stream editor.
  237. A stream editor is used to perform basic text
  238. transformations on an input stream
  239. (a file or input from a pipeline).
  240. While in some ways similar to an editor which
  241. permits scripted edits (such as <code>ed</code>),
  242. <code>sed</code> works by making only one pass over the
  243. input(s), and is consequently more efficient.
  244. But it is <code>sed</code>&rsquo;s ability to filter text in a pipeline
  245. which particularly distinguishes it from other types of
  246. editors.
  247. </p>
  248.  
  249. <hr>
  250. <a name="Invoking-sed"></a>
  251. <div class="header">
  252. <p>
  253. Next: <a href="#sed-scripts" accesskey="n" rel="next">sed scripts</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  254. </div>
  255. <a name="Running-sed"></a>
  256. <h2 class="chapter">2 Running sed</h2>
  257.  
  258. <p>This chapter covers how to run <code>sed</code>. Details of <code>sed</code>
  259. scripts and individual <code>sed</code> commands are discussed in the
  260. next chapter.
  261. </p>
  262. <table class="menu" border="0" cellspacing="0">
  263. <tr><td align="left" valign="top">&bull; <a href="#Overview" accesskey="1">Overview</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  264. </td></tr>
  265. <tr><td align="left" valign="top">&bull; <a href="#Command_002dLine-Options" accesskey="2">Command-Line Options</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  266. </td></tr>
  267. <tr><td align="left" valign="top">&bull; <a href="#Exit-status" accesskey="3">Exit status</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  268. </td></tr>
  269.  
  270.  
  271. <hr>
  272. <a name="Overview"></a>
  273. <div class="header">
  274. <p>
  275. Next: <a href="#Command_002dLine-Options" accesskey="n" rel="next">Command-Line Options</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  276. </div>
  277. <a name="Overview-1"></a>
  278. <h3 class="section">2.1 Overview</h3>
  279. <p>Normally <code>sed</code> is invoked like this:
  280. </p>
  281. <div class="example">
  282. <pre class="example">sed SCRIPT INPUTFILE...
  283. </pre></div>
  284.  
  285. <p>For example, to replace all occurrences of &lsquo;<samp>hello</samp>&rsquo; to &lsquo;<samp>world</samp>&rsquo;
  286. in the file <samp>input.txt</samp>:
  287. </p>
  288. <div class="example">
  289. <pre class="example">sed 's/hello/world/' input.txt &gt; output.txt
  290. </pre></div>
  291.  
  292. <a name="index-stdin"></a>
  293. <a name="index-standard-input"></a>
  294. <p>If you do not specify <var>INPUTFILE</var>, or if <var>INPUTFILE</var> is <samp>-</samp>,
  295. <code>sed</code> filters the contents of the standard input. The following
  296. commands are equivalent:
  297. </p>
  298. <div class="example">
  299. <pre class="example">sed 's/hello/world/' input.txt &gt; output.txt
  300. sed 's/hello/world/' &lt; input.txt &gt; output.txt
  301. cat input.txt | sed 's/hello/world/' - &gt; output.txt
  302. </pre></div>
  303.  
  304. <a name="index-stdout"></a>
  305. <a name="index-output"></a>
  306. <a name="index-standard-output"></a>
  307. <a name="index-_002di_002c-example"></a>
  308. <p><code>sed</code> writes output to standard output. Use <samp>-i</samp> to edit
  309. files in-place instead of printing to standard output.
  310. See also the <code>W</code> and <code>s///w</code> commands for writing output to
  311. other files. The following command modifies <samp>file.txt</samp> and
  312. does not produce any output:
  313. </p>
  314. <div class="example">
  315. <pre class="example">sed -i 's/hello/world/' file.txt
  316. </pre></div>
  317.  
  318. <a name="index-_002dn_002c-example"></a>
  319. <a name="index-p_002c-example"></a>
  320. <a name="index-suppressing-output"></a>
  321. <a name="index-output_002c-suppressing"></a>
  322. <p>By default <code>sed</code> prints all processed input (except input
  323. that has been modified/deleted by commands such as <code>d</code>).
  324. Use <samp>-n</samp> to suppress output, and the <code>p</code> command
  325. to print specific lines. The following command prints only line 45
  326. of the input file:
  327. </p>
  328. <div class="example">
  329. <pre class="example">sed -n '45p' file.txt
  330. </pre></div>
  331.  
  332.  
  333.  
  334. <a name="index-multiple-files"></a>
  335. <a name="index-_002ds_002c-example"></a>
  336. <p><code>sed</code> treats multiple input files as one long stream.
  337. The following example prints the first line of the first file
  338. (<samp>one.txt</samp>) and the last line of the last file (<samp>three.txt</samp>).
  339. Use <samp>-s</samp> to reverse this behavior.
  340. </p>
  341. <div class="example">
  342. <pre class="example">sed -n  '1p ; $p' one.txt two.txt three.txt
  343. </pre></div>
  344.  
  345.  
  346. <a name="index-_002de_002c-example"></a>
  347. <a name="index-_002d_002dexpression_002c-example"></a>
  348. <a name="index-_002df_002c-example"></a>
  349. <a name="index-_002d_002dfile_002c-example"></a>
  350. <a name="index-script-parameter"></a>
  351. <a name="index-parameters_002c-script"></a>
  352. <p>Without <samp>-e</samp> or <samp>-f</samp> options, <code>sed</code> uses
  353. the first non-option parameter as the <var>script</var>, and the following
  354. non-option parameters as input files.
  355. If <samp>-e</samp> or <samp>-f</samp> options are used to specify a <var>script</var>,
  356. all non-option parameters are taken as input files.
  357. Options <samp>-e</samp> and <samp>-f</samp> can be combined, and can appear
  358. multiple times (in which case the final effective <var>script</var> will be
  359. concatenation of all the individual <var>script</var>s).
  360. </p>
  361. <p>The following examples are equivalent:
  362. </p>
  363. <div class="example">
  364. <pre class="example">sed 's/hello/world/' input.txt &gt; output.txt
  365.  
  366. sed -e 's/hello/world/' input.txt &gt; output.txt
  367. sed --expression='s/hello/world/' input.txt &gt; output.txt
  368.  
  369. echo 's/hello/world/' &gt; myscript.sed
  370. sed -f myscript.sed input.txt &gt; output.txt
  371. sed --file=myscript.sed input.txt &gt; output.txt
  372. </pre></div>
  373.  
  374.  
  375. <hr>
  376. <a name="Command_002dLine-Options"></a>
  377. <div class="header">
  378. <p>
  379. Next: <a href="#Exit-status" accesskey="n" rel="next">Exit status</a>, Previous: <a href="#Overview" accesskey="p" rel="prev">Overview</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  380. </div>
  381. <a name="Command_002dLine-Options-1"></a>
  382. <h3 class="section">2.2 Command-Line Options</h3>
  383.  
  384. <p>The full format for invoking <code>sed</code> is:
  385. </p>
  386. <div class="example">
  387. <pre class="example">sed OPTIONS... [SCRIPT] [INPUTFILE...]
  388. </pre></div>
  389.  
  390. <p><code>sed</code> may be invoked with the following command-line options:
  391. </p>
  392. <dl compact="compact">
  393. <dt><code>--version</code></dt>
  394. <dd><a name="index-_002d_002dversion"></a>
  395. <a name="index-Version_002c-printing"></a>
  396. <p>Print out the version of <code>sed</code> that is being run and a copyright notice,
  397. then exit.
  398. </p>
  399. </dd>
  400. <dt><code>--help</code></dt>
  401. <dd><a name="index-_002d_002dhelp"></a>
  402. <a name="index-Usage-summary_002c-printing"></a>
  403. <p>Print a usage message briefly summarizing these command-line options
  404. and the bug-reporting address,
  405. then exit.
  406. </p>
  407. </dd>
  408. <dt><code>-n</code></dt>
  409. <dt><code>--quiet</code></dt>
  410. <dt><code>--silent</code></dt>
  411. <dd><a name="index-_002dn"></a>
  412. <a name="index-_002d_002dquiet"></a>
  413. <a name="index-_002d_002dsilent"></a>
  414. <a name="index-Disabling-autoprint_002c-from-command-line"></a>
  415. <p>By default, <code>sed</code> prints out the pattern space
  416. at the end of each cycle through the script (see <a href="#Execution-Cycle">How <code>sed</code> works</a>).
  417. These options disable this automatic printing,
  418. and <code>sed</code> only produces output when explicitly told to
  419. via the <code>p</code> command.
  420. </p>
  421. </dd>
  422. <dt><code>--debug</code></dt>
  423. <dd><a name="index-_002d_002ddebug"></a>
  424. <a name="index-GNU-extensions_002c-debug"></a>
  425. <p>Print the input sed program in canonical form,
  426. and annotate program execution.
  427. </p><div class="example">
  428. <pre class="example">$ echo 1 | sed '\%1%s21232'
  429. 3
  430.  
  431. $ echo 1 | sed --debug '\%1%s21232'
  432. SED PROGRAM:
  433.   /1/ s/1/3/
  434. INPUT:   'STDIN' line 1
  435. PATTERN: 1
  436. COMMAND: /1/ s/1/3/
  437. PATTERN: 3
  438. END-OF-CYCLE:
  439. 3
  440. </pre></div>
  441.  
  442.  
  443. </dd>
  444. <dt><code>-e <var>script</var></code></dt>
  445. <dt><code>--expression=<var>script</var></code></dt>
  446. <dd><a name="index-_002de"></a>
  447. <a name="index-_002d_002dexpression"></a>
  448. <a name="index-Script_002c-from-command-line"></a>
  449. <p>Add the commands in <var>script</var> to the set of commands to be
  450. run while processing the input.
  451. </p>
  452. </dd>
  453. <dt><code>-f <var>script-file</var></code></dt>
  454. <dt><code>--file=<var>script-file</var></code></dt>
  455. <dd><a name="index-_002df"></a>
  456. <a name="index-_002d_002dfile"></a>
  457. <a name="index-Script_002c-from-a-file"></a>
  458. <p>Add the commands contained in the file <var>script-file</var>
  459. to the set of commands to be run while processing the input.
  460. </p>
  461. </dd>
  462. <dt><code>-i[<var>SUFFIX</var>]</code></dt>
  463. <dt><code>--in-place[=<var>SUFFIX</var>]</code></dt>
  464. <dd><a name="index-_002di"></a>
  465. <a name="index-_002d_002din_002dplace"></a>
  466. <a name="index-In_002dplace-editing_002c-activating"></a>
  467. <a name="index-GNU-extensions_002c-in_002dplace-editing"></a>
  468. <p>This option specifies that files are to be edited in-place.
  469. GNU <code>sed</code> does this by creating a temporary file and
  470. sending output to this file rather than to the standard
  471. output.<a name="DOCF1" href="#FOOT1"><sup>1</sup></a>.
  472. </p>
  473. <p>This option implies <samp>-s</samp>.
  474. </p>
  475. <p>When the end of the file is reached, the temporary file is
  476. renamed to the output file&rsquo;s original name.  The extension,
  477. if supplied, is used to modify the name of the old file
  478. before renaming the temporary file, thereby making a backup
  479. copy<a name="DOCF2" href="#FOOT2"><sup>2</sup></a>).
  480. </p>
  481. <a name="index-In_002dplace-editing_002c-Perl_002dstyle-backup-file-names"></a>
  482. <p>This rule is followed: if the extension doesn&rsquo;t contain a <code>*</code>,
  483. then it is appended to the end of the current filename as a
  484. suffix; if the extension does contain one or more <code>*</code>
  485. characters, then <em>each</em> asterisk is replaced with the
  486. current filename.  This allows you to add a prefix to the
  487. backup file, instead of (or in addition to) a suffix, or
  488. even to place backup copies of the original files into another
  489. directory (provided the directory already exists).
  490. </p>
  491. <p>If no extension is supplied, the original file is
  492. overwritten without making a backup.
  493. </p>
  494. <p>Because <samp>-i</samp> takes an optional argument, it should
  495. not be followed by other short options:
  496. </p><dl compact="compact">
  497. <dt><code>sed -Ei '...' FILE</code></dt>
  498. <dd><p>Same as <samp>-E -i</samp> with no backup suffix - <samp>FILE</samp> will be
  499. edited in-place without creating a backup.
  500. </p>
  501. </dd>
  502. <dt><code>sed -iE '...' FILE</code></dt>
  503. <dd><p>This is equivalent to <samp>--in-place=E</samp>, creating <samp>FILEE</samp> as backup
  504. of <samp>FILE</samp>
  505. </p></dd>
  506. </dl>
  507.  
  508. <p>Be cautious of using <samp>-n</samp> with <samp>-i</samp>: the former disables
  509. automatic printing of lines and the latter changes the file in-place
  510. without a backup. Used carelessly (and without an explicit <code>p</code> command),
  511. the output file will be empty:
  512. </p><div class="example">
  513. <pre class="example"># WRONG USAGE: 'FILE' will be truncated.
  514. sed -ni 's/foo/bar/' FILE
  515. </pre></div>
  516.  
  517. </dd>
  518. <dt><code>-l <var>N</var></code></dt>
  519. <dt><code>--line-length=<var>N</var></code></dt>
  520. <dd><a name="index-_002dl"></a>
  521. <a name="index-_002d_002dline_002dlength"></a>
  522. <a name="index-Line-length_002c-setting"></a>
  523. <p>Specify the default line-wrap length for the <code>l</code> command.
  524. A length of 0 (zero) means to never wrap long lines.  If
  525. not specified, it is taken to be 70.
  526. </p>
  527. </dd>
  528. <dt><code>--posix</code></dt>
  529. <dd><a name="index-_002d_002dposix"></a>
  530. <a name="index-GNU-extensions_002c-disabling"></a>
  531. <p>GNU <code>sed</code> includes several extensions to POSIX
  532. sed.  In order to simplify writing portable scripts, this
  533. option disables all the extensions that this manual documents,
  534. including additional commands.
  535. <a name="index-POSIXLY_005fCORRECT-behavior_002c-enabling"></a>
  536. Most of the extensions accept <code>sed</code> programs that
  537. are outside the syntax mandated by POSIX, but some
  538. of them (such as the behavior of the <code>N</code> command
  539. described in <a href="#Reporting-Bugs">Reporting Bugs</a>) actually violate the
  540. standard.  If you want to disable only the latter kind of
  541. extension, you can set the <code>POSIXLY_CORRECT</code> variable
  542. to a non-empty value.
  543. </p>
  544. </dd>
  545. <dt><code>-b</code></dt>
  546. <dt><code>--binary</code></dt>
  547. <dd><a name="index-_002db"></a>
  548. <a name="index-_002d_002dbinary"></a>
  549. <p>This option is available on every platform, but is only effective where the
  550. operating system makes a distinction between text files and binary files.
  551. When such a distinction is made&mdash;as is the case for MS-DOS, Windows,
  552. Cygwin&mdash;text files are composed of lines separated by a carriage return
  553. <em>and</em> a line feed character, and <code>sed</code> does not see the
  554. ending CR.  When this option is specified, <code>sed</code> will open
  555. input files in binary mode, thus not requesting this special processing
  556. and considering lines to end at a line feed.
  557. </p>
  558. </dd>
  559. <dt><code>--follow-symlinks</code></dt>
  560. <dd><a name="index-_002d_002dfollow_002dsymlinks"></a>
  561. <p>This option is available only on platforms that support
  562. symbolic links and has an effect only if option <samp>-i</samp>
  563. is specified.  In this case, if the file that is specified
  564. on the command line is a symbolic link, <code>sed</code> will
  565. follow the link and edit the ultimate destination of the
  566. link.  The default behavior is to break the symbolic link,
  567. so that the link destination will not be modified.
  568. </p>
  569. </dd>
  570. <dt><code>-E</code></dt>
  571. <dt><code>-r</code></dt>
  572. <dt><code>--regexp-extended</code></dt>
  573. <dd><a name="index-_002dE"></a>
  574. <a name="index-_002dr"></a>
  575. <a name="index-_002d_002dregexp_002dextended"></a>
  576. <a name="index-Extended-regular-expressions_002c-choosing"></a>
  577. <a name="index-GNU-extensions_002c-extended-regular-expressions"></a>
  578. <p>Use extended regular expressions rather than basic
  579. regular expressions.  Extended regexps are those that
  580. <code>egrep</code> accepts; they can be clearer because they
  581. usually have fewer backslashes.
  582. Historically this was a GNU extension,
  583. but the <samp>-E</samp>
  584. extension has since been added to the POSIX standard
  585. (http://austingroupbugs.net/view.php?id=528),
  586. so use <samp>-E</samp> for portability.
  587. GNU sed has accepted <samp>-E</samp> as an undocumented option for years,
  588. and *BSD seds have accepted <samp>-E</samp> for years as well,
  589. but scripts that use <samp>-E</samp> might not port to other older systems.
  590. See <a href="#ERE-syntax">Extended regular expressions</a>.
  591. </p>
  592.  
  593. </dd>
  594. <dt><code>-s</code></dt>
  595. <dt><code>--separate</code></dt>
  596. <dd><a name="index-_002ds"></a>
  597. <a name="index-_002d_002dseparate"></a>
  598. <a name="index-Working-on-separate-files"></a>
  599. <p>By default, <code>sed</code> will consider the files specified on the
  600. command line as a single continuous long stream.  This GNU <code>sed</code>
  601. extension allows the user to consider them as separate files:
  602. range addresses (such as &lsquo;<samp>/abc/,/def/</samp>&rsquo;) are not allowed
  603. to span several files, line numbers are relative to the start
  604. of each file, <code>$</code> refers to the last line of each file,
  605. and files invoked from the <code>R</code> commands are rewound at the
  606. start of each file.
  607. </p>
  608. </dd>
  609. <dt><code>--sandbox</code></dt>
  610. <dd><a name="index-_002d_002dsandbox"></a>
  611. <a name="index-Sandbox-mode"></a>
  612. <p>In sandbox mode,  <code>e/w/r</code> commands are rejected - programs containing
  613. them will be aborted without being run. Sandbox mode ensures <code>sed</code>
  614. operates only on the input files designated on the command line, and
  615. cannot run external programs.
  616. </p>
  617.  
  618. </dd>
  619. <dt><code>-u</code></dt>
  620. <dt><code>--unbuffered</code></dt>
  621. <dd><a name="index-_002du"></a>
  622. <a name="index-_002d_002dunbuffered"></a>
  623. <a name="index-Unbuffered-I_002fO_002c-choosing"></a>
  624. <p>Buffer both input and output as minimally as practical.
  625. (This is particularly useful if the input is coming from
  626. the likes of &lsquo;<samp>tail -f</samp>&rsquo;, and you wish to see the transformed
  627. output as soon as possible.)
  628. </p>
  629. </dd>
  630. <dt><code>-z</code></dt>
  631. <dt><code>--null-data</code></dt>
  632. <dt><code>--zero-terminated</code></dt>
  633. <dd><a name="index-_002dz"></a>
  634. <a name="index-_002d_002dnull_002ddata"></a>
  635. <a name="index-_002d_002dzero_002dterminated"></a>
  636. <p>Treat the input as a set of lines, each terminated by a zero byte
  637. (the ASCII &lsquo;<samp>NUL</samp>&rsquo; character) instead of a newline.  This option can
  638. be used with commands like &lsquo;<samp>sort -z</samp>&rsquo; and &lsquo;<samp>find -print0</samp>&rsquo;
  639. to process arbitrary file names.
  640. </p></dd>
  641. </dl>
  642.  
  643. <p>If no <samp>-e</samp>, <samp>-f</samp>, <samp>--expression</samp>, or <samp>--file</samp>
  644. options are given on the command-line,
  645. then the first non-option argument on the command line is
  646. taken to be the <var>script</var> to be executed.
  647. </p>
  648. <a name="index-Files-to-be-processed-as-input"></a>
  649. <p>If any command-line parameters remain after processing the above,
  650. these parameters are interpreted as the names of input files to
  651. be processed.
  652. <a name="index-Standard-input_002c-processing-as-input"></a>
  653. A file name of &lsquo;<samp>-</samp>&rsquo; refers to the standard input stream.
  654. The standard input will be processed if no file names are specified.
  655. </p>
  656. <hr>
  657. <a name="Exit-status"></a>
  658. <div class="header">
  659. <p>
  660. Previous: <a href="#Command_002dLine-Options" accesskey="p" rel="prev">Command-Line Options</a>, Up: <a href="#Invoking-sed" accesskey="u" rel="up">Invoking sed</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  661. </div>
  662. <a name="Exit-status-1"></a>
  663. <h3 class="section">2.3 Exit status</h3>
  664. <a name="index-exit-status"></a>
  665. <p>An exit status of zero indicates success, and a nonzero value
  666. indicates failure. GNU <code>sed</code> returns the following exit status
  667. error values:
  668. </p>
  669. <dl compact="compact">
  670. <dt>0</dt>
  671. <dd><p>Successful completion.
  672. </p>
  673. </dd>
  674. <dt>1</dt>
  675. <dd><p>Invalid command, invalid syntax, invalid regular expression or a
  676. GNU <code>sed</code> extension command used with <samp>--posix</samp>.
  677. </p>
  678. </dd>
  679. <dt>2</dt>
  680. <dd><p>One or more of the input file specified on the command line could not be
  681. opened (e.g. if a file is not found, or read permission is denied).
  682. Processing continued with other files.
  683. </p>
  684. </dd>
  685. <dt>4</dt>
  686. <dd><p>An I/O error, or a serious processing error during runtime,
  687. GNU <code>sed</code> aborted immediately.
  688. </p></dd>
  689. </dl>
  690.  
  691. <a name="index-Q_002c-example"></a>
  692. <a name="index-exit-status_002c-example"></a>
  693. <p>Additionally, the commands <code>q</code> and <code>Q</code> can be used to terminate
  694. <code>sed</code> with a custom exit code value (this is a GNU <code>sed</code> extension):
  695. </p>
  696. <div class="example">
  697. <pre class="example">$ echo | sed 'Q42' ; echo $?
  698. 42
  699. </pre></div>
  700.  
  701.  
  702. <hr>
  703. <a name="sed-scripts"></a>
  704. <div class="header">
  705. <p>
  706. Next: <a href="#sed-addresses" accesskey="n" rel="next">sed addresses</a>, Previous: <a href="#Invoking-sed" accesskey="p" rel="prev">Invoking sed</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  707. </div>
  708. <a name="sed-scripts-1"></a>
  709. <h2 class="chapter">3 <code>sed</code> scripts</h2>
  710.  
  711.  
  712. <table class="menu" border="0" cellspacing="0">
  713. <tr><td align="left" valign="top">&bull; <a href="#sed-script-overview" accesskey="1">sed script overview</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top"><code>sed</code> script overview
  714. </td></tr>
  715. <tr><td align="left" valign="top">&bull; <a href="#sed-commands-list" accesskey="2">sed commands list</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top"><code>sed</code> commands summary
  716. </td></tr>
  717. <tr><td align="left" valign="top">&bull; <a href="#The-_0022s_0022-Command" accesskey="3">The &quot;s&quot; Command</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top"><code>sed</code>&rsquo;s Swiss Army Knife
  718. </td></tr>
  719. <tr><td align="left" valign="top">&bull; <a href="#Common-Commands" accesskey="4">Common Commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Often used commands
  720. </td></tr>
  721. <tr><td align="left" valign="top">&bull; <a href="#Other-Commands" accesskey="5">Other Commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Less frequently used commands
  722. </td></tr>
  723. <tr><td align="left" valign="top">&bull; <a href="#Programming-Commands" accesskey="6">Programming Commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Commands for <code>sed</code> gurus
  724. </td></tr>
  725. <tr><td align="left" valign="top">&bull; <a href="#Extended-Commands" accesskey="7">Extended Commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Commands specific of GNU <code>sed</code>
  726. </td></tr>
  727. <tr><td align="left" valign="top">&bull; <a href="#Multiple-commands-syntax" accesskey="8">Multiple commands syntax</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Extension for easier scripting
  728. </td></tr>
  729.  
  730. <hr>
  731. <a name="sed-script-overview"></a>
  732. <div class="header">
  733. <p>
  734. Next: <a href="#sed-commands-list" accesskey="n" rel="next">sed commands list</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  735. </div>
  736. <a name="sed-script-overview-1"></a>
  737. <h3 class="section">3.1 <code>sed</code> script overview</h3>
  738.  
  739. <a name="index-sed-script-structure"></a>
  740. <a name="index-Script-structure"></a>
  741.  
  742. <p>A <code>sed</code> program consists of one or more <code>sed</code> commands,
  743. passed in by one or more of the
  744. <samp>-e</samp>, <samp>-f</samp>, <samp>--expression</samp>, and <samp>--file</samp>
  745. options, or the first non-option argument if zero of these
  746. options are used.
  747. This document will refer to &ldquo;the&rdquo; <code>sed</code> script;
  748. this is understood to mean the in-order concatenation
  749. of all of the <var>script</var>s and <var>script-file</var>s passed in.
  750. See <a href="#Overview">Overview</a>.
  751. </p>
  752.  
  753. <a name="index-sed-commands-syntax"></a>
  754. <a name="index-syntax_002c-sed-commands"></a>
  755. <a name="index-addresses_002c-syntax"></a>
  756. <a name="index-syntax_002c-addresses"></a>
  757. <p><code>sed</code> commands follow this syntax:
  758. </p>
  759. <div class="example">
  760. <pre class="example">[addr]<var>X</var>[options]
  761. </pre></div>
  762.  
  763. <p><var>X</var> is a single-letter <code>sed</code> command.
  764. <code>[addr]</code> is an optional line address. If <code>[addr]</code> is specified,
  765. the command <var>X</var> will be executed only on the matched lines.
  766. <code>[addr]</code> can be a single line number, a regular expression,
  767. or a range of lines (see <a href="#sed-addresses">sed addresses</a>).
  768. Additional <code>[options]</code> are used for some <code>sed</code> commands.
  769. </p>
  770. <a name="index-d_002c-example"></a>
  771. <a name="index-address-range_002c-example"></a>
  772. <a name="index-example_002c-address-range"></a>
  773. <p>The following example deletes  lines 30 to 35 in the input.
  774. <code>30,35</code> is an address range. <code>d</code> is the delete command:
  775. </p>
  776. <div class="example">
  777. <pre class="example">sed '30,35d' input.txt &gt; output.txt
  778. </pre></div>
  779.  
  780. <a name="index-q_002c-example"></a>
  781. <a name="index-regular-expression_002c-example"></a>
  782. <a name="index-example_002c-regular-expression"></a>
  783. <p>The following example prints all input until a line
  784. starting with the word &lsquo;<samp>foo</samp>&rsquo; is found. If such line is found,
  785. <code>sed</code> will terminate with exit status 42.
  786. If such line was not found (and no other error occurred), <code>sed</code>
  787. will exit with status 0.
  788. <code>/^foo/</code> is a regular-expression address.
  789. <code>q</code> is the quit command. <code>42</code> is the command option.
  790. </p>
  791. <div class="example">
  792. <pre class="example">sed '/^foo/q42' input.txt &gt; output.txt
  793. </pre></div>
  794.  
  795.  
  796. <a name="index-multiple-sed-commands"></a>
  797. <a name="index-sed-commands_002c-multiple"></a>
  798. <a name="index-newline_002c-command-separator"></a>
  799. <a name="index-semicolons_002c-command-separator"></a>
  800. <a name="index-_003b_002c-command-separator"></a>
  801. <a name="index-_002de_002c-example-1"></a>
  802. <a name="index-_002df_002c-example-1"></a>
  803. <p>Commands within a <var>script</var> or <var>script-file</var> can be
  804. separated by semicolons (<code>;</code>) or newlines (ASCII 10).
  805. Multiple scripts can be specified with <samp>-e</samp> or <samp>-f</samp>
  806. options.
  807. </p>
  808. <p>The following examples are all equivalent. They perform two <code>sed</code>
  809. operations: deleting any lines matching the regular expression <code>/^foo/</code>,
  810. and replacing all occurrences of the string &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;:
  811. </p>
  812. <div class="example">
  813. <pre class="example">sed '/^foo/d ; s/hello/world/' input.txt &gt; output.txt
  814.  
  815. sed -e '/^foo/d' -e 's/hello/world/' input.txt &gt; output.txt
  816.  
  817. echo '/^foo/d' &gt; script.sed
  818. echo 's/hello/world/' &gt;&gt; script.sed
  819. sed -f script.sed input.txt &gt; output.txt
  820.  
  821. echo 's/hello/world/' &gt; script2.sed
  822. sed -e '/^foo/d' -f script2.sed input.txt &gt; output.txt
  823. </pre></div>
  824.  
  825.  
  826. <a name="index-a_002c-and-semicolons"></a>
  827. <a name="index-c_002c-and-semicolons"></a>
  828. <a name="index-i_002c-and-semicolons"></a>
  829. <p>Commands <code>a</code>, <code>c</code>, <code>i</code>, due to their syntax,
  830. cannot be followed by semicolons working as command separators and
  831. thus should be terminated
  832. with newlines or be placed at the end of a <var>script</var> or <var>script-file</var>.
  833. Commands can also be preceded with optional non-significant
  834. whitespace characters.
  835. See <a href="#Multiple-commands-syntax">Multiple commands syntax</a>.
  836. </p>
  837.  
  838.  
  839. <hr>
  840. <a name="sed-commands-list"></a>
  841. <div class="header">
  842. <p>
  843. Next: <a href="#The-_0022s_0022-Command" accesskey="n" rel="next">The &quot;s&quot; Command</a>, Previous: <a href="#sed-script-overview" accesskey="p" rel="prev">sed script overview</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  844. </div>
  845. <a name="sed-commands-summary"></a>
  846. <h3 class="section">3.2 <code>sed</code> commands summary</h3>
  847.  
  848. <p>The following commands are supported in GNU <code>sed</code>.
  849. Some are standard POSIX commands, while other are GNU extensions.
  850. Details and examples for each command are in the following sections.
  851. (Mnemonics) are shown in parentheses.
  852. </p>
  853. <dl compact="compact">
  854. <dt><code>a\</code></dt>
  855. <dt><code><var>text</var></code></dt>
  856. <dd><p>Append <var>text</var> after a line.
  857. </p>
  858. </dd>
  859. <dt><code>a <var>text</var></code></dt>
  860. <dd><p>Append <var>text</var> after a line (alternative syntax).
  861. </p>
  862. </dd>
  863. <dt><code>b <var>label</var></code></dt>
  864. <dd><p>Branch unconditionally to <var>label</var>.
  865. The <var>label</var> may be omitted, in which case the next cycle is started.
  866. </p>
  867. </dd>
  868. <dt><code>c\</code></dt>
  869. <dt><code><var>text</var></code></dt>
  870. <dd><p>Replace (change) lines with <var>text</var>.
  871. </p>
  872. </dd>
  873. <dt><code>c <var>text</var></code></dt>
  874. <dd><p>Replace (change) lines with <var>text</var> (alternative syntax).
  875. </p>
  876. </dd>
  877. <dt><code>d</code></dt>
  878. <dd><p>Delete the pattern space;
  879. immediately start next cycle.
  880. </p>
  881. </dd>
  882. <dt><code>D</code></dt>
  883. <dd><p>If pattern space contains newlines, delete text in the pattern
  884. space up to the first newline, and restart cycle with the resultant
  885. pattern space, without reading a new line of input.
  886. </p>
  887. <p>If pattern space contains no newline, start a normal new cycle as if
  888. the <code>d</code> command was issued.
  889. </p>
  890. </dd>
  891. <dt><code>e</code></dt>
  892. <dd><p>Executes the command that is found in pattern space and
  893. replaces the pattern space with the output; a trailing newline
  894. is suppressed.
  895. </p>
  896. </dd>
  897. <dt><code>e <var>command</var></code></dt>
  898. <dd><p>Executes <var>command</var> and sends its output to the output stream.
  899. The command can run across multiple lines, all but the last ending with
  900. a back-slash.
  901. </p>
  902. </dd>
  903. <dt><code>F</code></dt>
  904. <dd><p>(filename) Print the file name of the current input file (with a trailing
  905. newline).
  906. </p>
  907. </dd>
  908. <dt><code>g</code></dt>
  909. <dd><p>Replace the contents of the pattern space with the contents of the hold space.
  910. </p>
  911. </dd>
  912. <dt><code>G</code></dt>
  913. <dd><p>Append a newline to the contents of the pattern space,
  914. and then append the contents of the hold space to that of the pattern space.
  915. </p>
  916. </dd>
  917. <dt><code>h</code></dt>
  918. <dd><p>(hold) Replace the contents of the hold space with the contents of the
  919. pattern space.
  920. </p>
  921. </dd>
  922. <dt><code>H</code></dt>
  923. <dd><p>Append a newline to the contents of the hold space,
  924. and then append the contents of the pattern space to that of the hold space.
  925. </p>
  926. </dd>
  927. <dt><code>i\</code></dt>
  928. <dt><code><var>text</var></code></dt>
  929. <dd><p>insert <var>text</var> before a line.
  930. </p>
  931. </dd>
  932. <dt><code>i <var>text</var></code></dt>
  933. <dd><p>insert <var>text</var> before a line (alternative syntax).
  934. </p>
  935. </dd>
  936. <dt><code>l</code></dt>
  937. <dd><p>Print the pattern space in an unambiguous form.
  938. </p>
  939. </dd>
  940. <dt><code>n</code></dt>
  941. <dd><p>(next) If auto-print is not disabled, print the pattern space,
  942. then, regardless, replace the pattern space with the next line of input.
  943. If there is no more input then <code>sed</code> exits without processing
  944. any more commands.
  945. </p>
  946. </dd>
  947. <dt><code>N</code></dt>
  948. <dd><p>Add a newline to the pattern space,
  949. then append the next line of input to the pattern space.
  950. If there is no more input then <code>sed</code> exits without processing
  951. any more commands.
  952. </p>
  953. </dd>
  954. <dt><code>p</code></dt>
  955. <dd><p>Print the pattern space.
  956. </p>
  957. </dd>
  958. <dt><code>P</code></dt>
  959. <dd><p>Print the pattern space, up to the first &lt;newline&gt;.
  960. </p>
  961. </dd>
  962. <dt><code>q<var>[exit-code]</var></code></dt>
  963. <dd><p>(quit) Exit <code>sed</code> without processing any more commands or input.
  964. </p>
  965. </dd>
  966. <dt><code>Q<var>[exit-code]</var></code></dt>
  967. <dd><p>(quit) This command is the same as <code>q</code>, but will not print the
  968. contents of pattern space.  Like <code>q</code>, it provides the
  969. ability to return an exit code to the caller.
  970. </p>
  971. </dd>
  972. <dt><code>r filename</code></dt>
  973. <dd><p>Reads file <var>filename</var>.
  974. </p>
  975. </dd>
  976. <dt><code>R filename</code></dt>
  977. <dd><p>Queue a line of <var>filename</var> to be read and
  978. inserted into the output stream at the end of the current cycle,
  979. or when the next input line is read.
  980. </p>
  981. </dd>
  982. <dt><code>s<var>/regexp/replacement/[flags]</var></code></dt>
  983. <dd><p>(substitute) Match the regular-expression against the content of the
  984. pattern space.  If found, replace matched string with
  985. <var>replacement</var>.
  986. </p>
  987. </dd>
  988. <dt><code>t <var>label</var></code></dt>
  989. <dd><p>(test) Branch to <var>label</var> only if there has been a successful
  990. <code>s</code>ubstitution since the last input line was read or conditional
  991. branch was taken.  The <var>label</var> may be omitted, in which case the
  992. next cycle is started.
  993. </p>
  994. </dd>
  995. <dt><code>T <var>label</var></code></dt>
  996. <dd><p>(test) Branch to <var>label</var> only if there have been no successful
  997. <code>s</code>ubstitutions since the last input line was read or
  998. conditional branch was taken. The <var>label</var> may be omitted,
  999. in which case the next cycle is started.
  1000. </p>
  1001. </dd>
  1002. <dt><code>v <var>[version]</var></code></dt>
  1003. <dd><p>(version) This command does nothing, but makes <code>sed</code> fail if
  1004. GNU <code>sed</code> extensions are not supported, or if the requested version
  1005. is not available.
  1006. </p>
  1007. </dd>
  1008. <dt><code>w filename</code></dt>
  1009. <dd><p>Write the pattern space to <var>filename</var>.
  1010. </p>
  1011. </dd>
  1012. <dt><code>W filename</code></dt>
  1013. <dd><p>Write to the given filename the portion of the pattern space up to
  1014. the first newline
  1015. </p>
  1016. </dd>
  1017. <dt><code>x</code></dt>
  1018. <dd><p>Exchange the contents of the hold and pattern spaces.
  1019. </p>
  1020.  
  1021. </dd>
  1022. <dt><code>y/src/dst/</code></dt>
  1023. <dd><p>Transliterate any characters in the pattern space which match
  1024. any of the <var>source-chars</var> with the corresponding character
  1025. in <var>dest-chars</var>.
  1026. </p>
  1027.  
  1028. </dd>
  1029. <dt><code>z</code></dt>
  1030. <dd><p>(zap) This command empties the content of pattern space.
  1031. </p>
  1032. </dd>
  1033. <dt><code>#</code></dt>
  1034. <dd><p>A comment, until  the next newline.
  1035. </p>
  1036.  
  1037. </dd>
  1038. <dt><code>{ <var>cmd ; cmd ...</var> }</code></dt>
  1039. <dd><p>Group several commands together.
  1040. </p>
  1041. </dd>
  1042. <dt><code>=</code></dt>
  1043. <dd><p>Print the current input line number (with a trailing newline).
  1044. </p>
  1045. </dd>
  1046. <dt><code>: <var>label</var></code></dt>
  1047. <dd><p>Specify the location of <var>label</var> for branch commands (<code>b</code>,
  1048. <code>t</code>, <code>T</code>).
  1049. </p>
  1050. </dd>
  1051. </dl>
  1052.  
  1053.  
  1054. <hr>
  1055. <a name="The-_0022s_0022-Command"></a>
  1056. <div class="header">
  1057. <p>
  1058. Next: <a href="#Common-Commands" accesskey="n" rel="next">Common Commands</a>, Previous: <a href="#sed-commands-list" accesskey="p" rel="prev">sed commands list</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  1059. </div>
  1060. <a name="The-s-Command"></a>
  1061. <h3 class="section">3.3 The <code>s</code> Command</h3>
  1062.  
  1063. <p>The <code>s</code> command (as in substitute) is probably the most important
  1064. in <code>sed</code> and has a lot of different options.  The syntax of
  1065. the <code>s</code> command is
  1066. &lsquo;<samp>s/<var>regexp</var>/<var>replacement</var>/<var>flags</var></samp>&rsquo;.
  1067. </p>
  1068. <p>Its basic concept is simple: the <code>s</code> command attempts to match
  1069. the pattern space against the supplied regular expression <var>regexp</var>;
  1070. if the match is successful, then that portion of the
  1071. pattern space which was matched is replaced with <var>replacement</var>.
  1072. </p>
  1073. <p>For details about <var>regexp</var> syntax see <a href="#Regexp-Addresses">Regular
  1074. Expression Addresses</a>.
  1075. </p>
  1076. <a name="index-Backreferences_002c-in-regular-expressions"></a>
  1077. <a name="index-Parenthesized-substrings"></a>
  1078. <p>The <var>replacement</var> can contain <code>\<var>n</var></code> (<var>n</var> being
  1079. a number from 1 to 9, inclusive) references, which refer to
  1080. the portion of the match which is contained between the <var>n</var>th
  1081. <code>\(</code> and its matching <code>\)</code>.
  1082. Also, the <var>replacement</var> can contain unescaped <code>&amp;</code>
  1083. characters which reference the whole matched portion
  1084. of the pattern space.
  1085. </p>
  1086.  
  1087. <p>The <code>/</code>
  1088. characters may be uniformly replaced by any other single
  1089. character within any given <code>s</code> command.  The <code>/</code>
  1090. character (or whatever other character is used in its stead)
  1091. can appear in the <var>regexp</var> or <var>replacement</var>
  1092. only if it is preceded by a <code>\</code> character.
  1093. </p>
  1094.  
  1095.  
  1096. <a name="index-GNU-extensions_002c-case-modifiers-in-s-commands"></a>
  1097. <p>Finally, as a GNU <code>sed</code> extension, you can include a
  1098. special sequence made of a backslash and one of the letters
  1099. <code>L</code>, <code>l</code>, <code>U</code>, <code>u</code>, or <code>E</code>.
  1100. The meaning is as follows:
  1101. </p>
  1102. <dl compact="compact">
  1103. <dt><code>\L</code></dt>
  1104. <dd><p>Turn the replacement
  1105. to lowercase until a <code>\U</code> or <code>\E</code> is found,
  1106. </p>
  1107. </dd>
  1108. <dt><code>\l</code></dt>
  1109. <dd><p>Turn the
  1110. next character to lowercase,
  1111. </p>
  1112. </dd>
  1113. <dt><code>\U</code></dt>
  1114. <dd><p>Turn the replacement to uppercase
  1115. until a <code>\L</code> or <code>\E</code> is found,
  1116. </p>
  1117. </dd>
  1118. <dt><code>\u</code></dt>
  1119. <dd><p>Turn the next character
  1120. to uppercase,
  1121. </p>
  1122. </dd>
  1123. <dt><code>\E</code></dt>
  1124. <dd><p>Stop case conversion started by <code>\L</code> or <code>\U</code>.
  1125. </p></dd>
  1126. </dl>
  1127.  
  1128. <p>When the <code>g</code> flag is being used, case conversion does not
  1129. propagate from one occurrence of the regular expression to
  1130. another.  For example, when the following command is executed
  1131. with &lsquo;<samp>a-b-</samp>&rsquo; in pattern space:
  1132. </p><div class="example">
  1133. <pre class="example">s/\(b\?\)-/x\u\1/g
  1134. </pre></div>
  1135.  
  1136. <p>the output is &lsquo;<samp>axxB</samp>&rsquo;.  When replacing the first &lsquo;<samp>-</samp>&rsquo;,
  1137. the &lsquo;<samp>\u</samp>&rsquo; sequence only affects the empty replacement of
  1138. &lsquo;<samp>\1</samp>&rsquo;.  It does not affect the <code>x</code> character that is
  1139. added to pattern space when replacing <code>b-</code> with <code>xB</code>.
  1140. </p>
  1141. <p>On the other hand, <code>\l</code> and <code>\u</code> do affect the remainder
  1142. of the replacement text if they are followed by an empty substitution.
  1143. With &lsquo;<samp>a-b-</samp>&rsquo; in pattern space, the following command:
  1144. </p><div class="example">
  1145. <pre class="example">s/\(b\?\)-/\u\1x/g
  1146. </pre></div>
  1147.  
  1148. <p>will replace &lsquo;<samp>-</samp>&rsquo; with &lsquo;<samp>X</samp>&rsquo; (uppercase) and &lsquo;<samp>b-</samp>&rsquo; with
  1149. &lsquo;<samp>Bx</samp>&rsquo;.  If this behavior is undesirable, you can prevent it by
  1150. adding a &lsquo;<samp>\E</samp>&rsquo; sequence&mdash;after &lsquo;<samp>\1</samp>&rsquo; in this case.
  1151. </p>
  1152. <p>To include a literal <code>\</code>, <code>&amp;</code>, or newline in the final
  1153. replacement, be sure to precede the desired <code>\</code>, <code>&amp;</code>,
  1154. or newline in the <var>replacement</var> with a <code>\</code>.
  1155. </p>
  1156. <a name="index-s-command_002c-option-flags"></a>
  1157. <a name="index-Substitution-of-text_002c-options"></a>
  1158. <p>The <code>s</code> command can be followed by zero or more of the
  1159. following <var>flags</var>:
  1160. </p>
  1161. <dl compact="compact">
  1162. <dt><code>g</code></dt>
  1163. <dd><a name="index-Global-substitution"></a>
  1164. <a name="index-Replacing-all-text-matching-regexp-in-a-line"></a>
  1165. <p>Apply the replacement to <em>all</em> matches to the <var>regexp</var>,
  1166. not just the first.
  1167. </p>
  1168. </dd>
  1169. <dt><code><var>number</var></code></dt>
  1170. <dd><a name="index-Replacing-only-nth-match-of-regexp-in-a-line"></a>
  1171. <p>Only replace the <var>number</var>th match of the <var>regexp</var>.
  1172. </p>
  1173. <a name="index-GNU-extensions_002c-g-and-number-modifier"></a>
  1174. <p>interaction in <code>s</code> command
  1175. <a name="index-Mixing-g-and-number-modifiers-in-the-s-command"></a>
  1176. Note: the <small>POSIX</small> standard does not specify what should happen
  1177. when you mix the <code>g</code> and <var>number</var> modifiers,
  1178. and currently there is no widely agreed upon meaning
  1179. across <code>sed</code> implementations.
  1180. For GNU <code>sed</code>, the interaction is defined to be:
  1181. ignore matches before the <var>number</var>th,
  1182. and then match and replace all matches from
  1183. the <var>number</var>th on.
  1184. </p>
  1185. </dd>
  1186. <dt><code>p</code></dt>
  1187. <dd><a name="index-Text_002c-printing-after-substitution"></a>
  1188. <p>If the substitution was made, then print the new pattern space.
  1189. </p>
  1190. <p>Note: when both the <code>p</code> and <code>e</code> options are specified,
  1191. the relative ordering of the two produces very different results.
  1192. In general, <code>ep</code> (evaluate then print) is what you want,
  1193. but operating the other way round can be useful for debugging.
  1194. For this reason, the current version of GNU <code>sed</code> interprets
  1195. specially the presence of <code>p</code> options both before and after
  1196. <code>e</code>, printing the pattern space before and after evaluation,
  1197. while in general flags for the <code>s</code> command show their
  1198. effect just once.  This behavior, although documented, might
  1199. change in future versions.
  1200. </p>
  1201. </dd>
  1202. <dt><code>w <var>filename</var></code></dt>
  1203. <dd><a name="index-Text_002c-writing-to-a-file-after-substitution"></a>
  1204. <a name="index-GNU-extensions_002c-_002fdev_002fstdout-file-1"></a>
  1205. <a name="index-GNU-extensions_002c-_002fdev_002fstderr-file"></a>
  1206. <p>If the substitution was made, then write out the result to the named file.
  1207. As a GNU <code>sed</code> extension, two special values of <var>filename</var> are
  1208. supported: <samp>/dev/stderr</samp>, which writes the result to the standard
  1209. error, and <samp>/dev/stdout</samp>, which writes to the standard
  1210. output.<a name="DOCF3" href="#FOOT3"><sup>3</sup></a>
  1211. </p>
  1212. </dd>
  1213. <dt><code>e</code></dt>
  1214. <dd><a name="index-Evaluate-Bourne_002dshell-commands_002c-after-substitution"></a>
  1215. <a name="index-Subprocesses"></a>
  1216. <a name="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands"></a>
  1217. <a name="index-GNU-extensions_002c-subprocesses"></a>
  1218. <p>This command allows one to pipe input from a shell command
  1219. into pattern space.  If a substitution was made, the command
  1220. that is found in pattern space is executed and pattern space
  1221. is replaced with its output.  A trailing newline is suppressed;
  1222. results are undefined if the command to be executed contains
  1223. a <small>NUL</small> character.  This is a GNU <code>sed</code> extension.
  1224. </p>
  1225. </dd>
  1226. <dt><code>I</code></dt>
  1227. <dt><code>i</code></dt>
  1228. <dd><a name="index-GNU-extensions_002c-I-modifier"></a>
  1229. <a name="index-Case_002dinsensitive-matching"></a>
  1230. <p>The <code>I</code> modifier to regular-expression matching is a GNU
  1231. extension which makes <code>sed</code> match <var>regexp</var> in a
  1232. case-insensitive manner.
  1233. </p>
  1234. </dd>
  1235. <dt><code>M</code></dt>
  1236. <dt><code>m</code></dt>
  1237. <dd><a name="index-GNU-extensions_002c-M-modifier"></a>
  1238. <p>The <code>M</code> modifier to regular-expression matching is a GNU <code>sed</code>
  1239. extension which directs GNU <code>sed</code> to match the regular expression
  1240. in <cite>multi-line</cite> mode.  The modifier causes <code>^</code> and <code>$</code> to
  1241. match respectively (in addition to the normal behavior) the empty string
  1242. after a newline, and the empty string before a newline.  There are
  1243. special character sequences
  1244. (<code>\`</code> and <code>\'</code>)
  1245. which always match the beginning or the end of the buffer.
  1246. In addition,
  1247. the period character does not match a new-line character in
  1248. multi-line mode.
  1249. </p>
  1250.  
  1251. </dd>
  1252. </dl>
  1253.  
  1254. <hr>
  1255. <a name="Common-Commands"></a>
  1256. <div class="header">
  1257. <p>
  1258. Next: <a href="#Other-Commands" accesskey="n" rel="next">Other Commands</a>, Previous: <a href="#The-_0022s_0022-Command" accesskey="p" rel="prev">The &quot;s&quot; Command</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  1259. </div>
  1260. <a name="Often_002dUsed-Commands"></a>
  1261. <h3 class="section">3.4 Often-Used Commands</h3>
  1262.  
  1263. <p>If you use <code>sed</code> at all, you will quite likely want to know
  1264. these commands.
  1265. </p>
  1266. <dl compact="compact">
  1267. <dt><code>#</code></dt>
  1268. <dd><p>[No addresses allowed.]
  1269. </p>
  1270. <a name="index-_0023-_0028comments_0029"></a>
  1271. <a name="index-Comments_002c-in-scripts"></a>
  1272. <p>The <code>#</code> character begins a comment;
  1273. the comment continues until the next newline.
  1274. </p>
  1275. <a name="index-Portability_002c-comments"></a>
  1276. <p>If you are concerned about portability, be aware that
  1277. some implementations of <code>sed</code> (which are not <small>POSIX</small>
  1278. conforming) may only support a single one-line comment,
  1279. and then only when the very first character of the script is a <code>#</code>.
  1280. </p>
  1281. <a name="index-_002dn_002c-forcing-from-within-a-script"></a>
  1282. <a name="index-Caveat-_002d_002d_002d-_0023n-on-first-line"></a>
  1283. <p>Warning: if the first two characters of the <code>sed</code> script
  1284. are <code>#n</code>, then the <samp>-n</samp> (no-autoprint) option is forced.
  1285. If you want to put a comment in the first line of your script
  1286. and that comment begins with the letter &lsquo;<samp>n</samp>&rsquo;
  1287. and you do not want this behavior,
  1288. then be sure to either use a capital &lsquo;<samp>N</samp>&rsquo;,
  1289. or place at least one space before the &lsquo;<samp>n</samp>&rsquo;.
  1290. </p>
  1291. </dd>
  1292. <dt><code>q [<var>exit-code</var>]</code></dt>
  1293. <dd><a name="index-q-_0028quit_0029-command"></a>
  1294. <a name="index-GNU-extensions_002c-returning-an-exit-code"></a>
  1295. <a name="index-Quitting"></a>
  1296. <p>Exit <code>sed</code> without processing any more commands or input.
  1297. </p>
  1298. <p>Example: stop after printing the second line:
  1299. </p><div class="example">
  1300. <pre class="example">$ seq 3 | sed 2q
  1301. 1
  1302. 2
  1303. </pre></div>
  1304.  
  1305. <p>This command accepts only one address.
  1306. Note that the current pattern space is printed if auto-print is
  1307. not disabled with the <samp>-n</samp> options.  The ability to return
  1308. an exit code from the <code>sed</code> script is a GNU <code>sed</code> extension.
  1309. </p>
  1310. <p>See also the GNU <code>sed</code> extension <code>Q</code> command which quits silently
  1311. without printing the current pattern space.
  1312. </p>
  1313. </dd>
  1314. <dt><code>d</code></dt>
  1315. <dd><a name="index-d-_0028delete_0029-command"></a>
  1316. <a name="index-Text_002c-deleting"></a>
  1317. <p>Delete the pattern space;
  1318. immediately start next cycle.
  1319. </p>
  1320. <p>Example: delete the second input line:
  1321. </p><div class="example">
  1322. <pre class="example">$ seq 3 | sed 2d
  1323. 1
  1324. 3
  1325. </pre></div>
  1326.  
  1327. </dd>
  1328. <dt><code>p</code></dt>
  1329. <dd><a name="index-p-_0028print_0029-command"></a>
  1330. <a name="index-Text_002c-printing"></a>
  1331. <p>Print out the pattern space (to the standard output).
  1332. This command is usually only used in conjunction with the <samp>-n</samp>
  1333. command-line option.
  1334. </p>
  1335. <p>Example: print only the second input line:
  1336. </p><div class="example">
  1337. <pre class="example">$ seq 3 | sed -n 2p
  1338. 2
  1339. </pre></div>
  1340.  
  1341. </dd>
  1342. <dt><code>n</code></dt>
  1343. <dd><a name="index-n-_0028next_002dline_0029-command"></a>
  1344. <a name="index-Next-input-line_002c-replace-pattern-space-with"></a>
  1345. <a name="index-Read-next-input-line"></a>
  1346. <p>If auto-print is not disabled, print the pattern space,
  1347. then, regardless, replace the pattern space with the next line of input.
  1348. If there is no more input then <code>sed</code> exits without processing
  1349. any more commands.
  1350. </p>
  1351. <p>This command is useful to skip lines (e.g. process every Nth line).
  1352. </p>
  1353. <p>Example: perform substitution on every 3rd line (i.e. two <code>n</code> commands
  1354. skip two lines):
  1355. </p><div class="example">
  1356. <pre class="example">$ seq 6 | sed 'n;n;s/./x/'
  1357. 1
  1358. 2
  1359. x
  1360. 4
  1361. 5
  1362. x
  1363. </pre></div>
  1364.  
  1365. <p>GNU <code>sed</code> provides an extension address syntax of <var>first</var>~<var>step</var>
  1366. to achieve the same result:
  1367. </p>
  1368. <div class="example">
  1369. <pre class="example">$ seq 6 | sed '0~3s/./x/'
  1370. 1
  1371. 2
  1372. x
  1373. 4
  1374. 5
  1375. x
  1376. </pre></div>
  1377.  
  1378.  
  1379.  
  1380. </dd>
  1381. <dt><code>{ <var>commands</var> }</code></dt>
  1382. <dd><a name="index-_007b_007d-command-grouping"></a>
  1383. <a name="index-Grouping-commands"></a>
  1384. <a name="index-Command-groups"></a>
  1385. <p>A group of commands may be enclosed between
  1386. <code>{</code> and <code>}</code> characters.
  1387. This is particularly useful when you want a group of commands
  1388. to be triggered by a single address (or address-range) match.
  1389. </p>
  1390. <p>Example: perform substitution then print the second input line:
  1391. </p><div class="example">
  1392. <pre class="example">$ seq 3 | sed -n '2{s/2/X/ ; p}'
  1393. X
  1394. </pre></div>
  1395.  
  1396. </dd>
  1397. </dl>
  1398.  
  1399.  
  1400. <hr>
  1401. <a name="Other-Commands"></a>
  1402. <div class="header">
  1403. <p>
  1404. Next: <a href="#Programming-Commands" accesskey="n" rel="next">Programming Commands</a>, Previous: <a href="#Common-Commands" accesskey="p" rel="prev">Common Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  1405. </div>
  1406. <a name="Less-Frequently_002dUsed-Commands"></a>
  1407. <h3 class="section">3.5 Less Frequently-Used Commands</h3>
  1408.  
  1409. <p>Though perhaps less frequently used than those in the previous
  1410. section, some very small yet useful <code>sed</code> scripts can be built with
  1411. these commands.
  1412. </p>
  1413. <dl compact="compact">
  1414. <dt><code>y/<var>source-chars</var>/<var>dest-chars</var>/</code></dt>
  1415. <dd><a name="index-y-_0028transliterate_0029-command"></a>
  1416. <a name="index-Transliteration"></a>
  1417. <p>Transliterate any characters in the pattern space which match
  1418. any of the <var>source-chars</var> with the corresponding character
  1419. in <var>dest-chars</var>.
  1420. </p>
  1421. <p>Example: transliterate &lsquo;<samp>a-j</samp>&rsquo; into &lsquo;<samp>0-9</samp>&rsquo;:
  1422. </p><div class="example">
  1423. <pre class="example">$ echo hello world | sed 'y/abcdefghij/0123456789/'
  1424. 74llo worl3
  1425. </pre></div>
  1426.  
  1427. <p>(The <code>/</code> characters may be uniformly replaced by
  1428. any other single character within any given <code>y</code> command.)
  1429. </p>
  1430. <p>Instances of the <code>/</code> (or whatever other character is used in its stead),
  1431. <code>\</code>, or newlines can appear in the <var>source-chars</var> or <var>dest-chars</var>
  1432. lists, provide that each instance is escaped by a <code>\</code>.
  1433. The <var>source-chars</var> and <var>dest-chars</var> lists <em>must</em>
  1434. contain the same number of characters (after de-escaping).
  1435. </p>
  1436. <p>See the <code>tr</code> command from GNU coreutils for similar functionality.
  1437. </p>
  1438. </dd>
  1439. <dt><code>a <var>text</var></code></dt>
  1440. <dd><p>Appending <var>text</var> after a line. This is a GNU extension
  1441. to the standard <code>a</code> command - see below for details.
  1442. </p>
  1443. <p>Example: Add the word &lsquo;<samp>hello</samp>&rsquo; after the second line:
  1444. </p><div class="example">
  1445. <pre class="example">$ seq 3 | sed '2a hello'
  1446. 1
  1447. 2
  1448. hello
  1449. 3
  1450. </pre></div>
  1451.  
  1452. <p>Leading whitespace after the <code>a</code> command is ignored.
  1453. The text to add is read until the end of the line.
  1454. </p>
  1455.  
  1456. </dd>
  1457. <dt><code>a\</code></dt>
  1458. <dt><code><var>text</var></code></dt>
  1459. <dd><a name="index-a-_0028append-text-lines_0029-command"></a>
  1460. <a name="index-Appending-text-after-a-line"></a>
  1461. <a name="index-Text_002c-appending"></a>
  1462. <p>Appending <var>text</var> after a line.
  1463. </p>
  1464. <p>Example: Add &lsquo;<samp>hello</samp>&rsquo; after the second line
  1465. (-| indicates printed output lines):
  1466. </p><div class="example">
  1467. <pre class="example">$ seq 3 | sed '2a\
  1468. hello'
  1469. -|1
  1470. -|2
  1471. -|hello
  1472. -|3
  1473. </pre></div>
  1474.  
  1475. <p>The <code>a</code> command queues the lines of text which follow this command
  1476. (each but the last ending with a <code>\</code>,
  1477. which are removed from the output)
  1478. to be output at the end of the current cycle,
  1479. or when the next input line is read.
  1480. </p>
  1481. <a name="index-GNU-extensions_002c-two-addresses-supported-by-most-commands"></a>
  1482. <p>As a GNU extension, this command accepts two addresses.
  1483. </p>
  1484. <p>Escape sequences in <var>text</var> are processed, so you should
  1485. use <code>\\</code> in <var>text</var> to print a single backslash.
  1486. </p>
  1487. <p>The commands resume after the last line without a backslash (<code>\</code>) -
  1488. &lsquo;<samp>world</samp>&rsquo; in the following example:
  1489. </p><div class="example">
  1490. <pre class="example">$ seq 3 | sed '2a\
  1491. hello\
  1492. world
  1493. 3s/./X/'
  1494. -|1
  1495. -|2
  1496. -|hello
  1497. -|world
  1498. -|X
  1499. </pre></div>
  1500.  
  1501. <p>As a GNU extension, the <code>a</code> command and <var>text</var> can be
  1502. separated into two <code>-e</code> parameters, enabling easier scripting:
  1503. </p><div class="example">
  1504. <pre class="example">$ seq 3 | sed -e '2a\' -e hello
  1505. 1
  1506. 2
  1507. hello
  1508. 3
  1509.  
  1510. $ sed -e '2a\' -e &quot;$VAR&quot;
  1511. </pre></div>
  1512.  
  1513. </dd>
  1514. <dt><code>i <var>text</var></code></dt>
  1515. <dd><p>insert <var>text</var> before a line. This is a GNU extension
  1516. to the standard <code>i</code> command - see below for details.
  1517. </p>
  1518. <p>Example: Insert the word &lsquo;<samp>hello</samp>&rsquo; before the second line:
  1519. </p><div class="example">
  1520. <pre class="example">$ seq 3 | sed '2i hello'
  1521. 1
  1522. hello
  1523. 2
  1524. 3
  1525. </pre></div>
  1526.  
  1527. <p>Leading whitespace after the <code>i</code> command is ignored.
  1528. The text to add is read until the end of the line.
  1529. </p>
  1530. <a name="insert-command"></a></dd>
  1531. <dt><code>i\</code></dt>
  1532. <dt><code><var>text</var></code></dt>
  1533. <dd><a name="index-i-_0028insert-text-lines_0029-command"></a>
  1534. <a name="index-Inserting-text-before-a-line"></a>
  1535. <a name="index-Text_002c-insertion"></a>
  1536. <p>Immediately output the lines of text which follow this command.
  1537. </p>
  1538. <p>Example: Insert &lsquo;<samp>hello</samp>&rsquo; before the second line
  1539. (-| indicates printed output lines):
  1540. </p><div class="example">
  1541. <pre class="example">$ seq 3 | sed '2i\
  1542. hello'
  1543. -|1
  1544. -|hello
  1545. -|2
  1546. -|3
  1547. </pre></div>
  1548.  
  1549. <a name="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-1"></a>
  1550. <p>As a GNU extension, this command accepts two addresses.
  1551. </p>
  1552. <p>Escape sequences in <var>text</var> are processed, so you should
  1553. use <code>\\</code> in <var>text</var> to print a single backslash.
  1554. </p>
  1555. <p>The commands resume after the last line without a backslash (<code>\</code>) -
  1556. &lsquo;<samp>world</samp>&rsquo; in the following example:
  1557. </p><div class="example">
  1558. <pre class="example">$ seq 3 | sed '2i\
  1559. hello\
  1560. world
  1561. s/./X/'
  1562. -|X
  1563. -|hello
  1564. -|world
  1565. -|X
  1566. -|X
  1567. </pre></div>
  1568.  
  1569. <p>As a GNU extension, the <code>i</code> command and <var>text</var> can be
  1570. separated into two <code>-e</code> parameters, enabling easier scripting:
  1571. </p><div class="example">
  1572. <pre class="example">$ seq 3 | sed -e '2i\' -e hello
  1573. 1
  1574. hello
  1575. 2
  1576. 3
  1577.  
  1578. $ sed -e '2i\' -e &quot;$VAR&quot;
  1579. </pre></div>
  1580.  
  1581. </dd>
  1582. <dt><code>c <var>text</var></code></dt>
  1583. <dd><p>Replaces the line(s) with <var>text</var>. This is a GNU extension
  1584. to the standard <code>c</code> command - see below for details.
  1585. </p>
  1586. <p>Example: Replace the 2nd to 9th lines with the word &lsquo;<samp>hello</samp>&rsquo;:
  1587. </p><div class="example">
  1588. <pre class="example">$ seq 10 | sed '2,9c hello'
  1589. 1
  1590. hello
  1591. 10
  1592. </pre></div>
  1593.  
  1594. <p>Leading whitespace after the <code>c</code> command is ignored.
  1595. The text to add is read until the end of the line.
  1596. </p>
  1597. </dd>
  1598. <dt><code>c\</code></dt>
  1599. <dt><code><var>text</var></code></dt>
  1600. <dd><a name="index-c-_0028change-to-text-lines_0029-command"></a>
  1601. <a name="index-Replacing-selected-lines-with-other-text"></a>
  1602. <p>Delete the lines matching the address or address-range,
  1603. and output the lines of text which follow this command.
  1604. </p>
  1605. <p>Example: Replace 2nd to 4th lines with the words &lsquo;<samp>hello</samp>&rsquo; and
  1606. &lsquo;<samp>world</samp>&rsquo; (-| indicates printed output lines):
  1607. </p><div class="example">
  1608. <pre class="example">$ seq 5 | sed '2,4c\
  1609. hello\
  1610. world'
  1611. -|1
  1612. -|hello
  1613. -|world
  1614. -|5
  1615. </pre></div>
  1616.  
  1617. <p>If no addresses are given, each line is replaced.
  1618. </p>
  1619. <p>A new cycle is started after this command is done,
  1620. since the pattern space will have been deleted.
  1621. In the following example, the <code>c</code> starts a
  1622. new cycle and the substitution command is not performed
  1623. on the replaced text:
  1624. </p>
  1625. <div class="example">
  1626. <pre class="example">$ seq 3 | sed '2c\
  1627. hello
  1628. s/./X/'
  1629. -|X
  1630. -|hello
  1631. -|X
  1632. </pre></div>
  1633.  
  1634. <p>As a GNU extension, the <code>c</code> command and <var>text</var> can be
  1635. separated into two <code>-e</code> parameters, enabling easier scripting:
  1636. </p><div class="example">
  1637. <pre class="example">$ seq 3 | sed -e '2c\' -e hello
  1638. 1
  1639. hello
  1640. 3
  1641.  
  1642. $ sed -e '2c\' -e &quot;$VAR&quot;
  1643. </pre></div>
  1644.  
  1645.  
  1646. </dd>
  1647. <dt><code>=</code></dt>
  1648. <dd><a name="index-_003d-_0028print-line-number_0029-command"></a>
  1649. <a name="index-Printing-line-number"></a>
  1650. <a name="index-Line-number_002c-printing"></a>
  1651. <p>Print out the current input line number (with a trailing newline).
  1652. </p>
  1653. <div class="example">
  1654. <pre class="example">$ printf '%s\n' aaa bbb ccc | sed =
  1655. 1
  1656. aaa
  1657. 2
  1658. bbb
  1659. 3
  1660. ccc
  1661. </pre></div>
  1662.  
  1663. <a name="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-2"></a>
  1664. <p>As a GNU extension, this command accepts two addresses.
  1665. </p>
  1666.  
  1667.  
  1668.  
  1669. </dd>
  1670. <dt><code>l <var>n</var></code></dt>
  1671. <dd><a name="index-l-_0028list-unambiguously_0029-command"></a>
  1672. <a name="index-List-pattern-space"></a>
  1673. <a name="index-Printing-text-unambiguously"></a>
  1674. <a name="index-Line-length_002c-setting-1"></a>
  1675. <a name="index-GNU-extensions_002c-setting-line-length"></a>
  1676. <p>Print the pattern space in an unambiguous form:
  1677. non-printable characters (and the <code>\</code> character)
  1678. are printed in C-style escaped form; long lines are split,
  1679. with a trailing <code>\</code> character to indicate the split;
  1680. the end of each line is marked with a <code>$</code>.
  1681. </p>
  1682. <p><var>n</var> specifies the desired line-wrap length;
  1683. a length of 0 (zero) means to never wrap long lines.  If omitted,
  1684. the default as specified on the command line is used.  The <var>n</var>
  1685. parameter is a GNU <code>sed</code> extension.
  1686. </p>
  1687. </dd>
  1688. <dt><code>r <var>filename</var></code></dt>
  1689. <dd>
  1690. <a name="index-r-_0028read-file_0029-command"></a>
  1691. <a name="index-Read-text-from-a-file"></a>
  1692. <p>Reads file <var>filename</var>. Example:
  1693. </p>
  1694. <div class="example">
  1695. <pre class="example">$ seq 3 | sed '2r/etc/hostname'
  1696. 1
  1697. 2
  1698. fencepost.gnu.org
  1699. 3
  1700. </pre></div>
  1701.  
  1702. <a name="index-GNU-extensions_002c-_002fdev_002fstdin-file"></a>
  1703. <p>Queue the contents of <var>filename</var> to be read and
  1704. inserted into the output stream at the end of the current cycle,
  1705. or when the next input line is read.
  1706. Note that if <var>filename</var> cannot be read, it is treated as
  1707. if it were an empty file, without any error indication.
  1708. </p>
  1709. <p>As a GNU <code>sed</code> extension, the special value <samp>/dev/stdin</samp>
  1710. is supported for the file name, which reads the contents of the
  1711. standard input.
  1712. </p>
  1713. <a name="index-GNU-extensions_002c-two-addresses-supported-by-most-commands-3"></a>
  1714. <p>As a GNU extension, this command accepts two addresses. The
  1715. file will then be reread and inserted on each of the addressed lines.
  1716. </p>
  1717. </dd>
  1718. <dt><code>w <var>filename</var></code></dt>
  1719. <dd><a name="index-w-_0028write-file_0029-command"></a>
  1720. <a name="index-Write-to-a-file"></a>
  1721. <a name="index-GNU-extensions_002c-_002fdev_002fstdout-file-2"></a>
  1722. <a name="index-GNU-extensions_002c-_002fdev_002fstderr-file-1"></a>
  1723. <p>Write the pattern space to <var>filename</var>.
  1724. As a GNU <code>sed</code> extension, two special values of <var>filename</var> are
  1725. supported: <samp>/dev/stderr</samp>, which writes the result to the standard
  1726. error, and <samp>/dev/stdout</samp>, which writes to the standard
  1727. output.<a name="DOCF4" href="#FOOT4"><sup>4</sup></a>
  1728. </p>
  1729. <p>The file will be created (or truncated) before the first input line is
  1730. read; all <code>w</code> commands (including instances of the <code>w</code> flag
  1731. on successful <code>s</code> commands) which refer to the same <var>filename</var>
  1732. are output without closing and reopening the file.
  1733. </p>
  1734. </dd>
  1735. <dt><code>D</code></dt>
  1736. <dd><a name="index-D-_0028delete-first-line_0029-command"></a>
  1737. <a name="index-Delete-first-line-from-pattern-space"></a>
  1738. <p>If pattern space contains no newline, start a normal new cycle as if
  1739. the <code>d</code> command was issued.  Otherwise, delete text in the pattern
  1740. space up to the first newline, and restart cycle with the resultant
  1741. pattern space, without reading a new line of input.
  1742. </p>
  1743. </dd>
  1744. <dt><code>N</code></dt>
  1745. <dd><a name="index-N-_0028append-Next-line_0029-command"></a>
  1746. <a name="index-Next-input-line_002c-append-to-pattern-space"></a>
  1747. <a name="index-Append-next-input-line-to-pattern-space"></a>
  1748. <p>Add a newline to the pattern space,
  1749. then append the next line of input to the pattern space.
  1750. If there is no more input then <code>sed</code> exits without processing
  1751. any more commands.
  1752. </p>
  1753. <p>When <samp>-z</samp> is used, a zero byte (the ascii &lsquo;<samp>NUL</samp>&rsquo; character) is
  1754. added between the lines (instead of a new line).
  1755. </p>
  1756. <p>By default <code>sed</code> does not terminate if there is no &rsquo;next&rsquo; input line.
  1757. This is a GNU extension which can be disabled with <samp>--posix</samp>.
  1758. See <a href="#N_005fcommand_005flast_005fline">N command on the last line</a>.
  1759. </p>
  1760.  
  1761. </dd>
  1762. <dt><code>P</code></dt>
  1763. <dd><a name="index-P-_0028print-first-line_0029-command"></a>
  1764. <a name="index-Print-first-line-from-pattern-space"></a>
  1765. <p>Print out the portion of the pattern space up to the first newline.
  1766. </p>
  1767. </dd>
  1768. <dt><code>h</code></dt>
  1769. <dd><a name="index-h-_0028hold_0029-command"></a>
  1770. <a name="index-Copy-pattern-space-into-hold-space"></a>
  1771. <a name="index-Replace-hold-space-with-copy-of-pattern-space"></a>
  1772. <a name="index-Hold-space_002c-copying-pattern-space-into"></a>
  1773. <p>Replace the contents of the hold space with the contents of the pattern space.
  1774. </p>
  1775. </dd>
  1776. <dt><code>H</code></dt>
  1777. <dd><a name="index-H-_0028append-Hold_0029-command"></a>
  1778. <a name="index-Append-pattern-space-to-hold-space"></a>
  1779. <a name="index-Hold-space_002c-appending-from-pattern-space"></a>
  1780. <p>Append a newline to the contents of the hold space,
  1781. and then append the contents of the pattern space to that of the hold space.
  1782. </p>
  1783. </dd>
  1784. <dt><code>g</code></dt>
  1785. <dd><a name="index-g-_0028get_0029-command"></a>
  1786. <a name="index-Copy-hold-space-into-pattern-space"></a>
  1787. <a name="index-Replace-pattern-space-with-copy-of-hold-space"></a>
  1788. <a name="index-Hold-space_002c-copy-into-pattern-space"></a>
  1789. <p>Replace the contents of the pattern space with the contents of the hold space.
  1790. </p>
  1791. </dd>
  1792. <dt><code>G</code></dt>
  1793. <dd><a name="index-G-_0028appending-Get_0029-command"></a>
  1794. <a name="index-Append-hold-space-to-pattern-space"></a>
  1795. <a name="index-Hold-space_002c-appending-to-pattern-space"></a>
  1796. <p>Append a newline to the contents of the pattern space,
  1797. and then append the contents of the hold space to that of the pattern space.
  1798. </p>
  1799. </dd>
  1800. <dt><code>x</code></dt>
  1801. <dd><a name="index-x-_0028eXchange_0029-command"></a>
  1802. <a name="index-Exchange-hold-space-with-pattern-space"></a>
  1803. <a name="index-Hold-space_002c-exchange-with-pattern-space"></a>
  1804. <p>Exchange the contents of the hold and pattern spaces.
  1805. </p>
  1806. </dd>
  1807. </dl>
  1808.  
  1809.  
  1810. <hr>
  1811. <a name="Programming-Commands"></a>
  1812. <div class="header">
  1813. <p>
  1814. Next: <a href="#Extended-Commands" accesskey="n" rel="next">Extended Commands</a>, Previous: <a href="#Other-Commands" accesskey="p" rel="prev">Other Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  1815. </div>
  1816. <a name="Commands-for-sed-gurus"></a>
  1817. <h3 class="section">3.6 Commands for <code>sed</code> gurus</h3>
  1818.  
  1819. <p>In most cases, use of these commands indicates that you are
  1820. probably better off programming in something like <code>awk</code>
  1821. or Perl.  But occasionally one is committed to sticking
  1822. with <code>sed</code>, and these commands can enable one to write
  1823. quite convoluted scripts.
  1824. </p>
  1825. <a name="index-Flow-of-control-in-scripts"></a>
  1826. <dl compact="compact">
  1827. <dt><code>: <var>label</var></code></dt>
  1828. <dd><p>[No addresses allowed.]
  1829. </p>
  1830. <a name="index-_003a-_0028label_0029-command"></a>
  1831. <a name="index-Labels_002c-in-scripts"></a>
  1832. <p>Specify the location of <var>label</var> for branch commands.
  1833. In all other respects, a no-op.
  1834. </p>
  1835. </dd>
  1836. <dt><code>b <var>label</var></code></dt>
  1837. <dd><a name="index-b-_0028branch_0029-command"></a>
  1838. <a name="index-Branch-to-a-label_002c-unconditionally"></a>
  1839. <a name="index-Goto_002c-in-scripts"></a>
  1840. <p>Unconditionally branch to <var>label</var>.
  1841. The <var>label</var> may be omitted, in which case the next cycle is started.
  1842. </p>
  1843. </dd>
  1844. <dt><code>t <var>label</var></code></dt>
  1845. <dd><a name="index-t-_0028test-and-branch-if-successful_0029-command"></a>
  1846. <a name="index-Branch-to-a-label_002c-if-s_002f_002f_002f-succeeded"></a>
  1847. <a name="index-Conditional-branch"></a>
  1848. <p>Branch to <var>label</var> only if there has been a successful <code>s</code>ubstitution
  1849. since the last input line was read or conditional branch was taken.
  1850. The <var>label</var> may be omitted, in which case the next cycle is started.
  1851. </p>
  1852. </dd>
  1853. </dl>
  1854.  
  1855. <hr>
  1856. <a name="Extended-Commands"></a>
  1857. <div class="header">
  1858. <p>
  1859. Next: <a href="#Multiple-commands-syntax" accesskey="n" rel="next">Multiple commands syntax</a>, Previous: <a href="#Programming-Commands" accesskey="p" rel="prev">Programming Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  1860. </div>
  1861. <a name="Commands-Specific-to-GNU-sed"></a>
  1862. <h3 class="section">3.7 Commands Specific to GNU <code>sed</code></h3>
  1863.  
  1864. <p>These commands are specific to GNU <code>sed</code>, so you
  1865. must use them with care and only when you are sure that
  1866. hindering portability is not evil.  They allow you to check
  1867. for GNU <code>sed</code> extensions or to do tasks that are required
  1868. quite often, yet are unsupported by standard <code>sed</code>s.
  1869. </p>
  1870. <dl compact="compact">
  1871. <dt><code>e [<var>command</var>]</code></dt>
  1872. <dd><a name="index-e-_0028evaluate_0029-command"></a>
  1873. <a name="index-Evaluate-Bourne_002dshell-commands"></a>
  1874. <a name="index-Subprocesses-1"></a>
  1875. <a name="index-GNU-extensions_002c-evaluating-Bourne_002dshell-commands-1"></a>
  1876. <a name="index-GNU-extensions_002c-subprocesses-1"></a>
  1877. <p>This command allows one to pipe input from a shell command
  1878. into pattern space.  Without parameters, the <code>e</code> command
  1879. executes the command that is found in pattern space and
  1880. replaces the pattern space with the output; a trailing newline
  1881. is suppressed.
  1882. </p>
  1883. <p>If a parameter is specified, instead, the <code>e</code> command
  1884. interprets it as a command and sends its output to the output stream.
  1885. The command can run across multiple lines, all but the last ending with
  1886. a back-slash.
  1887. </p>
  1888. <p>In both cases, the results are undefined if the command to be
  1889. executed contains a <small>NUL</small> character.
  1890. </p>
  1891. <p>Note that, unlike the <code>r</code> command, the output of the command will
  1892. be printed immediately; the <code>r</code> command instead delays the output
  1893. to the end of the current cycle.
  1894. </p>
  1895. </dd>
  1896. <dt><code>F</code></dt>
  1897. <dd><a name="index-F-_0028File-name_0029-command"></a>
  1898. <a name="index-Printing-file-name"></a>
  1899. <a name="index-File-name_002c-printing"></a>
  1900. <p>Print out the file name of the current input file (with a trailing
  1901. newline).
  1902. </p>
  1903. </dd>
  1904. <dt><code>Q [<var>exit-code</var>]</code></dt>
  1905. <dd><p>This command accepts only one address.
  1906. </p>
  1907. <a name="index-Q-_0028silent-Quit_0029-command"></a>
  1908. <a name="index-GNU-extensions_002c-quitting-silently"></a>
  1909. <a name="index-GNU-extensions_002c-returning-an-exit-code-1"></a>
  1910. <a name="index-Quitting-1"></a>
  1911. <p>This command is the same as <code>q</code>, but will not print the
  1912. contents of pattern space.  Like <code>q</code>, it provides the
  1913. ability to return an exit code to the caller.
  1914. </p>
  1915. <p>This command can be useful because the only alternative ways
  1916. to accomplish this apparently trivial function are to use
  1917. the <samp>-n</samp> option (which can unnecessarily complicate
  1918. your script) or resorting to the following snippet, which
  1919. wastes time by reading the whole file without any visible effect:
  1920. </p>
  1921. <div class="example">
  1922. <pre class="example">:eat
  1923. $d       <i><span class="roman">Quit silently on the last line</span></i>
  1924. N        <i><span class="roman">Read another line, silently</span></i>
  1925. g        <i><span class="roman">Overwrite pattern space each time to save memory</span></i>
  1926. b eat
  1927. </pre></div>
  1928.  
  1929. </dd>
  1930. <dt><code>R <var>filename</var></code></dt>
  1931. <dd><a name="index-R-_0028read-line_0029-command"></a>
  1932. <a name="index-Read-text-from-a-file-1"></a>
  1933. <a name="index-GNU-extensions_002c-reading-a-file-a-line-at-a-time"></a>
  1934. <a name="index-GNU-extensions_002c-R-command"></a>
  1935. <a name="index-GNU-extensions_002c-_002fdev_002fstdin-file-1"></a>
  1936. <p>Queue a line of <var>filename</var> to be read and
  1937. inserted into the output stream at the end of the current cycle,
  1938. or when the next input line is read.
  1939. Note that if <var>filename</var> cannot be read, or if its end is
  1940. reached, no line is appended, without any error indication.
  1941. </p>
  1942. <p>As with the <code>r</code> command, the special value <samp>/dev/stdin</samp>
  1943. is supported for the file name, which reads a line from the
  1944. standard input.
  1945. </p>
  1946. </dd>
  1947. <dt><code>T <var>label</var></code></dt>
  1948. <dd><a name="index-T-_0028test-and-branch-if-failed_0029-command"></a>
  1949. <a name="index-GNU-extensions_002c-branch-if-s_002f_002f_002f-failed"></a>
  1950. <a name="index-Branch-to-a-label_002c-if-s_002f_002f_002f-failed"></a>
  1951. <a name="index-Conditional-branch-1"></a>
  1952. <p>Branch to <var>label</var> only if there have been no successful
  1953. <code>s</code>ubstitutions since the last input line was read or
  1954. conditional branch was taken. The <var>label</var> may be omitted,
  1955. in which case the next cycle is started.
  1956. </p>
  1957. </dd>
  1958. <dt><code>v <var>version</var></code></dt>
  1959. <dd><a name="index-v-_0028version_0029-command"></a>
  1960. <a name="index-GNU-extensions_002c-checking-for-their-presence"></a>
  1961. <a name="index-Requiring-GNU-sed"></a>
  1962. <p>This command does nothing, but makes <code>sed</code> fail if
  1963. GNU <code>sed</code> extensions are not supported, simply because other
  1964. versions of <code>sed</code> do not implement it.  In addition, you
  1965. can specify the version of <code>sed</code> that your script
  1966. requires, such as <code>4.0.5</code>.  The default is <code>4.0</code>
  1967. because that is the first version that implemented this command.
  1968. </p>
  1969. <p>This command enables all GNU extensions even if
  1970. <code>POSIXLY_CORRECT</code> is set in the environment.
  1971. </p>
  1972. </dd>
  1973. <dt><code>W <var>filename</var></code></dt>
  1974. <dd><a name="index-W-_0028write-first-line_0029-command"></a>
  1975. <a name="index-Write-first-line-to-a-file"></a>
  1976. <a name="index-GNU-extensions_002c-writing-first-line-to-a-file"></a>
  1977. <p>Write to the given filename the portion of the pattern space up to
  1978. the first newline.  Everything said under the <code>w</code> command about
  1979. file handling holds here too.
  1980. </p>
  1981. </dd>
  1982. <dt><code>z</code></dt>
  1983. <dd><a name="index-z-_0028Zap_0029-command"></a>
  1984. <a name="index-GNU-extensions_002c-emptying-pattern-space"></a>
  1985. <a name="index-Emptying-pattern-space"></a>
  1986. <p>This command empties the content of pattern space.  It is
  1987. usually the same as &lsquo;<samp>s/.*//</samp>&rsquo;, but is more efficient
  1988. and works in the presence of invalid multibyte sequences
  1989. in the input stream.  <small>POSIX</small> mandates that such sequences
  1990. are <em>not</em> matched by &lsquo;<samp>.</samp>&rsquo;, so that there is no portable
  1991. way to clear <code>sed</code>&rsquo;s buffers in the middle of the
  1992. script in most multibyte locales (including UTF-8 locales).
  1993. </p></dd>
  1994. </dl>
  1995.  
  1996.  
  1997. <hr>
  1998. <a name="Multiple-commands-syntax"></a>
  1999. <div class="header">
  2000. <p>
  2001. Previous: <a href="#Extended-Commands" accesskey="p" rel="prev">Extended Commands</a>, Up: <a href="#sed-scripts" accesskey="u" rel="up">sed scripts</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2002. </div>
  2003. <a name="Multiple-commands-syntax-1"></a>
  2004. <h3 class="section">3.8 Multiple commands syntax</h3>
  2005.  
  2006.  
  2007. <p>There are several methods to specify multiple commands in a <code>sed</code>
  2008. program.
  2009. </p>
  2010. <p>Using newlines is most natural when running a sed script from a file
  2011. (using the <samp>-f</samp> option).
  2012. </p>
  2013. <p>On the command line, all <code>sed</code> commands may be separated by newlines.
  2014. Alternatively, you may specify each command as an argument to an <samp>-e</samp>
  2015. option:
  2016. </p>
  2017. <div class="example">
  2018. <pre class="example">$ seq 6 | sed '1d
  2019. 3d
  2020. 5d'
  2021. 2
  2022. 4
  2023. 6
  2024.  
  2025. $ seq 6 | sed -e 1d -e 3d -e 5d
  2026. 2
  2027. 4
  2028. 6
  2029. </pre></div>
  2030.  
  2031. <p>A semicolon (&lsquo;<samp>;</samp>&rsquo;) may be used to separate most simple commands:
  2032. </p>
  2033. <div class="example">
  2034. <pre class="example">$ seq 6 | sed '1d;3d;5d'
  2035. 2
  2036. 4
  2037. 6
  2038. </pre></div>
  2039.  
  2040. <p>The <code>{</code>,<code>}</code>,<code>b</code>,<code>t</code>,<code>T</code>,<code>:</code> commands can
  2041. be separated with a semicolon (this is a non-portable GNU <code>sed</code> extension).
  2042. </p>
  2043. <div class="example">
  2044. <pre class="example">$ seq 4 | sed '{1d;3d}'
  2045. 2
  2046. 4
  2047.  
  2048. $ seq 6 | sed '{1d;3d};5d'
  2049. 2
  2050. 4
  2051. 6
  2052. </pre></div>
  2053.  
  2054. <p>Labels used in <code>b</code>,<code>t</code>,<code>T</code>,<code>:</code> commands are read
  2055. until a semicolon.  Leading and trailing whitespace is ignored.  In
  2056. the examples below the label is &lsquo;<samp>x</samp>&rsquo;.  The first example works
  2057. with GNU <code>sed</code>.  The second is a portable equivalent.  For more
  2058. information about branching and labels see <a href="#Branching-and-flow-control">Branching and flow control</a>.
  2059. </p>
  2060. <div class="example">
  2061. <pre class="example">$ seq 3 | sed '/1/b x ; s/^/=/ ; :x ; 3d'
  2062. 1
  2063. =2
  2064.  
  2065. $ seq 3 | sed -e '/1/bx' -e 's/^/=/' -e ':x' -e '3d'
  2066. 1
  2067. =2
  2068. </pre></div>
  2069.  
  2070.  
  2071.  
  2072. <a name="Commands-Requiring-a-newline"></a>
  2073. <h4 class="subsection">3.8.1 Commands Requiring a newline</h4>
  2074.  
  2075. <p>The following commands cannot be separated by a semicolon and
  2076. require a newline:
  2077. </p>
  2078. <dl compact="compact">
  2079. <dt><code>a</code>,<code>c</code>,<code>i</code> (append/change/insert)</dt>
  2080. <dd>
  2081. <p>All characters following <code>a</code>,<code>c</code>,<code>i</code> commands are taken
  2082. as the text to append/change/insert.  Using a semicolon leads to
  2083. undesirable results:
  2084. </p>
  2085. <div class="example">
  2086. <pre class="example">$ seq 2 | sed '1aHello ; 2d'
  2087. 1
  2088. Hello ; 2d
  2089. 2
  2090. </pre></div>
  2091.  
  2092. <p>Separate the commands using <samp>-e</samp> or a newline:
  2093. </p>
  2094. <div class="example">
  2095. <pre class="example">$ seq 2 | sed -e 1aHello -e 2d
  2096. 1
  2097. Hello
  2098.  
  2099. $ seq 2 | sed '1aHello
  2100. 2d'
  2101. 1
  2102. Hello
  2103. </pre></div>
  2104.  
  2105. <p>Note that specifying the text to add (&lsquo;<samp>Hello</samp>&rsquo;) immediately
  2106. after <code>a</code>,<code>c</code>,<code>i</code> is itself a GNU <code>sed</code> extension.
  2107. A portable, POSIX-compliant alternative is:
  2108. </p>
  2109. <div class="example">
  2110. <pre class="example">$ seq 2 | sed '1a\
  2111. Hello
  2112. 2d'
  2113. 1
  2114. Hello
  2115. </pre></div>
  2116.  
  2117. </dd>
  2118. <dt><code>#</code> (comment)</dt>
  2119. <dd>
  2120. <p>All characters following &lsquo;<samp>#</samp>&rsquo; until the next newline are ignored.
  2121. </p>
  2122. <div class="example">
  2123. <pre class="example">$ seq 3 | sed '# this is a comment ; 2d'
  2124. 1
  2125. 2
  2126. 3
  2127.  
  2128.  
  2129. $ seq 3 | sed '# this is a comment
  2130. 2d'
  2131. 1
  2132. 3
  2133. </pre></div>
  2134.  
  2135. </dd>
  2136. <dt><code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> (reading and writing files)</dt>
  2137. <dd>
  2138. <p>The <code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> commands parse the filename
  2139. until end of the line.  If whitespace, comments or semicolons are found,
  2140. they will be included in the filename, leading to unexpected results:
  2141. </p>
  2142. <div class="example">
  2143. <pre class="example">$ seq 2 | sed '1w hello.txt ; 2d'
  2144. 1
  2145. 2
  2146.  
  2147. $ ls -log
  2148. total 4
  2149. -rw-rw-r-- 1 2 Jan 23 23:03 hello.txt ; 2d
  2150.  
  2151. $ cat 'hello.txt ; 2d'
  2152. 1
  2153. </pre></div>
  2154.  
  2155. <p>Note that <code>sed</code> silently ignores read/write errors in
  2156. <code>r</code>,<code>R</code>,<code>w</code>,<code>W</code> commands (such as missing files).
  2157. In the following example, <code>sed</code> tries to read a file named
  2158. &lsquo;<samp><samp>hello.txt ; N</samp></samp>&rsquo;. The file is missing, and the error is silently
  2159. ignored:
  2160. </p>
  2161. <div class="example">
  2162. <pre class="example">$ echo x | sed '1rhello.txt ; N'
  2163. x
  2164. </pre></div>
  2165.  
  2166. </dd>
  2167. <dt><code>e</code> (command execution)</dt>
  2168. <dd>
  2169. <p>Any characters following the <code>e</code> command until the end of the line
  2170. will be sent to the shell.  If whitespace, comments or semicolons are found,
  2171. they will be included in the shell command, leading to unexpected results:
  2172. </p>
  2173. <div class="example">
  2174. <pre class="example">$ echo a | sed '1e touch foo#bar'
  2175. a
  2176.  
  2177. $ ls -1
  2178. foo#bar
  2179.  
  2180. $ echo a | sed '1e touch foo ; s/a/b/'
  2181. sh: 1: s/a/b/: not found
  2182. a
  2183. </pre></div>
  2184.  
  2185.  
  2186. </dd>
  2187. <dt><code>s///[we]</code> (substitute with <code>e</code> or <code>w</code> flags)</dt>
  2188. <dd>
  2189. <p>In a substitution command, the <code>w</code> flag writes the substitution
  2190. result to a file, and the <code>e</code> flag executes the subsitution result
  2191. as a shell command.  As with the <code>r/R/w/W/e</code> commands, these
  2192. must be terminated with a newline.  If whitespace, comments or semicolons
  2193. are found, they will be included in the shell command or filename, leading to
  2194. unexpected results:
  2195. </p>
  2196. <div class="example">
  2197. <pre class="example">$ echo a | sed 's/a/b/w1.txt#foo'
  2198. b
  2199.  
  2200. $ ls -1
  2201. 1.txt#foo
  2202. </pre></div>
  2203.  
  2204. </dd>
  2205. </dl>
  2206.  
  2207.  
  2208. <hr>
  2209. <a name="sed-addresses"></a>
  2210. <div class="header">
  2211. <p>
  2212. Next: <a href="#sed-regular-expressions" accesskey="n" rel="next">sed regular expressions</a>, Previous: <a href="#sed-scripts" accesskey="p" rel="prev">sed scripts</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2213. </div>
  2214. <a name="Addresses_003a-selecting-lines"></a>
  2215. <h2 class="chapter">4 Addresses: selecting lines</h2>
  2216.  
  2217. <table class="menu" border="0" cellspacing="0">
  2218. <tr><td align="left" valign="top">&bull; <a href="#Addresses-overview" accesskey="1">Addresses overview</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Addresses overview
  2219. </td></tr>
  2220. <tr><td align="left" valign="top">&bull; <a href="#Numeric-Addresses" accesskey="2">Numeric Addresses</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">selecting lines by numbers
  2221. </td></tr>
  2222. <tr><td align="left" valign="top">&bull; <a href="#Regexp-Addresses" accesskey="3">Regexp Addresses</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">selecting lines by text matching
  2223. </td></tr>
  2224. <tr><td align="left" valign="top">&bull; <a href="#Range-Addresses" accesskey="4">Range Addresses</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">selecting a range of lines
  2225. </td></tr>
  2226.  
  2227. <hr>
  2228. <a name="Addresses-overview"></a>
  2229. <div class="header">
  2230. <p>
  2231. Next: <a href="#Numeric-Addresses" accesskey="n" rel="next">Numeric Addresses</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">sed addresses</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2232. </div>
  2233. <a name="Addresses-overview-1"></a>
  2234. <h3 class="section">4.1 Addresses overview</h3>
  2235.  
  2236. <a name="index-addresses_002c-numeric"></a>
  2237. <a name="index-numeric-addresses"></a>
  2238. <p>Addresses determine on which line(s) the <code>sed</code> command will be
  2239. executed. The following command replaces the word &lsquo;<samp>hello</samp>&rsquo;
  2240. with &lsquo;<samp>world</samp>&rsquo; only on line 144:
  2241. </p>
  2242. <div class="example">
  2243. <pre class="example">sed '144s/hello/world/' input.txt &gt; output.txt
  2244. </pre></div>
  2245.  
  2246.  
  2247.  
  2248. <p>If no addresses are given, the command is performed on all lines.
  2249. The following command replaces the word &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;
  2250. on all lines in the input file:
  2251. </p>
  2252. <div class="example">
  2253. <pre class="example">sed 's/hello/world/' input.txt &gt; output.txt
  2254. </pre></div>
  2255.  
  2256.  
  2257.  
  2258. <a name="index-addresses_002c-regular-expression"></a>
  2259. <a name="index-regular-expression-addresses"></a>
  2260. <p>Addresses can contain regular expressions to match lines based
  2261. on content instead of line numbers. The following command replaces
  2262. the word &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo; only in lines
  2263. containing the word &lsquo;<samp>apple</samp>&rsquo;:
  2264. </p>
  2265. <div class="example">
  2266. <pre class="example">sed '/apple/s/hello/world/' input.txt &gt; output.txt
  2267. </pre></div>
  2268.  
  2269.  
  2270.  
  2271. <a name="index-addresses_002c-range"></a>
  2272. <a name="index-range-addresses"></a>
  2273. <p>An address range is specified with two addresses separated by a comma
  2274. (<code>,</code>). Addresses can be numeric, regular expressions, or a mix of
  2275. both.
  2276. The following command replaces the word &lsquo;<samp>hello</samp>&rsquo; with &lsquo;<samp>world</samp>&rsquo;
  2277. only in lines 4 to 17 (inclusive):
  2278. </p>
  2279. <div class="example">
  2280. <pre class="example">sed '4,17s/hello/world/' input.txt &gt; output.txt
  2281. </pre></div>
  2282.  
  2283.  
  2284.  
  2285. <a name="index-Excluding-lines"></a>
  2286. <a name="index-Selecting-non_002dmatching-lines"></a>
  2287. <a name="index-addresses_002c-negating"></a>
  2288. <a name="index-addresses_002c-excluding"></a>
  2289. <p>Appending the <code>!</code> character to the end of an address
  2290. specification (before the command letter) negates the sense of the
  2291. match.  That is, if the <code>!</code> character follows an address or an
  2292. address range, then only lines which do <em>not</em> match the addresses
  2293. will be selected. The following command replaces the word &lsquo;<samp>hello</samp>&rsquo;
  2294. with &lsquo;<samp>world</samp>&rsquo; only in lines <em>not</em> containing the word
  2295. &lsquo;<samp>apple</samp>&rsquo;:
  2296. </p>
  2297. <div class="example">
  2298. <pre class="example">sed '/apple/!s/hello/world/' input.txt &gt; output.txt
  2299. </pre></div>
  2300.  
  2301. <p>The following command replaces the word &lsquo;<samp>hello</samp>&rsquo; with
  2302. &lsquo;<samp>world</samp>&rsquo; only in lines 1 to 3 and 18 till the last line of the input file
  2303. (i.e. excluding lines 4 to 17):
  2304. </p>
  2305. <div class="example">
  2306. <pre class="example">sed '4,17!s/hello/world/' input.txt &gt; output.txt
  2307. </pre></div>
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313. <hr>
  2314. <a name="Numeric-Addresses"></a>
  2315. <div class="header">
  2316. <p>
  2317. Next: <a href="#Regexp-Addresses" accesskey="n" rel="next">Regexp Addresses</a>, Previous: <a href="#Addresses-overview" accesskey="p" rel="prev">Addresses overview</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">sed addresses</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2318. </div>
  2319. <a name="Selecting-lines-by-numbers"></a>
  2320. <h3 class="section">4.2 Selecting lines by numbers</h3>
  2321. <a name="index-Addresses_002c-in-sed-scripts"></a>
  2322. <a name="index-Line-selection"></a>
  2323. <a name="index-Selecting-lines-to-process"></a>
  2324.  
  2325. <p>Addresses in a <code>sed</code> script can be in any of the following forms:
  2326. </p><dl compact="compact">
  2327. <dt><code><var>number</var></code></dt>
  2328. <dd><a name="index-Address_002c-numeric"></a>
  2329. <a name="index-Line_002c-selecting-by-number"></a>
  2330. <p>Specifying a line number will match only that line in the input.
  2331. (Note that <code>sed</code> counts lines continuously across all input files
  2332. unless <samp>-i</samp> or <samp>-s</samp> options are specified.)
  2333. </p>
  2334. </dd>
  2335. <dt><code>$</code></dt>
  2336. <dd><a name="index-Address_002c-last-line"></a>
  2337. <a name="index-Last-line_002c-selecting"></a>
  2338. <a name="index-Line_002c-selecting-last"></a>
  2339. <p>This address matches the last line of the last file of input, or
  2340. the last line of each file when the <samp>-i</samp> or <samp>-s</samp> options
  2341. are specified.
  2342. </p>
  2343.  
  2344. </dd>
  2345. <dt><code><var>first</var>~<var>step</var></code></dt>
  2346. <dd><a name="index-GNU-extensions_002c-n_007em-addresses"></a>
  2347. <p>This GNU extension matches every <var>step</var>th line
  2348. starting with line <var>first</var>.
  2349. In particular, lines will be selected when there exists
  2350. a non-negative <var>n</var> such that the current line-number equals
  2351. <var>first</var> + (<var>n</var> * <var>step</var>).
  2352. Thus, one would use <code>1~2</code> to select the odd-numbered lines and
  2353. <code>0~2</code> for even-numbered lines;
  2354. to pick every third line starting with the second, &lsquo;<samp>2~3</samp>&rsquo; would be used;
  2355. to pick every fifth line starting with the tenth, use &lsquo;<samp>10~5</samp>&rsquo;;
  2356. and &lsquo;<samp>50~0</samp>&rsquo; is just an obscure way of saying <code>50</code>.
  2357. </p>
  2358. <p>The following commands demonstrate the step address usage:
  2359. </p>
  2360. <div class="example">
  2361. <pre class="example">$ seq 10 | sed -n '0~4p'
  2362. 4
  2363. 8
  2364.  
  2365. $ seq 10 | sed -n '1~3p'
  2366. 1
  2367. 4
  2368. 7
  2369. 10
  2370. </pre></div>
  2371.  
  2372.  
  2373. </dd>
  2374. </dl>
  2375.  
  2376.  
  2377.  
  2378. <hr>
  2379. <a name="Regexp-Addresses"></a>
  2380. <div class="header">
  2381. <p>
  2382. Next: <a href="#Range-Addresses" accesskey="n" rel="next">Range Addresses</a>, Previous: <a href="#Numeric-Addresses" accesskey="p" rel="prev">Numeric Addresses</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">sed addresses</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2383. </div>
  2384. <a name="selecting-lines-by-text-matching"></a>
  2385. <h3 class="section">4.3 selecting lines by text matching</h3>
  2386.  
  2387. <p>GNU <code>sed</code> supports the following regular expression addresses.
  2388. The default regular expression is
  2389. <a href="#BRE-syntax">Basic Regular Expression (BRE)</a>.
  2390. If <samp>-E</samp> or <samp>-r</samp> options are used, The regular expression should be
  2391. in <a href="#ERE-syntax">Extended Regular Expression (ERE)</a> syntax.
  2392. See <a href="#BRE-vs-ERE">BRE vs ERE</a>.
  2393. </p>
  2394. <dl compact="compact">
  2395. <dt><code>/<var>regexp</var>/</code></dt>
  2396. <dd><a name="index-Address_002c-as-a-regular-expression"></a>
  2397. <a name="index-Line_002c-selecting-by-regular-expression-match"></a>
  2398. <p>This will select any line which matches the regular expression <var>regexp</var>.
  2399. If <var>regexp</var> itself includes any <code>/</code> characters,
  2400. each must be escaped by a backslash (<code>\</code>).
  2401. </p>
  2402. <p>The following command prints lines in <samp>/etc/passwd</samp>
  2403. which end with &lsquo;<samp>bash</samp>&rsquo;<a name="DOCF5" href="#FOOT5"><sup>5</sup></a>:
  2404. </p>
  2405. <div class="example">
  2406. <pre class="example">sed -n '/bash$/p' /etc/passwd
  2407. </pre></div>
  2408.  
  2409. <a name="index-empty-regular-expression"></a>
  2410. <a name="index-GNU-extensions_002c-modifiers-and-the-empty-regular-expression"></a>
  2411. <p>The empty regular expression &lsquo;<samp>//</samp>&rsquo; repeats the last regular
  2412. expression match (the same holds if the empty regular expression is
  2413. passed to the <code>s</code> command).  Note that modifiers to regular expressions
  2414. are evaluated when the regular expression is compiled, thus it is invalid to
  2415. specify them together with the empty regular expression.
  2416. </p>
  2417. </dd>
  2418. <dt><code>\%<var>regexp</var>%</code></dt>
  2419. <dd><p>(The <code>%</code> may be replaced by any other single character.)
  2420. </p>
  2421. <a name="index-Slash-character_002c-in-regular-expressions"></a>
  2422. <p>This also matches the regular expression <var>regexp</var>,
  2423. but allows one to use a different delimiter than <code>/</code>.
  2424. This is particularly useful if the <var>regexp</var> itself contains
  2425. a lot of slashes, since it avoids the tedious escaping of every <code>/</code>.
  2426. If <var>regexp</var> itself includes any delimiter characters,
  2427. each must be escaped by a backslash (<code>\</code>).
  2428. </p>
  2429. <p>The following commands are equivalent. They print lines
  2430. which start with &lsquo;<samp>/home/alice/documents/</samp>&rsquo;:
  2431. </p>
  2432. <div class="example">
  2433. <pre class="example">sed -n '/^\/home\/alice\/documents\//p'
  2434. sed -n '\%^/home/alice/documents/%p'
  2435. sed -n '\;^/home/alice/documents/;p'
  2436. </pre></div>
  2437.  
  2438.  
  2439. </dd>
  2440. <dt><code>/<var>regexp</var>/I</code></dt>
  2441. <dt><code>\%<var>regexp</var>%I</code></dt>
  2442. <dd><a name="index-GNU-extensions_002c-I-modifier-1"></a>
  2443. <a name="index-case-insensitive_002c-regular-expression"></a>
  2444. <p>The <code>I</code> modifier to regular-expression matching is a GNU
  2445. extension which causes the <var>regexp</var> to be matched in
  2446. a case-insensitive manner.
  2447. </p>
  2448. <p>In many other programming languages, a lower case <code>i</code> is used
  2449. for case-insensitive regular expression matching. However, in <code>sed</code>
  2450. the <code>i</code> is used for the insert command (see <a href="#insert-command">insert command</a>).
  2451. </p>
  2452. <p>Observe the difference between the following examples.
  2453. </p>
  2454. <p>In this example, <code>/b/I</code> is the address: regular expression with <code>I</code>
  2455. modifier. <code>d</code> is the delete command:
  2456. </p>
  2457. <div class="example">
  2458. <pre class="example">$ printf &quot;%s\n&quot; a b c | sed '/b/Id'
  2459. a
  2460. c
  2461. </pre></div>
  2462.  
  2463. <p>Here, <code>/b/</code> is the address: a regular expression.
  2464. <code>i</code> is the insert command.
  2465. <code>d</code> is the value to insert.
  2466. A line with &lsquo;<samp>d</samp>&rsquo; is then inserted above the matched line:
  2467. </p>
  2468. <div class="example">
  2469. <pre class="example">$ printf &quot;%s\n&quot; a b c | sed '/b/id'
  2470. a
  2471. d
  2472. b
  2473. c
  2474. </pre></div>
  2475.  
  2476. </dd>
  2477. <dt><code>/<var>regexp</var>/M</code></dt>
  2478. <dt><code>\%<var>regexp</var>%M</code></dt>
  2479. <dd><a name="index-GNU-extensions_002c-M-modifier-1"></a>
  2480. <p>The <code>M</code> modifier to regular-expression matching is a GNU <code>sed</code>
  2481. extension which directs GNU <code>sed</code> to match the regular expression
  2482. in <cite>multi-line</cite> mode.  The modifier causes <code>^</code> and <code>$</code> to
  2483. match respectively (in addition to the normal behavior) the empty string
  2484. after a newline, and the empty string before a newline.  There are
  2485. special character sequences
  2486. (<code>\`</code> and <code>\'</code>)
  2487. which always match the beginning or the end of the buffer.
  2488. In addition,
  2489. the period character does not match a new-line character in
  2490. multi-line mode.
  2491. </p></dd>
  2492. </dl>
  2493.  
  2494.  
  2495. <a name="index-regex-addresses-and-pattern-space"></a>
  2496. <a name="index-regex-addresses-and-input-lines"></a>
  2497. <p>Regex addresses operate on the content of the current
  2498. pattern space. If the pattern space is changed (for example with <code>s///</code>
  2499. command) the regular expression matching will operate on the changed text.
  2500. </p>
  2501. <p>In the following example, automatic printing is disabled with
  2502. <samp>-n</samp>.  The <code>s/2/X/</code> command changes lines containing
  2503. &lsquo;<samp>2</samp>&rsquo; to &lsquo;<samp>X</samp>&rsquo;. The command <code>/[0-9]/p</code> matches
  2504. lines with digits and prints them.
  2505. Because the second line is changed before the <code>/[0-9]/</code> regex,
  2506. it will not match and will not be printed:
  2507. </p>
  2508. <div class="example">
  2509. <pre class="example">$ seq 3 | sed -n 's/2/X/ ; /[0-9]/p'
  2510. 1
  2511. 3
  2512. </pre></div>
  2513.  
  2514.  
  2515. <hr>
  2516. <a name="Range-Addresses"></a>
  2517. <div class="header">
  2518. <p>
  2519. Previous: <a href="#Regexp-Addresses" accesskey="p" rel="prev">Regexp Addresses</a>, Up: <a href="#sed-addresses" accesskey="u" rel="up">sed addresses</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2520. </div>
  2521. <a name="Range-Addresses-1"></a>
  2522. <h3 class="section">4.4 Range Addresses</h3>
  2523.  
  2524. <a name="index-Range-of-lines"></a>
  2525. <a name="index-Several-lines_002c-selecting"></a>
  2526. <p>An address range can be specified by specifying two addresses
  2527. separated by a comma (<code>,</code>).  An address range matches lines
  2528. starting from where the first address matches, and continues
  2529. until the second address matches (inclusively):
  2530. </p>
  2531. <div class="example">
  2532. <pre class="example">$ seq 10 | sed -n '4,6p'
  2533. 4
  2534. 5
  2535. 6
  2536. </pre></div>
  2537.  
  2538. <p>If the second address is a <var>regexp</var>, then checking for the
  2539. ending match will start with the line <em>following</em> the
  2540. line which matched the first address: a range will always
  2541. span at least two lines (except of course if the input stream
  2542. ends).
  2543. </p>
  2544. <div class="example">
  2545. <pre class="example">$ seq 10 | sed -n '4,/[0-9]/p'
  2546. 4
  2547. 5
  2548. </pre></div>
  2549.  
  2550. <p>If the second address is a <var>number</var> less than (or equal to)
  2551. the line matching the first address, then only the one line is
  2552. matched:
  2553. </p>
  2554. <div class="example">
  2555. <pre class="example">$ seq 10 | sed -n '4,1p'
  2556. 4
  2557. </pre></div>
  2558.  
  2559. <a name="index-Special-addressing-forms"></a>
  2560. <a name="index-Range-with-start-address-of-zero"></a>
  2561. <a name="index-Zero_002c-as-range-start-address"></a>
  2562. <a name="index-addr1_002c_002bN"></a>
  2563. <a name="index-addr1_002c_007eN"></a>
  2564. <a name="index-GNU-extensions_002c-special-two_002daddress-forms"></a>
  2565. <a name="index-GNU-extensions_002c-0-address"></a>
  2566. <a name="index-GNU-extensions_002c-0_002caddr2-addressing"></a>
  2567. <a name="index-GNU-extensions_002c-addr1_002c_002bN-addressing"></a>
  2568. <a name="index-GNU-extensions_002c-addr1_002c_007eN-addressing"></a>
  2569. <p>GNU <code>sed</code> also supports some special two-address forms; all these
  2570. are GNU extensions:
  2571. </p><dl compact="compact">
  2572. <dt><code>0,/<var>regexp</var>/</code></dt>
  2573. <dd><p>A line number of <code>0</code> can be used in an address specification like
  2574. <code>0,/<var>regexp</var>/</code> so that <code>sed</code> will try to match
  2575. <var>regexp</var> in the first input line too.  In other words,
  2576. <code>0,/<var>regexp</var>/</code> is similar to <code>1,/<var>regexp</var>/</code>,
  2577. except that if <var>addr2</var> matches the very first line of input the
  2578. <code>0,/<var>regexp</var>/</code> form will consider it to end the range, whereas
  2579. the <code>1,/<var>regexp</var>/</code> form will match the beginning of its range and
  2580. hence make the range span up to the <em>second</em> occurrence of the
  2581. regular expression.
  2582. </p>
  2583. <p>Note that this is the only place where the <code>0</code> address makes
  2584. sense; there is no 0-th line and commands which are given the <code>0</code>
  2585. address in any other way will give an error.
  2586. </p>
  2587. <p>The following examples demonstrate the difference between starting
  2588. with address 1 and 0:
  2589. </p>
  2590. <div class="example">
  2591. <pre class="example">$ seq 10 | sed -n '1,/[0-9]/p'
  2592. 1
  2593. 2
  2594.  
  2595. $ seq 10 | sed -n '0,/[0-9]/p'
  2596. 1
  2597. </pre></div>
  2598.  
  2599.  
  2600. </dd>
  2601. <dt><code><var>addr1</var>,+<var>N</var></code></dt>
  2602. <dd><p>Matches <var>addr1</var> and the <var>N</var> lines following <var>addr1</var>.
  2603. </p>
  2604. <div class="example">
  2605. <pre class="example">$ seq 10 | sed -n '6,+2p'
  2606. 6
  2607. 7
  2608. 8
  2609. </pre></div>
  2610.  
  2611. <p><var>addr1</var> can be a line number or a regular expression.
  2612. </p>
  2613. </dd>
  2614. <dt><code><var>addr1</var>,~<var>N</var></code></dt>
  2615. <dd><p>Matches <var>addr1</var> and the lines following <var>addr1</var>
  2616. until the next line whose input line number is a multiple of <var>N</var>.
  2617. The following command prints starting at line 6, until the next line which
  2618. is a multiple of 4 (i.e. line 8):
  2619. </p>
  2620. <div class="example">
  2621. <pre class="example">$ seq 10 | sed -n '6,~4p'
  2622. 6
  2623. 7
  2624. 8
  2625. </pre></div>
  2626.  
  2627. <p><var>addr1</var> can be a line number or a regular expression.
  2628. </p>
  2629. </dd>
  2630. </dl>
  2631.  
  2632.  
  2633.  
  2634.  
  2635. <hr>
  2636. <a name="sed-regular-expressions"></a>
  2637. <div class="header">
  2638. <p>
  2639. Next: <a href="#advanced-sed" accesskey="n" rel="next">advanced sed</a>, Previous: <a href="#sed-addresses" accesskey="p" rel="prev">sed addresses</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2640. </div>
  2641. <a name="Regular-Expressions_003a-selecting-text"></a>
  2642. <h2 class="chapter">5 Regular Expressions: selecting text</h2>
  2643.  
  2644. <table class="menu" border="0" cellspacing="0">
  2645. <tr><td align="left" valign="top">&bull; <a href="#Regular-Expressions-Overview" accesskey="1">Regular Expressions Overview</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Overview of Regular expression in <code>sed</code>
  2646. </td></tr>
  2647. <tr><td align="left" valign="top">&bull; <a href="#BRE-vs-ERE" accesskey="2">BRE vs ERE</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Basic (BRE) and extended (ERE) regular expression
  2648.                              syntax
  2649. </td></tr>
  2650. <tr><td align="left" valign="top">&bull; <a href="#BRE-syntax" accesskey="3">BRE syntax</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Overview of basic regular expression syntax
  2651. </td></tr>
  2652. <tr><td align="left" valign="top">&bull; <a href="#ERE-syntax" accesskey="4">ERE syntax</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Overview of extended regular expression syntax
  2653. </td></tr>
  2654. <tr><td align="left" valign="top">&bull; <a href="#Character-Classes-and-Bracket-Expressions" accesskey="5">Character Classes and Bracket Expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  2655. </td></tr>
  2656. <tr><td align="left" valign="top">&bull; <a href="#regexp-extensions" accesskey="6">regexp extensions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Additional regular expression commands
  2657. </td></tr>
  2658. <tr><td align="left" valign="top">&bull; <a href="#Back_002dreferences-and-Subexpressions" accesskey="7">Back-references and Subexpressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Back-references and Subexpressions
  2659. </td></tr>
  2660. <tr><td align="left" valign="top">&bull; <a href="#Escapes" accesskey="8">Escapes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Specifying special characters
  2661. </td></tr>
  2662. <tr><td align="left" valign="top">&bull; <a href="#Locale-Considerations" accesskey="9">Locale Considerations</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Multibyte characters and locale considrations
  2663. </td></tr>
  2664.  
  2665. <hr>
  2666. <a name="Regular-Expressions-Overview"></a>
  2667. <div class="header">
  2668. <p>
  2669. Next: <a href="#BRE-vs-ERE" accesskey="n" rel="next">BRE vs ERE</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">sed regular expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2670. </div>
  2671. <a name="Overview-of-regular-expression-in-sed"></a>
  2672. <h3 class="section">5.1 Overview of regular expression in <code>sed</code></h3>
  2673.  
  2674.  
  2675.  
  2676. <p>To know how to use <code>sed</code>, people should understand regular
  2677. expressions (<em>regexp</em> for short).  A regular expression
  2678. is a pattern that is matched against a
  2679. subject string from left to right.  Most characters are
  2680. <em>ordinary</em>: they stand for
  2681. themselves in a pattern, and match the corresponding characters.
  2682. Regular expressions in <code>sed</code> are specified between two
  2683. slashes.
  2684. </p>
  2685. <p>The following command prints lines containing the word
  2686. &lsquo;<samp>hello</samp>&rsquo;:
  2687. </p>
  2688. <div class="example">
  2689. <pre class="example">sed -n '/hello/p'
  2690. </pre></div>
  2691.  
  2692. <p>The above example is equivalent to this <code>grep</code> command:
  2693. </p>
  2694. <div class="example">
  2695. <pre class="example">grep 'hello'
  2696. </pre></div>
  2697.  
  2698. <p>The power of regular expressions comes from the ability to include
  2699. alternatives and repetitions in the pattern.  These are encoded in the
  2700. pattern by the use of <em>special characters</em>, which do not stand for
  2701. themselves but instead are interpreted in some special way.
  2702. </p>
  2703. <p>The character <code>^</code> (caret) in a regular expression matches the
  2704. beginning of the line. The character <code>.</code> (dot) matches any single
  2705. character. The following <code>sed</code> command matches and prints
  2706. lines which start with the letter &lsquo;<samp>b</samp>&rsquo;, followed by any single character,
  2707. followed by the letter &lsquo;<samp>d</samp>&rsquo;:
  2708. </p>
  2709. <div class="example">
  2710. <pre class="example">$ printf &quot;%s\n&quot; abode bad bed bit bid byte body | sed -n '/^b.d/p'
  2711. bad
  2712. bed
  2713. bid
  2714. body
  2715. </pre></div>
  2716.  
  2717. <p>The following sections explain the meaning and usage of special
  2718. characters in regular expressions.
  2719. </p>
  2720. <hr>
  2721. <a name="BRE-vs-ERE"></a>
  2722. <div class="header">
  2723. <p>
  2724. Next: <a href="#BRE-syntax" accesskey="n" rel="next">BRE syntax</a>, Previous: <a href="#Regular-Expressions-Overview" accesskey="p" rel="prev">Regular Expressions Overview</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">sed regular expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2725. </div>
  2726. <a name="Basic-_0028BRE_0029-and-extended-_0028ERE_0029-regular-expression"></a>
  2727. <h3 class="section">5.2 Basic (BRE) and extended (ERE) regular expression</h3>
  2728.  
  2729. <p>Basic and extended regular expressions are two variations on the
  2730. syntax of the specified pattern. Basic Regular Expression (BRE) syntax is the
  2731. default in <code>sed</code> (and similarly in <code>grep</code>).
  2732. Use the POSIX-specified <samp>-E</samp> option (<samp>-r</samp>,
  2733. <samp>--regexp-extended</samp>) to enable Extended Regular Expression (ERE) syntax.
  2734. </p>
  2735. <p>In GNU <code>sed</code>, the only difference between basic and extended regular
  2736. expressions is in the behavior of a few special characters: &lsquo;<samp>?</samp>&rsquo;,
  2737. &lsquo;<samp>+</samp>&rsquo;, parentheses, braces (&lsquo;<samp>{}</samp>&rsquo;), and &lsquo;<samp>|</samp>&rsquo;.
  2738. </p>
  2739. <p>With basic (BRE) syntax, these characters do not have special meaning
  2740. unless prefixed with a backslash (&lsquo;<samp>\</samp>&rsquo;); While with extended (ERE) syntax
  2741. it is reversed: these characters are special unless they are prefixed
  2742. with backslash (&lsquo;<samp>\</samp>&rsquo;).
  2743. </p>
  2744. <thead><tr><th width="28%">Desired pattern</th><th width="36%">Basic (BRE) Syntax</th><th width="35%">Extended (ERE) Syntax</th></tr></thead>
  2745. <tr><td width="28%">literal &lsquo;<samp>+</samp>&rsquo; (plus sign)</td><td width="36%"><div class="example">
  2746. <pre class="example">$ echo 'a+b=c' &gt; foo
  2747. $ sed -n '/a+b/p' foo
  2748. a+b=c
  2749. </pre></div></td><td width="35%"><div class="example">
  2750. <pre class="example">$ echo 'a+b=c' &gt; foo
  2751. $ sed -E -n '/a\+b/p' foo
  2752. a+b=c
  2753. </pre></div></td></tr>
  2754. <tr><td width="28%">One or more &lsquo;<samp>a</samp>&rsquo; characters followed by &lsquo;<samp>b</samp>&rsquo;
  2755. (plus sign as special meta-character)</td><td width="36%"><div class="example">
  2756. <pre class="example">$ echo aab &gt; foo
  2757. $ sed -n '/a\+b/p' foo
  2758. aab
  2759. </pre></div></td><td width="35%"><div class="example">
  2760. <pre class="example">$ echo aab &gt; foo
  2761. $ sed -E -n '/a+b/p' foo
  2762. aab
  2763. </pre></div></td></tr>
  2764.  
  2765.  
  2766.  
  2767.  
  2768. <hr>
  2769. <a name="BRE-syntax"></a>
  2770. <div class="header">
  2771. <p>
  2772. Next: <a href="#ERE-syntax" accesskey="n" rel="next">ERE syntax</a>, Previous: <a href="#BRE-vs-ERE" accesskey="p" rel="prev">BRE vs ERE</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">sed regular expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  2773. </div>
  2774. <a name="Overview-of-basic-regular-expression-syntax"></a>
  2775. <h3 class="section">5.3 Overview of basic regular expression syntax</h3>
  2776.  
  2777. <p>Here is a brief description
  2778. of regular expression syntax as used in <code>sed</code>.
  2779. </p>
  2780. <dl compact="compact">
  2781. <dt><code><var>char</var></code></dt>
  2782. <dd><p>A single ordinary character matches itself.
  2783. </p>
  2784. </dd>
  2785. <dt><code>*</code></dt>
  2786. <dd><a name="index-GNU-extensions_002c-to-basic-regular-expressions"></a>
  2787. <p>Matches a sequence of zero or more instances of matches for the
  2788. preceding regular expression, which must be an ordinary character, a
  2789. special character preceded by <code>\</code>, a <code>.</code>, a grouped regexp
  2790. (see below), or a bracket expression.  As a GNU extension, a
  2791. postfixed regular expression can also be followed by <code>*</code>; for
  2792. example, <code>a**</code> is equivalent to <code>a*</code>.  POSIX
  2793. 1003.1-2001 says that <code>*</code> stands for itself when it appears at
  2794. the start of a regular expression or subexpression, but many
  2795. nonGNU implementations do not support this and portable
  2796. scripts should instead use <code>\*</code> in these contexts.
  2797. </p></dd>
  2798. <dt><code>.</code></dt>
  2799. <dd><p>Matches any character, including newline.
  2800. </p>
  2801. </dd>
  2802. <dt><code>^</code></dt>
  2803. <dd><p>Matches the null string at beginning of the pattern space, i.e. what
  2804. appears after the circumflex must appear at the beginning of the
  2805. pattern space.
  2806. </p>
  2807. <p>In most scripts, pattern space is initialized to the content of each
  2808. line (see <a href="#Execution-Cycle">How <code>sed</code> works</a>).  So, it is a
  2809. useful simplification to think of <code>^#include</code> as matching only
  2810. lines where &lsquo;<samp>#include</samp>&rsquo; is the first thing on line&mdash;if there are
  2811. spaces before, for example, the match fails.  This simplification is
  2812. valid as long as the original content of pattern space is not modified,
  2813. for example with an <code>s</code> command.
  2814. </p>
  2815. <p><code>^</code> acts as a special character only at the beginning of the
  2816. regular expression or subexpression (that is, after <code>\(</code> or
  2817. <code>\|</code>).  Portable scripts should avoid <code>^</code> at the beginning of
  2818. a subexpression, though, as POSIX allows implementations that
  2819. treat <code>^</code> as an ordinary character in that context.
  2820. </p>
  2821. </dd>
  2822. <dt><code>$</code></dt>
  2823. <dd><p>It is the same as <code>^</code>, but refers to end of pattern space.
  2824. <code>$</code> also acts as a special character only at the end
  2825. of the regular expression or subexpression (that is, before <code>\)</code>
  2826. or <code>\|</code>), and its use at the end of a subexpression is not
  2827. portable.
  2828. </p>
  2829.  
  2830. </dd>
  2831. <dt><code>[<var>list</var>]</code></dt>
  2832. <dt><code>[^<var>list</var>]</code></dt>
  2833. <dd><p>Matches any single character in <var>list</var>: for example,
  2834. <code>[aeiou]</code> matches all vowels.  A list may include
  2835. sequences like <code><var>char1</var>-<var>char2</var></code>, which
  2836. matches any character between (inclusive) <var>char1</var>
  2837. and <var>char2</var>.
  2838. See <a href="#Character-Classes-and-Bracket-Expressions">Character Classes and Bracket Expressions</a>.
  2839. </p>
  2840. </dd>
  2841. <dt><code>\+</code></dt>
  2842. <dd><a name="index-GNU-extensions_002c-to-basic-regular-expressions-1"></a>
  2843. <p>As <code>*</code>, but matches one or more.  It is a GNU extension.
  2844. </p>
  2845. </dd>
  2846. <dt><code>\?</code></dt>
  2847. <dd><a name="index-GNU-extensions_002c-to-basic-regular-expressions-2"></a>
  2848. <p>As <code>*</code>, but only matches zero or one.  It is a GNU extension.
  2849. </p>
  2850. </dd>
  2851. <dt><code>\{<var>i</var>\}</code></dt>
  2852. <dd><p>As <code>*</code>, but matches exactly <var>i</var> sequences (<var>i</var> is a
  2853. decimal integer; for portability, keep it between 0 and 255
  2854. inclusive).
  2855. </p>
  2856. </dd>
  2857. <dt><code>\{<var>i</var>,<var>j</var>\}</code></dt>
  2858. <dd><p>Matches between <var>i</var> and <var>j</var>, inclusive, sequences.
  2859. </p>
  2860. </dd>
  2861. <dt><code>\{<var>i</var>,\}</code></dt>
  2862. <dd><p>Matches more than or equal to <var>i</var> sequences.
  2863. </p>
  2864. </dd>
  2865. <dt><code>\(<var>regexp</var>\)</code></dt>
  2866. <dd><p>Groups the inner <var>regexp</var> as a whole, this is used to:
  2867. </p>
  2868. <ul>
  2869. <li> <a name="index-GNU-extensions_002c-to-basic-regular-expressions-3"></a>
  2870. Apply postfix operators, like <code>\(abcd\)*</code>:
  2871. this will search for zero or more whole sequences
  2872. of &lsquo;<samp>abcd</samp>&rsquo;, while <code>abcd*</code> would search
  2873. for &lsquo;<samp>abc</samp>&rsquo; followed by zero or more occurrences
  2874. of &lsquo;<samp>d</samp>&rsquo;.  Note that support for <code>\(abcd\)*</code> is
  2875. required by POSIX 1003.1-2001, but many non-GNU
  2876. implementations do not support it and hence it is not universally
  2877. portable.
  2878.  
  2879. </li><li> Use back references (see below).
  2880. </li></ul>
  2881.  
  2882.  
  2883. </dd>
  2884. <dt><code><var>regexp1</var>\|<var>regexp2</var></code></dt>
  2885. <dd><a name="index-GNU-extensions_002c-to-basic-regular-expressions-4"></a>
  2886. <p>Matches either <var>regexp1</var> or <var>regexp2</var>.  Use
  2887. parentheses to use complex alternative regular expressions.
  2888. The matching process tries each alternative in turn, from
  2889. left to right, and the first one that succeeds is used.
  2890. It is a GNU extension.
  2891. </p>
  2892. </dd>
  2893. <dt><code><var>regexp1</var><var>regexp2</var></code></dt>
  2894. <dd><p>Matches the concatenation of <var>regexp1</var> and <var>regexp2</var>.
  2895. Concatenation binds more tightly than <code>\|</code>, <code>^</code>, and
  2896. <code>$</code>, but less tightly than the other regular expression
  2897. operators.
  2898. </p>
  2899. </dd>
  2900. <dt><code>\<var>digit</var></code></dt>
  2901. <dd><p>Matches the <var>digit</var>-th <code>\(&hellip;\)</code> parenthesized
  2902. subexpression in the regular expression.  This is called a <em>back
  2903. reference</em>.  Subexpressions are implicitly numbered by counting
  2904. occurrences of <code>\(</code> left-to-right.
  2905. </p>
  2906. </dd>
  2907. <dt><code>\n</code></dt>
  2908. <dd><p>Matches the newline character.
  2909. </p>
  2910. </dd>
  2911. <dt><code>\<var>char</var></code></dt>
  2912. <dd><p>Matches <var>char</var>, where <var>char</var> is one of <code>$</code>,
  2913. <code>*</code>, <code>.</code>, <code>[</code>, <code>\</code>, or <code>^</code>.
  2914. Note that the only C-like
  2915. backslash sequences that you can portably assume to be
  2916. interpreted are <code>\n</code> and <code>\\</code>; in particular
  2917. <code>\t</code> is not portable, and matches a &lsquo;<samp>t</samp>&rsquo; under most
  2918. implementations of <code>sed</code>, rather than a tab character.
  2919. </p>
  2920. </dd>
  2921. </dl>
  2922.  
  2923. <a name="index-Greedy-regular-expression-matching"></a>
  2924. <p>Note that the regular expression matcher is greedy, i.e., matches
  2925. are attempted from left to right and, if two or more matches are
  2926. possible starting at the same character, it selects the longest.
  2927. </p>
  2928. <p>Examples:
  2929. </p><dl compact="compact">
  2930. <dt>&lsquo;<samp>abcdef</samp>&rsquo;</dt>
  2931. <dd><p>Matches &lsquo;<samp>abcdef</samp>&rsquo;.
  2932. </p>
  2933. </dd>
  2934. <dt>&lsquo;<samp>a*b</samp>&rsquo;</dt>
  2935. <dd><p>Matches zero or more &lsquo;<samp>a</samp>&rsquo;s followed by a single
  2936. &lsquo;<samp>b</samp>&rsquo;.  For example, &lsquo;<samp>b</samp>&rsquo; or &lsquo;<samp>aaaaab</samp>&rsquo;.
  2937. </p>
  2938. </dd>
  2939. <dt>&lsquo;<samp>a\?b</samp>&rsquo;</dt>
  2940. <dd><p>Matches &lsquo;<samp>b</samp>&rsquo; or &lsquo;<samp>ab</samp>&rsquo;.
  2941. </p>
  2942. </dd>
  2943. <dt>&lsquo;<samp>a\+b\+</samp>&rsquo;</dt>
  2944. <dd><p>Matches one or more &lsquo;<samp>a</samp>&rsquo;s followed by one or more
  2945. &lsquo;<samp>b</samp>&rsquo;s: &lsquo;<samp>ab</samp>&rsquo; is the shortest possible match, but
  2946. other examples are &lsquo;<samp>aaaab</samp>&rsquo; or &lsquo;<samp>abbbbb</samp>&rsquo; or
  2947. &lsquo;<samp>aaaaaabbbbbbb</samp>&rsquo;.
  2948. </p>
  2949. </dd>
  2950. <dt>&lsquo;<samp>.*</samp>&rsquo;</dt>
  2951. <dt>&lsquo;<samp>.\+</samp>&rsquo;</dt>
  2952. <dd><p>These two both match all the characters in a string;
  2953. however, the first matches every string (including the empty
  2954. string), while the second matches only strings containing
  2955. at least one character.
  2956. </p>
  2957. </dd>
  2958. <dt>&lsquo;<samp>^main.*(.*)</samp>&rsquo;</dt>
  2959. <dd><p>This matches a string starting with &lsquo;<samp>main</samp>&rsquo;,
  2960. followed by an opening and closing
  2961. parenthesis.  The &lsquo;<samp>n</samp>&rsquo;, &lsquo;<samp>(</samp>&rsquo; and &lsquo;<samp>)</samp>&rsquo; need not
  2962. be adjacent.
  2963. </p>
  2964. </dd>
  2965. <dt>&lsquo;<samp>^#</samp>&rsquo;</dt>
  2966. <dd><p>This matches a string beginning with &lsquo;<samp>#</samp>&rsquo;.
  2967. </p>
  2968. </dd>
  2969. <dt>&lsquo;<samp>\\$</samp>&rsquo;</dt>
  2970. <dd><p>This matches a string ending with a single backslash.  The
  2971. regexp contains two backslashes for escaping.
  2972. </p>
  2973. </dd>
  2974. <dt>&lsquo;<samp>\$</samp>&rsquo;</dt>
  2975. <dd><p>Instead, this matches a string consisting of a single dollar sign,
  2976. because it is escaped.
  2977. </p>
  2978. </dd>
  2979. <dt>&lsquo;<samp>[a-zA-Z0-9]</samp>&rsquo;</dt>
  2980. <dd><p>In the C locale, this matches any ASCII letters or digits.
  2981. </p>
  2982. </dd>
  2983. <dt>&lsquo;<samp>[^ <kbd><span class="key">TAB</span></kbd>]\+</samp>&rsquo;</dt>
  2984. <dd><p>(Here <kbd><span class="key">TAB</span></kbd> stands for a single tab character.)
  2985. This matches a string of one or more
  2986. characters, none of which is a space or a tab.
  2987. Usually this means a word.
  2988. </p>
  2989. </dd>
  2990. <dt>&lsquo;<samp>^\(.*\)\n\1$</samp>&rsquo;</dt>
  2991. <dd><p>This matches a string consisting of two equal substrings separated by
  2992. a newline.
  2993. </p>
  2994. </dd>
  2995. <dt>&lsquo;<samp>.\{9\}A$</samp>&rsquo;</dt>
  2996. <dd><p>This matches nine characters followed by an &lsquo;<samp>A</samp>&rsquo; at the end of a line.
  2997. </p>
  2998. </dd>
  2999. <dt>&lsquo;<samp>^.\{15\}A</samp>&rsquo;</dt>
  3000. <dd><p>This matches the start of a string that contains 16 characters,
  3001. the last of which is an &lsquo;<samp>A</samp>&rsquo;.
  3002. </p>
  3003. </dd>
  3004. </dl>
  3005.  
  3006.  
  3007. <hr>
  3008. <a name="ERE-syntax"></a>
  3009. <div class="header">
  3010. <p>
  3011. 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">BRE syntax</a>, Up: <a href="#sed-regular-expressions" accesskey="u" rel="up">sed regular expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Concept-Index" title="Index" rel="index">Index</a>]</p>
  3012. </div>
  3013. <a name=