home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue4 / SDL / gcc346 / !gcc / docs / sed < prev   
Encoding:
Text File  |  2006-09-17  |  156.5 KB  |  2,666 lines

  1. <html lang="en">
  2. <head>
  3. <title>sed, a stream editor</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="sed, a stream editor">
  6. <meta name="generator" content="makeinfo 4.8">
  7. <link title="Top" rel="top" href="#Top">
  8. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  9. <!--
  10. This file documents version 4.0.9 of
  11. GNU `sed', a stream editor.
  12.  
  13. Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004 Free
  14. Software Foundation, Inc.
  15.  
  16. This document is released under the terms of the GNU Free Documentation
  17. License as published by the Free Software Foundation; either version 1.1, or
  18. (at your option) any later version.
  19.  
  20. You should have received a copy of the GNU Free Documentation License along
  21. with GNU `sed'; see the file `COPYING.DOC'.  If not, write to the Free
  22. Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  
  24. There are no Cover Texts and no Invariant Sections; this text, along
  25. with its equivalent in the printed manual, constitutes the Title Page.-->
  26. <meta http-equiv="Content-Style-Type" content="text/css">
  27. <style type="text/css"><!--
  28.   pre.display { font-family:inherit }
  29.   pre.format  { font-family:inherit }
  30.   pre.smalldisplay { font-family:inherit; font-size:smaller }
  31.   pre.smallformat  { font-family:inherit; font-size:smaller }
  32.   pre.smallexample { font-size:smaller }
  33.   pre.smalllisp    { font-size:smaller }
  34.   span.sc    { font-variant:small-caps }
  35.   span.roman { font-family:serif; font-weight:normal; } 
  36.   span.sansserif { font-family:sans-serif; font-weight:normal; } 
  37. --></style>
  38. </head>
  39. <body>
  40. <h1 class="settitle">sed, a stream editor</h1>
  41. <div class="node">
  42. <p><hr>
  43. <a name="Top"></a>
  44. Next: <a rel="next" accesskey="n" href="#Introduction">Introduction</a>,
  45. Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
  46.  
  47. </div>
  48.  
  49. <h2 class="unnumbered">sed, a stream editor</h2>
  50.  
  51. <p>This file documents version 4.0.9 of
  52. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>, a stream editor.
  53.  
  54.    <p>Copyright © 1998, 1999, 2001, 2002, 2003, 2004 Free
  55. Software Foundation, Inc.
  56.  
  57.    <p>This document is released under the terms of the GNU Free Documentation
  58. License as published by the Free Software Foundation; either version 1.1, or
  59. (at your option) any later version.
  60.  
  61.    <p>You should have received a copy of the GNU Free Documentation License along
  62. with <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>; see the file <samp><span class="file">COPYING.DOC</span></samp>.  If not, write to the Free
  63. Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  64.  
  65.    <p>There are no Cover Texts and no Invariant Sections; this text, along
  66. with its equivalent in the printed manual, constitutes the Title Page.
  67.  
  68. <ul class="menu">
  69. <li><a accesskey="1" href="#Introduction">Introduction</a>:                Introduction
  70. <li><a accesskey="2" href="#Invoking-sed">Invoking sed</a>:                Invocation
  71. <li><a accesskey="3" href="#sed-Programs">sed Programs</a>:                <samp><span class="command">sed</span></samp> programs
  72. <li><a accesskey="4" href="#Examples">Examples</a>:                    Some sample scripts
  73. <li><a accesskey="5" href="#Limitations">Limitations</a>:                 Limitations and (non-)limitations of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  74. <li><a accesskey="6" href="#Other-Resources">Other Resources</a>:             Other resources for learning about <samp><span class="command">sed</span></samp>
  75. <li><a accesskey="7" href="#Reporting-Bugs">Reporting Bugs</a>:              Reporting bugs
  76.  
  77. <li><a accesskey="8" href="#Extended-regexps">Extended regexps</a>:            <samp><span class="command">egrep</span></samp>-style regular expressions
  78.  
  79. <li><a accesskey="9" href="#Concept-Index">Concept Index</a>:               A menu with all the topics in this manual. 
  80. <li><a href="#Command-and-Option-Index">Command and Option Index</a>:    A menu with all <samp><span class="command">sed</span></samp> commands and
  81.                                command-line options.
  82.  
  83. </li></ul>
  84. <p>--- The detailed node listing ---
  85.  
  86. <p>sed Programs:
  87. </p>
  88. <ul class="menu">
  89. <li><a href="#Addresses">Addresses</a>:                        Selecting lines with <samp><span class="command">sed</span></samp>
  90. <li><a href="#Regular-Expressions">Regular Expressions</a>:              Overview of regular expression syntax
  91. <li><a href="#Data-Spaces">Data Spaces</a>:                      Where <samp><span class="command">sed</span></samp> buffers data
  92. <li><a href="#Common-Commands">Common Commands</a>:                  Often used commands
  93. <li><a href="#The-_0022s_0022-Command">The "s" Command</a>:                  <samp><span class="command">sed</span></samp>'s Swiss Army Knife
  94. <li><a href="#Other-Commands">Other Commands</a>:                   Less frequently used commands
  95. <li><a href="#Programming-Commands">Programming Commands</a>:             Commands for <samp><span class="command">sed</span></samp> gurus
  96. <li><a href="#Extended-Commands">Extended Commands</a>:                Commands specific of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  97. <li><a href="#Escapes">Escapes</a>:                          Specifying special characters
  98.  
  99. </li></ul>
  100. <p>Examples:
  101. </p>
  102. <ul class="menu">
  103. <li><a href="#Centering-lines">Centering lines</a>
  104. <li><a href="#Increment-a-number">Increment a number</a>
  105. <li><a href="#Rename-files-to-lower-case">Rename files to lower case</a>
  106. <li><a href="#Print-bash-environment">Print bash environment</a>
  107. <li><a href="#Reverse-chars-of-lines">Reverse chars of lines</a>
  108. <li><a href="#tac">tac</a>:                              Reverse lines of files
  109. <li><a href="#cat-_002dn">cat -n</a>:                           Numbering lines
  110. <li><a href="#cat-_002db">cat -b</a>:                           Numbering non-blank lines
  111. <li><a href="#wc-_002dc">wc -c</a>:                            Counting chars
  112. <li><a href="#wc-_002dw">wc -w</a>:                            Counting words
  113. <li><a href="#wc-_002dl">wc -l</a>:                            Counting lines
  114. <li><a href="#head">head</a>:                             Printing the first lines
  115. <li><a href="#tail">tail</a>:                             Printing the last lines
  116. <li><a href="#uniq">uniq</a>:                             Make duplicate lines unique
  117. <li><a href="#uniq-_002dd">uniq -d</a>:                          Print duplicated lines of input
  118. <li><a href="#uniq-_002du">uniq -u</a>:                          Remove all duplicated lines
  119. <li><a href="#cat-_002ds">cat -s</a>:                           Squeezing blank lines
  120.  
  121. </ul>
  122.  
  123. <div class="node">
  124. <p><hr>
  125. <a name="Introduction"></a>
  126. Next: <a rel="next" accesskey="n" href="#Invoking-sed">Invoking sed</a>,
  127. Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>,
  128. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  129.  
  130. </div>
  131.  
  132. <h2 class="chapter">1 Introduction</h2>
  133.  
  134. <p><a name="index-Stream-editor-1"></a><samp><span class="command">sed</span></samp> is a stream editor. 
  135. A stream editor is used to perform basic text
  136. transformations on an input stream
  137. (a file or input from a pipeline). 
  138. While in some ways similar to an editor which
  139. permits scripted edits (such as <samp><span class="command">ed</span></samp>),
  140. <samp><span class="command">sed</span></samp> works by making only one pass over the
  141. input(s), and is consequently more efficient. 
  142. But it is <samp><span class="command">sed</span></samp>'s ability to filter text in a pipeline
  143. which particularly distinguishes it from other types of
  144. editors.
  145.  
  146. <div class="node">
  147. <p><hr>
  148. <a name="Invoking-sed"></a>
  149. Next: <a rel="next" accesskey="n" href="#sed-Programs">sed Programs</a>,
  150. Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,
  151. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  152.  
  153. </div>
  154.  
  155. <h2 class="chapter">2 Invocation</h2>
  156.  
  157. <p><samp><span class="command">sed</span></samp> may be invoked with the following command-line options:
  158.  
  159.      <dl>
  160. <dt><code>-V</code><dt><code>--version</code><dd><a name="index-g_t_002dV-2"></a><a name="index-g_t_002d_002dversion-3"></a><a name="index-Version_002c-printing-4"></a>Print out the version of <samp><span class="command">sed</span></samp> that is being run and a copyright notice,
  161. then exit.
  162.  
  163.      <br><dt><code>-h</code><dt><code>--help</code><dd><a name="index-g_t_002dh-5"></a><a name="index-g_t_002d_002dhelp-6"></a><a name="index-Usage-summary_002c-printing-7"></a>Print a usage message briefly summarizing these command-line options
  164. and the bug-reporting address,
  165. then exit.
  166.  
  167.      <br><dt><code>-n</code><dt><code>--quiet</code><dt><code>--silent</code><dd><a name="index-g_t_002dn-8"></a><a name="index-g_t_002d_002dquiet-9"></a><a name="index-g_t_002d_002dsilent-10"></a><a name="index-Disabling-autoprint_002c-from-command-line-11"></a>By default, <samp><span class="command">sed</span></samp> prints out the pattern space
  168. at the end of each cycle through the script. 
  169. These options disable this automatic printing,
  170. and <samp><span class="command">sed</span></samp> only produces output when explicitly told to
  171. via the <code>p</code> command.
  172.  
  173.      <br><dt><code>-i[</code><var>SUFFIX</var><code>]</code><dt><code>--in-place[=</code><var>SUFFIX</var><code>]</code><dd><a name="index-g_t_002di-12"></a><a name="index-g_t_002d_002din_002dplace-13"></a><a name="index-In_002dplace-editing-14"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-in_002dplace-editing-15"></a>This option specifies that files are to be edited in-place. 
  174. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> does this by creating a temporary file and
  175. sending output to this file rather than to the standard
  176. output.<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>
  177.  
  178.      <p>When the end of the file is reached, the temporary file is
  179. renamed to the output file's original name.
  180.  
  181.      <p>The extension, if supplied, is used to modify the name of
  182. the old file before renaming the temporary file (thereby
  183. making a backup copy<a rel="footnote" href="#fn-2" name="fnd-2"><sup>2</sup></a>) following
  184. this rule: if the extension doesn't contain a <code>*</code>,
  185. then it is appended to the end of the current filename
  186. as a suffix; if the extension does contain one or more
  187. <code>*</code> characters, then <em>each</em> asterisk is
  188. replaced with the current filename.  This allows
  189. you to add a prefix to the backup file, instead of (or in
  190. addition to) a suffix, or even to place backup copies of
  191. the original files into another directory (provided the
  192. directory already exists).
  193.  
  194.      <p>This option implies <samp><span class="option">-s</span></samp>.
  195.  
  196.      <br><dt><code>-l </code><var>N</var><dt><code>--line-length=</code><var>N</var><dd><a name="index-g_t_002dl-16"></a><a name="index-g_t_002d_002dline_002dlength-17"></a><a name="index-Line-length_002c-setting-18"></a>Specify the default line-wrap length for the <code>l</code> command. 
  197. A length of 0 (zero) means to never wrap long lines.  If
  198. not specified, it is taken to be 70.
  199.  
  200.      <br><dt><code>-r</code><dt><code>--regexp-extended</code><dd><a name="index-g_t_002dr-19"></a><a name="index-g_t_002d_002dregexp_002dextended-20"></a><a name="index-Extended-regular-expressions_002c-choosing-21"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-extended-regular-expressions-22"></a>Use extended regular expressions rather than basic
  201. regular expressions.  Extended regexps are those that
  202. <samp><span class="command">egrep</span></samp> accepts; they can be clearer because they
  203. usually have less backslashes, but are a <acronym>GNU</acronym> extension
  204. and hence scripts that use them are not portable. 
  205. See <a href="#Extended-regexps">Extended regular expressions</a>.
  206.  
  207.      <br><dt><code>-s</code><dt><code>--separate</code><dd><a name="index-Working-on-separate-files-23"></a>By default, <samp><span class="command">sed</span></samp> will consider the files specified on the
  208. command line as a single continuous long stream.  This <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  209. extension allows the user to consider them as separate files:
  210. range addresses (such as `<samp><span class="samp">/abc/,/def/</span></samp>') are not allowed
  211. to span several files, line numbers are relative to the start
  212. of each file, <code>$</code> refers to the last line of each file,
  213. and files invoked from the <code>R</code> commands are rewound at the
  214. start of each file.
  215.  
  216.      <br><dt><code>-u</code><dt><code>--unbuffered</code><dd><a name="index-g_t_002du-24"></a><a name="index-g_t_002d_002dunbuffered-25"></a><a name="index-Unbuffered-I_002fO_002c-choosing-26"></a>Buffer both input and output as minimally as practical. 
  217. (This is particularly useful if the input is coming from
  218. the likes of `<samp><span class="samp">tail -f</span></samp>', and you wish to see the transformed
  219. output as soon as possible.)
  220.  
  221.      <br><dt><code>-e </code><var>script</var><dt><code>--expression=</code><var>script</var><dd><a name="index-g_t_002de-27"></a><a name="index-g_t_002d_002dexpression-28"></a><a name="index-Script_002c-from-command-line-29"></a>Add the commands in <var>script</var> to the set of commands to be
  222. run while processing the input.
  223.  
  224.      <br><dt><code>-f </code><var>script-file</var><dt><code>--file=</code><var>script-file</var><dd><a name="index-g_t_002df-30"></a><a name="index-g_t_002d_002dfile-31"></a><a name="index-Script_002c-from-a-file-32"></a>Add the commands contained in the file <var>script-file</var>
  225. to the set of commands to be run while processing the input.
  226.  
  227.    </dl>
  228.  
  229.    <p>If no <samp><span class="option">-e</span></samp>, <samp><span class="option">-f</span></samp>, <samp><span class="option">--expression</span></samp>, or <samp><span class="option">--file</span></samp>
  230. options are given on the command-line,
  231. then the first non-option argument on the command line is
  232. taken to be the <var>script</var> to be executed.
  233.  
  234.    <p><a name="index-Files-to-be-processed-as-input-33"></a>If any command-line parameters remain after processing the above,
  235. these parameters are interpreted as the names of input files to
  236. be processed. 
  237. <a name="index-Standard-input_002c-processing-as-input-34"></a>A file name of `<samp><span class="samp">-</span></samp>' refers to the standard input stream. 
  238. The standard input will be processed if no file names are specified.
  239.  
  240. <div class="node">
  241. <p><hr>
  242. <a name="sed-Programs"></a>
  243. Next: <a rel="next" accesskey="n" href="#Examples">Examples</a>,
  244. Previous: <a rel="previous" accesskey="p" href="#Invoking-sed">Invoking sed</a>,
  245. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  246.  
  247. </div>
  248.  
  249. <h2 class="chapter">3 <samp><span class="command">sed</span></samp> Programs</h2>
  250.  
  251. <p><a name="index-g_t_0040command_007bsed_007d-program-structure-35"></a><a name="index-Script-structure-36"></a>A <samp><span class="command">sed</span></samp> program consists of one or more <samp><span class="command">sed</span></samp> commands,
  252. passed in by one or more of the
  253. <samp><span class="option">-e</span></samp>, <samp><span class="option">-f</span></samp>, <samp><span class="option">--expression</span></samp>, and <samp><span class="option">--file</span></samp>
  254. options, or the first non-option argument if zero of these
  255. options are used. 
  256. This document will refer to “the” <samp><span class="command">sed</span></samp> script;
  257. this is understood to mean the in-order catenation
  258. of all of the <var>script</var>s and <var>script-file</var>s passed in.
  259.  
  260.    <p>Each <code>sed</code> command consists of an optional address or
  261. address range, followed by a one-character command name
  262. and any additional command-specific code.
  263.  
  264. <ul class="menu">
  265. <li><a accesskey="1" href="#Addresses">Addresses</a>:                 Selecting lines with <samp><span class="command">sed</span></samp>
  266. <li><a accesskey="2" href="#Regular-Expressions">Regular Expressions</a>:       Overview of regular expression syntax
  267. <li><a accesskey="3" href="#Data-Spaces">Data Spaces</a>:               Where <samp><span class="command">sed</span></samp> buffers data
  268. <li><a accesskey="4" href="#Common-Commands">Common Commands</a>:           Often used commands
  269. <li><a accesskey="5" href="#The-_0022s_0022-Command">The "s" Command</a>:           <samp><span class="command">sed</span></samp>'s Swiss Army Knife
  270. <li><a accesskey="6" href="#Other-Commands">Other Commands</a>:            Less frequently used commands
  271. <li><a accesskey="7" href="#Programming-Commands">Programming Commands</a>:      Commands for <samp><span class="command">sed</span></samp> gurus
  272. <li><a accesskey="8" href="#Extended-Commands">Extended Commands</a>:         Commands specific of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  273. <li><a accesskey="9" href="#Escapes">Escapes</a>:                   Specifying special characters
  274. </ul>
  275.  
  276. <div class="node">
  277. <p><hr>
  278. <a name="Addresses"></a>
  279. Next: <a rel="next" accesskey="n" href="#Regular-Expressions">Regular Expressions</a>,
  280. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  281.  
  282. </div>
  283.  
  284. <h3 class="section">3.1 Selecting lines with <samp><span class="command">sed</span></samp></h3>
  285.  
  286. <p><a name="index-Addresses_002c-in-_0040command_007bsed_007d-scripts-37"></a><a name="index-Line-selection-38"></a><a name="index-Selecting-lines-to-process-39"></a>
  287. Addresses in a <samp><span class="command">sed</span></samp> script can be in any of the following forms:
  288.      <dl>
  289. <dt><var>number</var><dd><a name="index-Address_002c-numeric-40"></a><a name="index-Line_002c-selecting-by-number-41"></a>Specifying a line number will match only that line in the input. 
  290. (Note that <samp><span class="command">sed</span></samp> counts lines continuously across all input files
  291. unless <samp><span class="option">-i</span></samp> or <samp><span class="option">-s</span></samp> options are specified.)
  292.  
  293.      <br><dt><var>first</var><code>~</code><var>step</var><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040samp_007b_0040var_007bn_007d_007e_0040var_007bm_007d_007d-addresses-42"></a>This <acronym>GNU</acronym> extension matches every <var>step</var>th line
  294. starting with line <var>first</var>. 
  295. In particular, lines will be selected when there exists
  296. a non-negative <var>n</var> such that the current line-number equals
  297. <var>first</var> + (<var>n</var> * <var>step</var>). 
  298. Thus, to select the odd-numbered lines,
  299. one would use <code>1~2</code>;
  300. to pick every third line starting with the second, `<samp><span class="samp">2~3</span></samp>' would be used;
  301. to pick every fifth line starting with the tenth, use `<samp><span class="samp">10~5</span></samp>';
  302. and `<samp><span class="samp">50~0</span></samp>' is just an obscure way of saying <code>50</code>.
  303.  
  304.      <br><dt><code>$</code><dd><a name="index-Address_002c-last-line-43"></a><a name="index-Last-line_002c-selecting-44"></a><a name="index-Line_002c-selecting-last-45"></a>This address matches the last line of the last file of input, or
  305. the last line of each file when the <samp><span class="option">-i</span></samp> or <samp><span class="option">-s</span></samp> options
  306. are specified.
  307.  
  308.      <br><dt><code>/</code><var>regexp</var><code>/</code><dd><a name="index-Address_002c-as-a-regular-expression-46"></a><a name="index-Line_002c-selecting-by-regular-expression-match-47"></a>This will select any line which matches the regular expression <var>regexp</var>. 
  309. If <var>regexp</var> itself includes any <code>/</code> characters,
  310. each must be escaped by a backslash (<code>\</code>).
  311.  
  312.      <p><a name="index-empty-regular-expression-48"></a><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-empty-regular-expression-49"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-modifiers-and-the-empty-regular-expression-50"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-modifiers-and-the-empty-regular-expression-51"></a>Unless <code>POSIXLY_CORRECT</code> is set, the empty regular expression
  313. `<samp><span class="samp">//</span></samp>' repeats the last regular expression match (the same holds
  314. if the empty regular expression is passed to the <code>s</code> command). 
  315. Note that modifiers to regular expressions are evaluated
  316. when the regular expression is compiled, thus it is illegal to specify
  317. them together with the empty regular expression. 
  318. <!-- *** CHECK CURRENT POSIX, I'M NOT SURE THIS IS STILL TRUE. ADR. -->
  319. If <code>POSIXLY_CORRECT</code> is set, instead, `<samp><span class="samp">//</span></samp>' is the null match:
  320. this behavior is mandated by <span class="sc">posix</span>, but it would break too many legacy
  321. <samp><span class="command">sed</span></samp> scripts to blithely change <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>'s default behavior.
  322.  
  323.      <br><dt><code>\%</code><var>regexp</var><code>%</code><dd>(The <code>%</code> may be replaced by any other single character.)
  324.  
  325.      <p><a name="index-Slash-character_002c-in-regular-expressions-52"></a>This also matches the regular expression <var>regexp</var>,
  326. but allows one to use a different delimiter than <code>/</code>. 
  327. This is particularly useful if the <var>regexp</var> itself contains
  328. a lot of slashes, since it avoids the tedious escaping of every <code>/</code>. 
  329. If <var>regexp</var> itself includes any delimiter characters,
  330. each must be escaped by a backslash (<code>\</code>).
  331.  
  332.      <br><dt><code>/</code><var>regexp</var><code>/I</code><dt><code>\%</code><var>regexp</var><code>%I</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-53"></a>The <code>I</code> modifier to regular-expression matching is a <acronym>GNU</acronym>
  333. extension which causes the <var>regexp</var> to be matched in
  334. a case-insensitive manner.
  335.  
  336.      <br><dt><code>/</code><var>regexp</var><code>/M</code><dt><code>\%</code><var>regexp</var><code>%M</code><dd><a name="index-Perl_002dstyle-regular-expressions_002c-multiline-54"></a>The <code>M</code> modifier to regular-expression matching is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  337. extension which causes <code>^</code> and <code>$</code> to match respectively
  338. (in addition to the normal behavior) the empty string after a newline,
  339. and the empty string before a newline.  There are special character
  340. sequences
  341. (<code>\`</code> and <code>\'</code>)
  342. which always match the beginning or the end of the buffer. 
  343. <code>M</code> stands for <cite>multi-line</cite>.
  344.  
  345.    </dl>
  346.  
  347.    <p>If no addresses are given, then all lines are matched;
  348. if one address is given, then only lines matching that
  349. address are matched.
  350.  
  351.    <p><a name="index-Range-of-lines-55"></a><a name="index-Several-lines_002c-selecting-56"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007b0_007d-address-57"></a>An address range can be specified by specifying two addresses
  352. separated by a comma (<code>,</code>). 
  353. An address range matches lines starting from where the first
  354. address matches, and continues until the second address matches
  355. (inclusively). 
  356. If the second address is a <var>regexp</var>, then checking for the
  357. ending match will start with the line <em>following</em> the
  358. line which matched the first address.  As a <acronym>GNU</acronym> extension, a
  359. line number of <code>0</code> can be used in an address specification
  360. like `<samp><span class="samp">0,/</span><var>regexp</var><span class="samp">/</span></samp>' so that <var>regexp</var> will be
  361. matched in the first input line too.
  362.  
  363.    <p>If the second address is a <var>number</var> less than (or equal to)
  364. the line matching the first address,
  365. then only the one line is matched.
  366.  
  367.    <p><a name="index-Special-addressing-forms-58"></a><a name="index-Range-with-start-address-of-zero-59"></a><a name="index-Zero_002c-as-range-start-address-60"></a><a name="index-g_t_0040var_007baddr1_007d_002c_002bN-61"></a><a name="index-g_t_0040var_007baddr1_007d_002c_007eN-62"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-two_002daddress-forms-63"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-0_002c_0040var_007baddr2_007d-addressing-64"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_002b_0040var_007bN_007d-addressing-65"></a><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_007e_0040var_007bN_007d-addressing-66"></a><acronym>GNU</acronym> <samp><span class="command">sed</span></samp> also supports some special two-address forms:
  368.      <dl>
  369. <dt><code>0,</code><var>addr2</var><dd>Start out in “matched first address” state,
  370. until <var>addr2</var> is found. 
  371. This is similar to `<samp><span class="samp">1,</span><var>addr2</var></samp>',
  372. except that if <var>addr2</var> matches the very first line of input
  373. the 0,<var>addr2</var> form will be at the end of its range,
  374. whereas the 1,<var>addr2</var> form will still be at the beginning of its range.
  375.  
  376.      <br><dt><var>addr1</var><code>,+</code><var>N</var><dd>Matches <var>addr1</var> and the <var>N</var> lines following <var>addr1</var>.
  377.  
  378.      <br><dt><var>addr1</var><code>,~</code><var>N</var><dd>Matches <var>addr1</var> and the lines following <var>addr1</var>
  379. until the next line whose input line number is a multiple of <var>N</var>. 
  380. </dl>
  381.  
  382.    <p><a name="index-Excluding-lines-67"></a><a name="index-Selecting-non_002dmatching-lines-68"></a>Appending the <code>!</code> character to the end of an address
  383. specification negates the sense of the match. 
  384. That is, if the <code>!</code> character follows an address range,
  385. then only lines which do <em>not</em> match the address range
  386. will be selected. 
  387. This also works for singleton addresses,
  388. and, perhaps perversely, for the null address.
  389.  
  390. <div class="node">
  391. <p><hr>
  392. <a name="Regular-Expressions"></a>
  393. Next: <a rel="next" accesskey="n" href="#Data-Spaces">Data Spaces</a>,
  394. Previous: <a rel="previous" accesskey="p" href="#Addresses">Addresses</a>,
  395. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  396.  
  397. </div>
  398.  
  399. <h3 class="section">3.2 Overview of Regular Expression Syntax</h3>
  400.  
  401. <p>To know how to use <samp><span class="command">sed</span></samp>, people should understand regular
  402. expressions (<dfn>regexp</dfn> for short).  A regular expression
  403. is a pattern that is matched against a
  404. subject string from left to right.  Most characters are
  405. <dfn>ordinary</dfn>: they stand for
  406. themselves in a pattern, and match the corresponding characters
  407. in the subject.  As a trivial example, the pattern
  408.  
  409. <pre class="example">          The quick brown fox
  410. </pre>
  411.    <p class="noindent">matches a portion of a subject string that is identical to
  412. itself.  The power of regular expressions comes from the
  413. ability to include alternatives and repetitions in the pattern. 
  414. These are encoded in the pattern by the use of <dfn>special characters</dfn>,
  415. which do not stand for themselves but instead
  416. are interpreted in some special way.  Here is a brief description
  417. of regular expression syntax as used in <samp><span class="command">sed</span></samp>.
  418.  
  419.      <dl>
  420. <dt><var>char</var><dd>A single ordinary character matches itself.
  421.  
  422.      <br><dt><code>*</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-69"></a>Matches a sequence of zero or more instances of matches for the
  423. preceding regular expression, which must be an ordinary character, a
  424. special character preceded by <code>\</code>, a <code>.</code>, a grouped regexp
  425. (see below), or a bracket expression.  As a <acronym>GNU</acronym> extension, a
  426. postfixed regular expression can also be followed by <code>*</code>; for
  427. example, <code>a**</code> is equivalent to <code>a*</code>.  <acronym>POSIX</acronym>
  428. 1003.1-2001 says that <code>*</code> stands for itself when it appears at
  429. the start of a regular expression or subexpression, but many
  430. non<acronym>GNU</acronym> implementations do not support this and portable
  431. scripts should instead use <code>\*</code> in these contexts.
  432.  
  433.      <br><dt><code>\+</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-70"></a>As <code>*</code>, but matches one or more.  It is a <acronym>GNU</acronym> extension.
  434.  
  435.      <br><dt><code>\?</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-71"></a>As <code>*</code>, but only matches zero or one.  It is a <acronym>GNU</acronym> extension.
  436.  
  437.      <br><dt><code>\{</code><var>i</var><code>\}</code><dd>As <code>*</code>, but matches exactly <var>i</var> sequences (<var>i</var> is a
  438. decimal integer; for portability, keep it between 0 and 255
  439. inclusive).
  440.  
  441.      <br><dt><code>\{</code><var>i</var><code>,</code><var>j</var><code>\}</code><dd>Matches between <var>i</var> and <var>j</var>, inclusive, sequences.
  442.  
  443.      <br><dt><code>\{</code><var>i</var><code>,\}</code><dd>Matches more than or equal to <var>i</var> sequences.
  444.  
  445.      <br><dt><code>\(</code><var>regexp</var><code>\)</code><dd>Groups the inner <var>regexp</var> as a whole, this is used to:
  446.  
  447.           <ul>
  448. <li>Apply postfix operators, like <code>\(abcd\)*</code>:
  449. this will search for zero or more whole sequences
  450. of `<samp><span class="samp">abcd</span></samp>', while <code>abcd*</code> would search
  451. for `<samp><span class="samp">abc</span></samp>' followed by zero or more occurrences
  452. of `<samp><span class="samp">d</span></samp>'.  Note that support for <code>\(abcd\)*</code> is required by
  453. <acronym>POSIX</acronym> 1003.1-2001, but many non-<acronym>GNU</acronym>
  454. implementations do not support it and hence it is not universally
  455. portable.
  456.  
  457.           <li>Use back references (see below). 
  458. </ul>
  459.  
  460.      <br><dt><code>.</code><dd>Matches any single character, including newline.
  461.  
  462.      <br><dt><code>^</code><dd>Matches the null string at beginning of line, i.e. what
  463. appears after the circumflex must appear at the
  464. beginning of line. <code>^#include</code> will match only
  465. lines where `<samp><span class="samp">#include</span></samp>' is the first thing on line—if
  466. there are spaces before, for example, the match fails. 
  467. <code>^</code> acts as a special character only at the beginning
  468. of the regular expression or subexpression (that is, after <code>\(</code>
  469. or <code>\|</code>).  Portable scripts should avoid <code>^</code> at the
  470. beginning of a subexpression, though, as <acronym>POSIX</acronym> allows
  471. implementations that treat <code>^</code> as an ordinary character in that
  472. context.
  473.  
  474.      <br><dt><code>$</code><dd>It is the same as <code>^</code>, but refers to end of line. 
  475. <code>$</code> also acts as a special character only at the end
  476. of the regular expression or subexpression (that is, before <code>\)</code>
  477. or <code>\|</code>), and its use at the end of a subexpression is not
  478. portable.
  479.  
  480.      <br><dt><code>[</code><var>list</var><code>]</code><dt><code>[^</code><var>list</var><code>]</code><dd>Matches any single character in <var>list</var>: for example,
  481. <code>[aeiou]</code> matches all vowels.  A list may include
  482. sequences like <var>char1</var><code>-</code><var>char2</var>, which
  483. matches any character between (inclusive) <var>char1</var>
  484. and <var>char2</var>.
  485.  
  486.      <p>A leading <code>^</code> reverses the meaning of <var>list</var>, so that
  487. it matches any single character <em>not</em> in <var>list</var>.  To include
  488. <code>]</code> in the list, make it the first character (after
  489. the <code>^</code> if needed), to include <code>-</code> in the list,
  490. make it the first or last; to include <code>^</code> put
  491. it after the first character.
  492.  
  493.      <p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-bracket-expressions-72"></a>The characters <code>$</code>, <code>*</code>, <code>.</code>, <code>[</code>, and <code>\</code>
  494. are normally not special within <var>list</var>.  For example, <code>[\*]</code>
  495. matches either `<samp><span class="samp">\</span></samp>' or `<samp><span class="samp">*</span></samp>', because the <code>\</code> is not
  496. special here.  However, strings like <code>[.ch.]</code>, <code>[=a=]</code>, and
  497. <code>[:space:]</code> are special within <var>list</var> and represent collating
  498. symbols, equivalence classes, and character classes, respectively, and
  499. <code>[</code> is therefore special within <var>list</var> when it is followed by
  500. <code>.</code>, <code>=</code>, or <code>:</code>.  Special escapes like <code>\n</code> and
  501. <code>\t</code> are recognized within <var>list</var>; this will change in a
  502. future version in <code>POSIXLY_CORRECT</code> mode.  See <a href="#Escapes">Escapes</a>.
  503.  
  504.      <br><dt><var>regexp1</var><code>\|</code><var>regexp2</var><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-73"></a>Matches either <var>regexp1</var> or <var>regexp2</var>.  Use
  505. parentheses to use complex alternative regular expressions. 
  506. The matching process tries each alternative in turn, from
  507. left to right, and the first one that succeeds is used. 
  508. It is a <acronym>GNU</acronym> extension.
  509.  
  510.      <br><dt><var>regexp1</var><var>regexp2</var><dd>Matches the concatenation of <var>regexp1</var> and <var>regexp2</var>. 
  511. Concatenation binds more tightly than <code>\|</code>, <code>^</code>, and
  512. <code>$</code>, but less tightly than the other regular expression
  513. operators.
  514.  
  515.      <br><dt><code>\</code><var>digit</var><dd>Matches the <var>digit</var>-th <code>\(...\)</code> parenthesized
  516. subexpression in the regular expression.  This is called a <dfn>back
  517. reference</dfn>.  Subexpressions are implicity numbered by counting
  518. occurrences of <code>\(</code> left-to-right.
  519.  
  520.      <br><dt><code>\n</code><dd>Matches the newline character.
  521.  
  522.      <br><dt><code>\</code><var>char</var><dd>Matches <var>char</var>, where <var>char</var> is one of <code>$</code>,
  523. <code>*</code>, <code>.</code>, <code>[</code>, <code>\</code>, or <code>^</code>. 
  524. Note that the only C-like
  525. backslash sequences that you can portably assume to be
  526. interpreted are <code>\n</code> and <code>\\</code>; in particular
  527. <code>\t</code> is not portable, and matches a `<samp><span class="samp">t</span></samp>' under most
  528. implementations of <samp><span class="command">sed</span></samp>, rather than a tab character.
  529.  
  530.    </dl>
  531.  
  532.    <p><a name="index-Greedy-regular-expression-matching-74"></a>Note that the regular expression matcher is greedy, i.e., if
  533. two or more matches are detected, it selects the longest; if
  534. there are two or more selected with the same size, it
  535. selects the first in text.
  536.  
  537. <p class="noindent">Examples:
  538.      <dl>
  539. <dt>`<samp><span class="samp">abcdef</span></samp>'<dd>Matches `<samp><span class="samp">abcdef</span></samp>'.
  540.  
  541.      <br><dt>`<samp><span class="samp">a*b</span></samp>'<dd>Matches zero or more `<samp><span class="samp">a</span></samp>'s followed by a single
  542. `<samp><span class="samp">b</span></samp>'.  For example, `<samp><span class="samp">b</span></samp>' or `<samp><span class="samp">aaaaab</span></samp>'.
  543.  
  544.      <br><dt>`<samp><span class="samp">a\?b</span></samp>'<dd>Matches `<samp><span class="samp">b</span></samp>' or `<samp><span class="samp">ab</span></samp>'.
  545.  
  546.      <br><dt>`<samp><span class="samp">a\+b\+</span></samp>'<dd>Matches one or more `<samp><span class="samp">a</span></samp>'s followed by one or more
  547. `<samp><span class="samp">b</span></samp>'s: `<samp><span class="samp">ab</span></samp>' is the shortest possible match, but
  548. other examples are `<samp><span class="samp">aaaab</span></samp>' or `<samp><span class="samp">abbbbb</span></samp>' or
  549. `<samp><span class="samp">aaaaaabbbbbbb</span></samp>'.
  550.  
  551.      <br><dt>`<samp><span class="samp">.*</span></samp>'<dt>`<samp><span class="samp">.\+</span></samp>'<dd>These two both match all the characters in a string;
  552. however, the first matches every string (including the empty
  553. string), while the second matches only strings containing
  554. at least one character.
  555.  
  556.      <br><dt>`<samp><span class="samp">^main.*(.*)</span></samp>'<dd>This matches a string starting with `<samp><span class="samp">main</span></samp>',
  557. followed by an opening and closing
  558. parenthesis.  The `<samp><span class="samp">n</span></samp>', `<samp><span class="samp">(</span></samp>' and `<samp><span class="samp">)</span></samp>' need not
  559. be adjacent.
  560.  
  561.      <br><dt>`<samp><span class="samp">^#</span></samp>'<dd>This matches a string beginning with `<samp><span class="samp">#</span></samp>'.
  562.  
  563.      <br><dt>`<samp><span class="samp">\\$</span></samp>'<dd>This matches a string ending with a single backslash.  The
  564. regexp contains two backslashes for escaping.
  565.  
  566.      <br><dt>`<samp><span class="samp">\$</span></samp>'<dd>Instead, this matches a string consisting of a single dollar sign,
  567. because it is escaped.
  568.  
  569.      <br><dt>`<samp><span class="samp">[a-zA-Z0-9]</span></samp>'<dd>In the C locale, this matches any <acronym>ASCII</acronym> letters or digits.
  570.  
  571.      <br><dt>`<samp><span class="samp">[^ </span><kbd>tab</kbd><span class="samp">]\+</span></samp>'<dd>(Here <kbd>tab</kbd> stands for a single tab character.) 
  572. This matches a string of one or more
  573. characters, none of which is a space or a tab. 
  574. Usually this means a word.
  575.  
  576.      <br><dt>`<samp><span class="samp">^\(.*\)\n\1$</span></samp>'<dd>This matches a string consisting of two equal substrings separated by
  577. a newline.
  578.  
  579.      <br><dt>`<samp><span class="samp">.\{9\}A$</span></samp>'<dd>This matches nine characters followed by an `<samp><span class="samp">A</span></samp>'.
  580.  
  581.      <br><dt>`<samp><span class="samp">^.\{15\}A</span></samp>'<dd>This matches the start of a string that contains 16 characters,
  582. the last of which is an `<samp><span class="samp">A</span></samp>'.
  583.  
  584.    </dl>
  585.  
  586. <div class="node">
  587. <p><hr>
  588. <a name="Data-Spaces"></a>
  589. Next: <a rel="next" accesskey="n" href="#Common-Commands">Common Commands</a>,
  590. Previous: <a rel="previous" accesskey="p" href="#Regular-Expressions">Regular Expressions</a>,
  591. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  592.  
  593. </div>
  594.  
  595. <h3 class="section">3.3 Where <samp><span class="command">sed</span></samp> Buffers Data</h3>
  596.  
  597. <p><a name="index-Buffer-spaces_002c-pattern-and-hold-75"></a><a name="index-Spaces_002c-pattern-and-hold-76"></a><a name="index-Pattern-space_002c-definition-77"></a><a name="index-Hold-space_002c-definition-78"></a><samp><span class="command">sed</span></samp> maintains two data buffers: the active <em>pattern</em> space,
  598. and the auxiliary <em>hold</em> space. 
  599. In “normal” operation, <samp><span class="command">sed</span></samp> reads in one line from the
  600. input stream and places it in the pattern space. 
  601. This pattern space is where text manipulations occur. 
  602. The hold space is initially empty, but there are commands
  603. for moving data between the pattern and hold spaces. 
  604. <!-- XXX FIXME: explain why this is useful/interesting to know. -->
  605.  
  606. <div class="node">
  607. <p><hr>
  608. <a name="Common-Commands"></a>
  609. Next: <a rel="next" accesskey="n" href="#The-_0022s_0022-Command">The "s" Command</a>,
  610. Previous: <a rel="previous" accesskey="p" href="#Data-Spaces">Data Spaces</a>,
  611. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  612.  
  613. </div>
  614.  
  615. <h3 class="section">3.4 Often-Used Commands</h3>
  616.  
  617. <p>If you use <samp><span class="command">sed</span></samp> at all, you will quite likely want to know
  618. these commands.
  619.  
  620.      <dl>
  621. <dt><code>#</code><dd>[No addresses allowed.]
  622.  
  623.      <p><a name="index-g_t_0023-_0028comments_0029-79"></a><a name="index-Comments_002c-in-scripts-80"></a>The <code>#</code> character begins a comment;
  624. the comment continues until the next newline.
  625.  
  626.      <p><a name="index-Portability_002c-comments-81"></a>If you are concerned about portability, be aware that
  627. some implementations of <samp><span class="command">sed</span></samp> (which are not <span class="sc">posix</span>
  628. conformant) may only support a single one-line comment,
  629. and then only when the very first character of the script is a <code>#</code>.
  630.  
  631.      <p><a name="index-g_t_002dn_002c-forcing-from-within-a-script-82"></a><a name="index-Caveat-_002d_002d_002d-_0023n-on-first-line-83"></a>Warning: if the first two characters of the <samp><span class="command">sed</span></samp> script
  632. are <code>#n</code>, then the <samp><span class="option">-n</span></samp> (no-autoprint) option is forced. 
  633. If you want to put a comment in the first line of your script
  634. and that comment begins with the letter `<samp><span class="samp">n</span></samp>'
  635. and you do not want this behavior,
  636. then be sure to either use a capital `<samp><span class="samp">N</span></samp>',
  637. or place at least one space before the `<samp><span class="samp">n</span></samp>'.
  638.  
  639.      <br><dt><code>q [</code><var>exit-code</var><code>]</code><dd>This command only accepts a single address.
  640.  
  641.      <p><a name="index-q-_0028quit_0029-command-84"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-85"></a><a name="index-Quitting-86"></a>Exit <samp><span class="command">sed</span></samp> without processing any more commands or input. 
  642. Note that the current pattern space is printed if auto-print is
  643. not disabled with the <samp><span class="option">-n</span></samp> options.  The ability to return
  644. an exit code from the <samp><span class="command">sed</span></samp> script is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
  645.  
  646.      <br><dt><code>d</code><dd><a name="index-d-_0028delete_0029-command-87"></a><a name="index-Text_002c-deleting-88"></a>Delete the pattern space;
  647. immediately start next cycle.
  648.  
  649.      <br><dt><code>p</code><dd><a name="index-p-_0028print_0029-command-89"></a><a name="index-Text_002c-printing-90"></a>Print out the pattern space (to the standard output). 
  650. This command is usually only used in conjunction with the <samp><span class="option">-n</span></samp>
  651. command-line option.
  652.  
  653.      <p>Note: some implementations of <samp><span class="command">sed</span></samp>, such as this one, will
  654. double-print lines when auto-print is not disabled and the <code>p</code>
  655. command is given. 
  656. Other implementations will only print the line once. 
  657. <!-- *** CHECK CURRENT STANDARD. I THINK NOT DOUBLE-PRINTING IS HOW IT WORKS. ADR. -->
  658. Both ways conform with the <span class="sc">posix</span> standard, and so neither
  659. way can be considered to be in error.
  660.  
  661.      <p><a name="index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-91"></a><a name="index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-92"></a>Portable <samp><span class="command">sed</span></samp> scripts should thus avoid relying on either behavior;
  662. either use the <samp><span class="option">-n</span></samp> option and explicitly print what you want,
  663. or avoid use of the <code>p</code> command (and also the <code>p</code> flag to the
  664. <code>s</code> command).
  665.  
  666.      <br><dt><code>n</code><dd><a name="index-n-_0028next_002dline_0029-command-93"></a><a name="index-Next-input-line_002c-replace-pattern-space-with-94"></a><a name="index-Read-next-input-line-95"></a>If auto-print is not disabled, print the pattern space,
  667. then, regardless, replace the pattern space with the next line of input. 
  668. If there is no more input then <samp><span class="command">sed</span></samp> exits without processing
  669. any more commands.
  670.  
  671.      <br><dt><code>{ </code><var>commands</var><code> }</code><dd><a name="index-g_t_0040_007b_0040_007d-command-grouping-96"></a><a name="index-Grouping-commands-97"></a><a name="index-Command-groups-98"></a>A group of commands may be enclosed between
  672. <code>{</code> and <code>}</code> characters. 
  673. This is particularly useful when you want a group of commands
  674. to be triggered by a single address (or address-range) match.
  675.  
  676.    </dl>
  677.  
  678. <div class="node">
  679. <p><hr>
  680. <a name="The-%22s%22-Command"></a>
  681. <a name="The-_0022s_0022-Command"></a>
  682. Next: <a rel="next" accesskey="n" href="#Other-Commands">Other Commands</a>,
  683. Previous: <a rel="previous" accesskey="p" href="#Common-Commands">Common Commands</a>,
  684. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  685.  
  686. </div>
  687.  
  688. <h3 class="section">3.5 The <code>s</code> Command</h3>
  689.  
  690. <p>The syntax of the <code>s</code> (as in substitute) command is
  691. `<samp><span class="samp">s/</span><var>regexp</var><span class="samp">/</span><var>replacement</var><span class="samp">/</span><var>flags</var></samp>'.  The <code>/</code>
  692. characters may be uniformly replaced by any other single
  693. character within any given <code>s</code> command.  The <code>/</code>
  694. character (or whatever other character is used in its stead)
  695. can appear in the <var>regexp</var> or <var>replacement</var>
  696. only if it is preceded by a <code>\</code> character.
  697.  
  698.    <p>The <code>s</code> command is probably the most important in <samp><span class="command">sed</span></samp>
  699. and has a lot of different options.  Its basic concept is simple:
  700. the <code>s</code> command attempts to match the pattern
  701. space against the supplied <var>regexp</var>; if the match is
  702. successful, then that portion of the pattern
  703. space which was matched is replaced with <var>replacement</var>.
  704.  
  705.    <p><a name="index-Backreferences_002c-in-regular-expressions-99"></a><a name="index-Parenthesized-substrings-100"></a>The <var>replacement</var> can contain <code>\</code><var>n</var> (<var>n</var> being
  706. a number from 1 to 9, inclusive) references, which refer to
  707. the portion of the match which is contained between the <var>n</var>th
  708. <code>\(</code> and its matching <code>\)</code>. 
  709. Also, the <var>replacement</var> can contain unescaped <code>&</code>
  710. characters which reference the whole matched portion
  711. of the pattern space. 
  712. <a name="index-g_t_0040value_007bSSEDEXT_007d_002c-case-modifiers-in-_0040code_007bs_007d-commands-101"></a>Finally (this is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension) you can include a
  713. special sequence made of a backslash and one of the letters
  714. <code>L</code>, <code>l</code>, <code>U</code>, <code>u</code>, or <code>E</code>. 
  715. The meaning is as follows:
  716.  
  717.      <dl>
  718. <dt><code>\L</code><dd>Turn the replacement
  719. to lowercase until a <code>\U</code> or <code>\E</code> is found,
  720.  
  721.      <br><dt><code>\l</code><dd>Turn the
  722. next character to lowercase,
  723.  
  724.      <br><dt><code>\U</code><dd>Turn the replacement to uppercase
  725. until a <code>\L</code> or <code>\E</code> is found,
  726.  
  727.      <br><dt><code>\u</code><dd>Turn the next character
  728. to uppercase,
  729.  
  730.      <br><dt><code>\E</code><dd>Stop case conversion started by <code>\L</code> or <code>\U</code>. 
  731. </dl>
  732.  
  733.    <p>To include a literal <code>\</code>, <code>&</code>, or newline in the final
  734. replacement, be sure to precede the desired <code>\</code>, <code>&</code>,
  735. or newline in the <var>replacement</var> with a <code>\</code>.
  736.  
  737.    <p><a name="index-s-command_002c-option-flags-102"></a><a name="index-Substitution-of-text_002c-options-103"></a>The <code>s</code> command can be followed by zero or more of the
  738. following <var>flags</var>:
  739.  
  740.      <dl>
  741. <dt><code>g</code><dd><a name="index-Global-substitution-104"></a><a name="index-Replacing-all-text-matching-regexp-in-a-line-105"></a>Apply the replacement to <em>all</em> matches to the <var>regexp</var>,
  742. not just the first.
  743.  
  744.      <br><dt><var>number</var><dd><a name="index-Replacing-only-_0040var_007bn_007dth-match-of-regexp-in-a-line-106"></a>Only replace the <var>number</var>th match of the <var>regexp</var>.
  745.  
  746.      <p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifier-interaction-in-_0040code_007bs_007d-command-107"></a><a name="index-Mixing-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifiers-in-the-_0040code_007bs_007d-command-108"></a>Note: the <span class="sc">posix</span> standard does not specify what should happen
  747. when you mix the <code>g</code> and <var>number</var> modifiers,
  748. and currently there is no widely agreed upon meaning
  749. across <samp><span class="command">sed</span></samp> implementations. 
  750. For <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>, the interaction is defined to be:
  751. ignore matches before the <var>number</var>th,
  752. and then match and replace all matches from
  753. the <var>number</var>th on.
  754.  
  755.      <br><dt><code>p</code><dd><a name="index-Text_002c-printing-after-substitution-109"></a>If the substitution was made, then print the new pattern space.
  756.  
  757.      <p>Note: when both the <code>p</code> and <code>e</code> options are specified,
  758. the relative ordering of the two produces very different results. 
  759. In general, <code>ep</code> (evaluate then print) is what you want,
  760. but operating the other way round can be useful for debugging. 
  761. For this reason, the current version of <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> interprets
  762. specially the presence of <code>p</code> options both before and after
  763. <code>e</code>, printing the pattern space before and after evaluation,
  764. while in general flags for the <code>s</code> command show their
  765. effect just once.  This behavior, although documented, might
  766. change in future versions.
  767.  
  768.      <br><dt><code>w </code><var>file-name</var><dd><a name="index-Text_002c-writing-to-a-file-after-substitution-110"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-111"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-112"></a>If the substitution was made, then write out the result to the named file. 
  769. As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, two special values of <var>file-name</var> are
  770. supported: <samp><span class="file">/dev/stderr</span></samp>, which writes the result to the standard
  771. error, and <samp><span class="file">/dev/stdout</span></samp>, which writes to the standard
  772. output.<a rel="footnote" href="#fn-3" name="fnd-3"><sup>3</sup></a>
  773.  
  774.      <br><dt><code>e</code><dd><a name="index-Evaluate-Bourne_002dshell-commands_002c-after-substitution-113"></a><a name="index-Subprocesses-114"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-115"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-116"></a>This command allows one to pipe input from a shell command
  775. into pattern space.  If a substitution was made, the command
  776. that is found in pattern space is executed and pattern space
  777. is replaced with its output.  A trailing newline is suppressed;
  778. results are undefined if the command to be executed contains
  779. a <span class="sc">nul</span> character.  This is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
  780.  
  781.      <br><dt><code>I</code><dt><code>i</code><dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-117"></a><a name="index-Case_002dinsensitive-matching-118"></a>The <code>I</code> modifier to regular-expression matching is a <acronym>GNU</acronym>
  782. extension which makes <samp><span class="command">sed</span></samp> match <var>regexp</var> in a
  783. case-insensitive manner.
  784.  
  785.      <br><dt><code>M</code><dt><code>m</code><dd><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bM_007d-modifier-119"></a>The <code>M</code> modifier to regular-expression matching is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  786. extension which causes <code>^</code> and <code>$</code> to match respectively
  787. (in addition to the normal behavior) the empty string after a newline,
  788. and the empty string before a newline.  There are special character
  789. sequences
  790. (<code>\`</code> and <code>\'</code>)
  791. which always match the beginning or the end of the buffer. 
  792. <code>M</code> stands for <cite>multi-line</cite>.
  793.  
  794.    </dl>
  795.  
  796. <div class="node">
  797. <p><hr>
  798. <a name="Other-Commands"></a>
  799. Next: <a rel="next" accesskey="n" href="#Programming-Commands">Programming Commands</a>,
  800. Previous: <a rel="previous" accesskey="p" href="#The-_0022s_0022-Command">The "s" Command</a>,
  801. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  802.  
  803. </div>
  804.  
  805. <h3 class="section">3.6 Less Frequently-Used Commands</h3>
  806.  
  807. <p>Though perhaps less frequently used than those in the previous
  808. section, some very small yet useful <samp><span class="command">sed</span></samp> scripts can be built with
  809. these commands.
  810.  
  811.      <dl>
  812. <dt><code>y/</code><var>source-chars</var><code>/</code><var>dest-chars</var><code>/</code><dd>(The <code>/</code> characters may be uniformly replaced by
  813. any other single character within any given <code>y</code> command.)
  814.  
  815.      <p><a name="index-y-_0028transliterate_0029-command-120"></a><a name="index-Transliteration-121"></a>Transliterate any characters in the pattern space which match
  816. any of the <var>source-chars</var> with the corresponding character
  817. in <var>dest-chars</var>.
  818.  
  819.      <p>Instances of the <code>/</code> (or whatever other character is used in its stead),
  820. <code>\</code>, or newlines can appear in the <var>source-chars</var> or <var>dest-chars</var>
  821. lists, provide that each instance is escaped by a <code>\</code>. 
  822. The <var>source-chars</var> and <var>dest-chars</var> lists <em>must</em>
  823. contain the same number of characters (after de-escaping).
  824.  
  825.      <br><dt><code>a\</code><dt><var>text</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-122"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
  826. address.
  827.  
  828.      <p><a name="index-a-_0028append-text-lines_0029-command-123"></a><a name="index-Appending-text-after-a-line-124"></a><a name="index-Text_002c-appending-125"></a>Queue the lines of text which follow this command
  829. (each but the last ending with a <code>\</code>,
  830. which are removed from the output)
  831. to be output at the end of the current cycle,
  832. or when the next input line is read.
  833.  
  834.      <p>As a <acronym>GNU</acronym> extension, if between the <code>a</code> and the newline there is
  835. other than a whitespace-<code>\</code> sequence, then the text of this line,
  836. starting at the first non-whitespace character after the <code>a</code>,
  837. is taken as the first line of the <var>text</var> block. 
  838. (This enables a simplification in scripting a one-line add.) 
  839. This extension also works with the <code>i</code> and <code>c</code> commands.
  840.  
  841.      <br><dt><code>i\</code><dt><var>text</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-126"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
  842. address.
  843.  
  844.      <p><a name="index-i-_0028insert-text-lines_0029-command-127"></a><a name="index-Inserting-text-before-a-line-128"></a><a name="index-Text_002c-insertion-129"></a>Immediately output the lines of text which follow this command
  845. (each but the last ending with a <code>\</code>,
  846. which are removed from the output).
  847.  
  848.      <br><dt><code>c\</code><dt><var>text</var><dd><a name="index-c-_0028change-to-text-lines_0029-command-130"></a><a name="index-Replacing-selected-lines-with-other-text-131"></a>Delete the lines matching the address or address-range,
  849. and output the lines of text which follow this command
  850. (each but the last ending with a <code>\</code>,
  851. which are removed from the output)
  852. in place of the last line
  853. (or in place of each line, if no addresses were specified). 
  854. A new cycle is started after this command is done,
  855. since the pattern space will have been deleted.
  856.  
  857.      <br><dt><code>=</code><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-132"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
  858. address.
  859.  
  860.      <p><a name="index-g_t_003d-_0028print-line-number_0029-command-133"></a><a name="index-Printing-line-number-134"></a><a name="index-Line-number_002c-printing-135"></a>Print out the current input line number (with a trailing newline).
  861.  
  862.      <br><dt><code>l </code><var>n</var><dd><a name="index-l-_0028list-unambiguously_0029-command-136"></a><a name="index-List-pattern-space-137"></a><a name="index-Printing-text-unambiguously-138"></a><a name="index-Line-length_002c-setting-139"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-setting-line-length-140"></a>Print the pattern space in an unambiguous form:
  863. non-printable characters (and the <code>\</code> character)
  864. are printed in C-style escaped form; long lines are split,
  865. with a trailing <code>\</code> character to indicate the split;
  866. the end of each line is marked with a <code>$</code>.
  867.  
  868.      <p><var>n</var> specifies the desired line-wrap length;
  869. a length of 0 (zero) means to never wrap long lines.  If omitted,
  870. the default as specified on the command line is used.  The <var>n</var>
  871. parameter is a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension.
  872.  
  873.      <br><dt><code>r </code><var>filename</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-141"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
  874. address.
  875.  
  876.      <p><a name="index-r-_0028read-file_0029-command-142"></a><a name="index-Read-text-from-a-file-143"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-144"></a>Queue the contents of <var>filename</var> to be read and
  877. inserted into the output stream at the end of the current cycle,
  878. or when the next input line is read. 
  879. Note that if <var>filename</var> cannot be read, it is treated as
  880. if it were an empty file, without any error indication.
  881.  
  882.      <p>As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, the special value <samp><span class="file">/dev/stdin</span></samp>
  883. is supported for the file name, which reads the contents of the
  884. standard input.
  885.  
  886.      <br><dt><code>w </code><var>filename</var><dd><a name="index-w-_0028write-file_0029-command-145"></a><a name="index-Write-to-a-file-146"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-147"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-148"></a>Write the pattern space to <var>filename</var>. 
  887. As a <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension, two special values of <var>file-name</var> are
  888. supported: <samp><span class="file">/dev/stderr</span></samp>, which writes the result to the standard
  889. error, and <samp><span class="file">/dev/stdout</span></samp>, which writes to the standard
  890. output.<a rel="footnote" href="#fn-4" name="fnd-4"><sup>4</sup></a>
  891.  
  892.      <p>The file will be created (or truncated) before the
  893. first input line is read; all <code>w</code> commands
  894. (including instances of <code>w</code> flag on successful <code>s</code> commands)
  895. which refer to the same <var>filename</var> are output without
  896. closing and reopening the file.
  897.  
  898.      <br><dt><code>D</code><dd><a name="index-D-_0028delete-first-line_0029-command-149"></a><a name="index-Delete-first-line-from-pattern-space-150"></a>Delete text in the pattern space up to the first newline. 
  899. If any text is left, restart cycle with the resultant
  900. pattern space (without reading a new line of input),
  901. otherwise start a normal new cycle.
  902.  
  903.      <br><dt><code>N</code><dd><a name="index-N-_0028append-Next-line_0029-command-151"></a><a name="index-Next-input-line_002c-append-to-pattern-space-152"></a><a name="index-Append-next-input-line-to-pattern-space-153"></a>Add a newline to the pattern space,
  904. then append the next line of input to the pattern space. 
  905. If there is no more input then <samp><span class="command">sed</span></samp> exits without processing
  906. any more commands.
  907.  
  908.      <br><dt><code>P</code><dd><a name="index-P-_0028print-first-line_0029-command-154"></a><a name="index-Print-first-line-from-pattern-space-155"></a>Print out the portion of the pattern space up to the first newline.
  909.  
  910.      <br><dt><code>h</code><dd><a name="index-h-_0028hold_0029-command-156"></a><a name="index-Copy-pattern-space-into-hold-space-157"></a><a name="index-Replace-hold-space-with-copy-of-pattern-space-158"></a><a name="index-Hold-space_002c-copying-pattern-space-into-159"></a>Replace the contents of the hold space with the contents of the pattern space.
  911.  
  912.      <br><dt><code>H</code><dd><a name="index-H-_0028append-Hold_0029-command-160"></a><a name="index-Append-pattern-space-to-hold-space-161"></a><a name="index-Hold-space_002c-appending-from-pattern-space-162"></a>Append a newline to the contents of the hold space,
  913. and then append the contents of the pattern space to that of the hold space.
  914.  
  915.      <br><dt><code>g</code><dd><a name="index-g-_0028get_0029-command-163"></a><a name="index-Copy-hold-space-into-pattern-space-164"></a><a name="index-Replace-pattern-space-with-copy-of-hold-space-165"></a><a name="index-Hold-space_002c-copy-into-pattern-space-166"></a>Replace the contents of the pattern space with the contents of the hold space.
  916.  
  917.      <br><dt><code>G</code><dd><a name="index-G-_0028appending-Get_0029-command-167"></a><a name="index-Append-hold-space-to-pattern-space-168"></a><a name="index-Hold-space_002c-appending-to-pattern-space-169"></a>Append a newline to the contents of the pattern space,
  918. and then append the contents of the hold space to that of the pattern space.
  919.  
  920.      <br><dt><code>x</code><dd><a name="index-x-_0028eXchange_0029-command-170"></a><a name="index-Exchange-hold-space-with-pattern-space-171"></a><a name="index-Hold-space_002c-exchange-with-pattern-space-172"></a>Exchange the contents of the hold and pattern spaces.
  921.  
  922.    </dl>
  923.  
  924. <div class="node">
  925. <p><hr>
  926. <a name="Programming-Commands"></a>
  927. Next: <a rel="next" accesskey="n" href="#Extended-Commands">Extended Commands</a>,
  928. Previous: <a rel="previous" accesskey="p" href="#Other-Commands">Other Commands</a>,
  929. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  930.  
  931. </div>
  932.  
  933. <h3 class="section">3.7 Commands for <samp><span class="command">sed</span></samp> gurus</h3>
  934.  
  935. <p>In most cases, use of these commands indicates that you are
  936. probably better off programming in something like <samp><span class="command">awk</span></samp>
  937. or Perl.  But occasionally one is committed to sticking
  938. with <samp><span class="command">sed</span></samp>, and these commands can enable one to write
  939. quite convoluted scripts.
  940.  
  941.    <p><a name="index-Flow-of-control-in-scripts-173"></a>
  942.      <dl>
  943. <dt><code>: </code><var>label</var><dd>[No addresses allowed.]
  944.  
  945.      <p><a name="index-g_t_003a-_0028label_0029-command-174"></a><a name="index-Labels_002c-in-scripts-175"></a>Specify the location of <var>label</var> for branch commands. 
  946. In all other respects, a no-op.
  947.  
  948.      <br><dt><code>b </code><var>label</var><dd><a name="index-b-_0028branch_0029-command-176"></a><a name="index-Branch-to-a-label_002c-unconditionally-177"></a><a name="index-Goto_002c-in-scripts-178"></a>Unconditionally branch to <var>label</var>. 
  949. The <var>label</var> may be omitted, in which case the next cycle is started.
  950.  
  951.      <br><dt><code>t </code><var>label</var><dd><a name="index-t-_0028test-and-branch-if-successful_0029-command-179"></a><a name="index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-succeeded-180"></a><a name="index-Conditional-branch-181"></a>Branch to <var>label</var> only if there has been a successful <code>s</code>ubstitution
  952. since the last input line was read or conditional branch was taken. 
  953. The <var>label</var> may be omitted, in which case the next cycle is started.
  954.  
  955.    </dl>
  956.  
  957. <div class="node">
  958. <p><hr>
  959. <a name="Extended-Commands"></a>
  960. Next: <a rel="next" accesskey="n" href="#Escapes">Escapes</a>,
  961. Previous: <a rel="previous" accesskey="p" href="#Programming-Commands">Programming Commands</a>,
  962. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  963.  
  964. </div>
  965.  
  966. <h3 class="section">3.8 Commands Specific to <acronym>GNU</acronym> <samp><span class="command">sed</span></samp></h3>
  967.  
  968. <p>These commands are specific to <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>, so you
  969. must use them with care and only when you are sure that
  970. hindering portability is not evil.  They allow you to check
  971. for <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extensions or to do tasks that are required
  972. quite often, yet are unsupported by standard <samp><span class="command">sed</span></samp>s.
  973.  
  974.      <dl>
  975. <dt><code>e [</code><var>command</var><code>]</code><dd><a name="index-e-_0028evaluate_0029-command-182"></a><a name="index-Evaluate-Bourne_002dshell-commands-183"></a><a name="index-Subprocesses-184"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-185"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-186"></a>This command allows one to pipe input from a shell command
  976. into pattern space.  Without parameters, the <code>e</code> command
  977. executes the command that is found in pattern space and
  978. replaces the pattern space with the output; a trailing newline
  979. is suppressed.
  980.  
  981.      <p>If a parameter is specified, instead, the <code>e</code> command
  982. interprets it as a command and sends its output to the output stream
  983. (like <code>r</code> does).  The command can run across multiple
  984. lines, all but the last ending with a back-slash.
  985.  
  986.      <p>In both cases, the results are undefined if the command to be
  987. executed contains a <span class="sc">nul</span> character.
  988.  
  989.      <br><dt><code>L </code><var>n</var><dd><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-187"></a>In <code>POSIXLY_CORRECT</code> mode, this command only accepts a single
  990. address.
  991.  
  992.      <p><a name="index-L-_0028fLow-paragraphs_0029-command-188"></a><a name="index-Reformat-pattern-space-189"></a><a name="index-Reformatting-paragraphs-190"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-reformatting-paragraphs-191"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bL_007d-command-192"></a>This <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extension fills and joins lines in pattern space
  993. to produce output lines of (at most) <var>n</var> characters, like
  994. <code>fmt</code> does; if <var>n</var> is omitted, the default as specified
  995. on the command line is used.
  996.  
  997.      <p>Blank lines, spaces between words, and indentation are
  998. preserved in the output; successive input lines with different
  999. indentation are not joined; tabs are expanded to 8 columns.
  1000.  
  1001.      <p>If the pattern space contains multiple lines, they are joined, but
  1002. since the pattern space usually contains a single line, the behavior
  1003. of a simple <code>L;d</code> script is the same as `<samp><span class="samp">fmt -s</span></samp>' (i.e.,
  1004. it does not join short lines to form longer ones).
  1005.  
  1006.      <p><var>n</var> specifies the desired line-wrap length; if omitted,
  1007. the default as specified on the command line is used.
  1008.  
  1009.      <br><dt><code>Q [</code><var>exit-code</var><code>]</code><dd>This command only accepts a single address.
  1010.  
  1011.      <p><a name="index-Q-_0028silent-Quit_0029-command-193"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-quitting-silently-194"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-195"></a><a name="index-Quitting-196"></a>This command is the same as <code>q</code>, but will not print the
  1012. contents of pattern space.  Like <code>q</code>, it provides the
  1013. ability to return an exit code to the caller.
  1014.  
  1015.      <p>This command can be useful because the only alternative ways
  1016. to accomplish this apparently trivial function are to use
  1017. the <samp><span class="option">-n</span></samp> option (which can unnecessarily complicate
  1018. your script) or resorting to the following snippet, which
  1019. wastes time by reading the whole file without any visible effect:
  1020.  
  1021.      <pre class="example">          :eat
  1022.           $d       <i>Quit silently on the last line</i>
  1023.           N        <i>Read another line, silently</i>
  1024.           g        <i>Overwrite pattern space each time to save memory</i>
  1025.           b eat
  1026.      </pre>
  1027.      <br><dt><code>R </code><var>filename</var><dd><a name="index-R-_0028read-line_0029-command-197"></a><a name="index-Read-text-from-a-file-198"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-reading-a-file-a-line-at-a-time-199"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bR_007d-command-200"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-201"></a>Queue a line of <var>filename</var> to be read and
  1028. inserted into the output stream at the end of the current cycle,
  1029. or when the next input line is read. 
  1030. Note that if <var>filename</var> cannot be read, or if its end is
  1031. reached, no line is appended, without any error indication.
  1032.  
  1033.      <p>As with the <code>r</code> command, the special value <samp><span class="file">/dev/stdin</span></samp>
  1034. is supported for the file name, which reads a line from the
  1035. standard input.
  1036.  
  1037.      <br><dt><code>T </code><var>label</var><dd><a name="index-T-_0028test-and-branch-if-failed_0029-command-202"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-branch-if-_0040code_007bs_002f_002f_002f_007d-failed-203"></a><a name="index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-failed-204"></a><a name="index-Conditional-branch-205"></a>Branch to <var>label</var> only if there have been no successful
  1038. <code>s</code>ubstitutions since the last input line was read or
  1039. conditional branch was taken. The <var>label</var> may be omitted,
  1040. in which case the next cycle is started.
  1041.  
  1042.      <br><dt><code>v </code><var>version</var><dd><a name="index-v-_0028version_0029-command-206"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-checking-for-their-presence-207"></a><a name="index-Requiring-_0040value_007bSSED_007d-208"></a>This command does nothing, but makes <samp><span class="command">sed</span></samp> fail if
  1043. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> extensions are not supported, simply because other
  1044. versions of <samp><span class="command">sed</span></samp> do not implement it.  In addition, you
  1045. can specify the version of <samp><span class="command">sed</span></samp> that your script
  1046. requires, such as <code>4.0.5</code>.  The default is <code>4.0</code>
  1047. because that is the first version that implemented this command.
  1048.  
  1049.      <p>This commands also enables <acronym>GNU</acronym> extensions unconditionally, even
  1050. if <samp><span class="env">POSIXLY_CORRECT</span></samp> is set in the environment.
  1051.  
  1052.      <br><dt><code>W </code><var>filename</var><dd><a name="index-W-_0028write-first-line_0029-command-209"></a><a name="index-Write-first-line-to-a-file-210"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-writing-first-line-to-a-file-211"></a>Write to the given filename the portion of the pattern space up to
  1053. the first newline.  Everything said under the <code>w</code> command about
  1054. file handling holds here too. 
  1055. </dl>
  1056.  
  1057. <div class="node">
  1058. <p><hr>
  1059. <a name="Escapes"></a>
  1060. Previous: <a rel="previous" accesskey="p" href="#Extended-Commands">Extended Commands</a>,
  1061. Up: <a rel="up" accesskey="u" href="#sed-Programs">sed Programs</a>
  1062.  
  1063. </div>
  1064.  
  1065. <h3 class="section">3.9 <acronym>GNU</acronym> Extensions for Escapes in Regular Expressions</h3>
  1066.  
  1067. <p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-212"></a>Until this chapter, we have only encountered escapes of the form
  1068. `<samp><span class="samp">\^</span></samp>', which tell <samp><span class="command">sed</span></samp> not to interpret the circumflex
  1069. as a special character, but rather to take it literally.  For
  1070. example, `<samp><span class="samp">\*</span></samp>' matches a single asterisk rather than zero
  1071. or more backslashes.
  1072.  
  1073.    <p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-escapes-213"></a>This chapter introduces another kind of escape<a rel="footnote" href="#fn-5" name="fnd-5"><sup>5</sup></a>—that
  1074. is, escapes that are applied to a character or sequence of characters
  1075. that ordinarily are taken literally, and that <samp><span class="command">sed</span></samp> replaces
  1076. with a special character.  This provides a way
  1077. of encoding non-printable characters in patterns in a visible manner. 
  1078. There is no restriction on the appearance of non-printing characters
  1079. in a <samp><span class="command">sed</span></samp> script but when a script is being prepared in the
  1080. shell or by text editing, it is usually easier to use one of
  1081. the following escape sequences than the binary character it
  1082. represents:
  1083.  
  1084.    <p>The list of these escapes is:
  1085.  
  1086.      <dl>
  1087. <dt><code>\a</code><dd>Produces or matches a <span class="sc">bel</span> character, that is an “alert” (<span class="sc">ascii</span> 7).
  1088.  
  1089.      <br><dt><code>\f</code><dd>Produces or matches a form feed (<span class="sc">ascii</span> 12).
  1090.  
  1091.      <br><dt><code>\n</code><dd>Produces or matches a newline (<span class="sc">ascii</span> 10).
  1092.  
  1093.      <br><dt><code>\r</code><dd>Produces or matches a carriage return (<span class="sc">ascii</span> 13).
  1094.  
  1095.      <br><dt><code>\t</code><dd>Produces or matches a horizontal tab (<span class="sc">ascii</span> 9).
  1096.  
  1097.      <br><dt><code>\v</code><dd>Produces or matches a so called “vertical tab” (<span class="sc">ascii</span> 11).
  1098.  
  1099.      <br><dt><code>\c</code><var>x</var><dd>Produces or matches <span class="sc">Control</span><kbd>-</kbd><var>x</var>, where <var>x</var> is
  1100. any character.  The precise effect of `<samp><span class="samp">\c</span><var>x</var></samp>' is as follows:
  1101. if <var>x</var> is a lower case letter, it is converted to upper case. 
  1102. Then bit 6 of the character (hex 40) is inverted.  Thus `<samp><span class="samp">\cz</span></samp>' becomes
  1103. hex 1A, but `<samp><span class="samp">\c{</span></samp>' becomes hex 3B, while `<samp><span class="samp">\c;</span></samp>' becomes hex 7B.
  1104.  
  1105.      <br><dt><code>\d</code><var>xxx</var><dd>Produces or matches a character whose decimal <span class="sc">ascii</span> value is <var>xxx</var>.
  1106.  
  1107.      <br><dt><code>\o</code><var>xxx</var><dd>Produces or matches a character whose octal <span class="sc">ascii</span> value is <var>xxx</var>.
  1108.  
  1109.      <br><dt><code>\x</code><var>xx</var><dd>Produces or matches a character whose hexadecimal <span class="sc">ascii</span> value is <var>xx</var>. 
  1110. </dl>
  1111.  
  1112.    <p>`<samp><span class="samp">\b</span></samp>' (backspace) was omitted because of the conflict with
  1113. the existing “word boundary” meaning.
  1114.  
  1115.    <p>Other escapes match a particular character class and are valid only in
  1116. regular expressions:
  1117.  
  1118.      <dl>
  1119. <dt><code>\w</code><dd>Matches any “word” character.  A “word” character is any
  1120. letter or digit or the underscore character.
  1121.  
  1122.      <br><dt><code>\W</code><dd>Matches any “non-word” character.
  1123.  
  1124.      <br><dt><code>\b</code><dd>Matches a word boundary; that is it matches if the character
  1125. to the left is a “word” character and the character to the
  1126. right is a “non-word” character, or vice-versa.
  1127.  
  1128.      <br><dt><code>\B</code><dd>Matches everywhere but on a word boundary; that is it matches
  1129. if the character to the left and the character to the right
  1130. are either both “word” characters or both “non-word”
  1131. characters.
  1132.  
  1133.      <br><dt><code>\`</code><dd>Matches only at the start of pattern space.  This is different
  1134. from <code>^</code> in multi-line mode.
  1135.  
  1136.      <br><dt><code>\'</code><dd>Matches only at the end of pattern space.  This is different
  1137. from <code>$</code> in multi-line mode. 
  1138. </dl>
  1139.  
  1140. <div class="node">
  1141. <p><hr>
  1142. <a name="Examples"></a>
  1143. Next: <a rel="next" accesskey="n" href="#Limitations">Limitations</a>,
  1144. Previous: <a rel="previous" accesskey="p" href="#sed-Programs">sed Programs</a>,
  1145. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  1146.  
  1147. </div>
  1148.  
  1149. <h2 class="chapter">4 Some Sample Scripts</h2>
  1150.  
  1151. <p>Here are some <samp><span class="command">sed</span></samp> scripts to guide you in the art of mastering
  1152. <samp><span class="command">sed</span></samp>.
  1153.  
  1154. </li></ul>
  1155. <p>Some exotic examples:
  1156. <ul class="menu">
  1157. <li><a accesskey="1" href="#Centering-lines">Centering lines</a>
  1158. <li><a accesskey="2" href="#Increment-a-number">Increment a number</a>
  1159. <li><a accesskey="3" href="#Rename-files-to-lower-case">Rename files to lower case</a>
  1160. <li><a accesskey="4" href="#Print-bash-environment">Print bash environment</a>
  1161. <li><a accesskey="5" href="#Reverse-chars-of-lines">Reverse chars of lines</a>
  1162.  
  1163. </li></ul>
  1164. <p>Emulating standard utilities:
  1165. </p>
  1166. <ul class="menu">
  1167. <li><a accesskey="6" href="#tac">tac</a>:                              Reverse lines of files
  1168. <li><a accesskey="7" href="#cat-_002dn">cat -n</a>:                           Numbering lines
  1169. <li><a accesskey="8" href="#cat-_002db">cat -b</a>:                           Numbering non-blank lines
  1170. <li><a accesskey="9" href="#wc-_002dc">wc -c</a>:                            Counting chars
  1171. <li><a href="#wc-_002dw">wc -w</a>:                            Counting words
  1172. <li><a href="#wc-_002dl">wc -l</a>:                            Counting lines
  1173. <li><a href="#head">head</a>:                             Printing the first lines
  1174. <li><a href="#tail">tail</a>:                             Printing the last lines
  1175. <li><a href="#uniq">uniq</a>:                             Make duplicate lines unique
  1176. <li><a href="#uniq-_002dd">uniq -d</a>:                          Print duplicated lines of input
  1177. <li><a href="#uniq-_002du">uniq -u</a>:                          Remove all duplicated lines
  1178. <li><a href="#cat-_002ds">cat -s</a>:                           Squeezing blank lines
  1179. </ul>
  1180.  
  1181. <div class="node">
  1182. <p><hr>
  1183. <a name="Centering-lines"></a>
  1184. Next: <a rel="next" accesskey="n" href="#Increment-a-number">Increment a number</a>,
  1185. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1186.  
  1187. </div>
  1188.  
  1189. <h3 class="section">4.1 Centering Lines</h3>
  1190.  
  1191. <p>This script centers all lines of a file on a 80 columns width. 
  1192. To change that width, the number in <code>\{...\}</code> must be
  1193. replaced, and the number of added spaces also must be changed.
  1194.  
  1195.    <p>Note how the buffer commands are used to separate parts in
  1196. the regular expressions to be matched—this is a common
  1197. technique.
  1198.  
  1199. <!-- start- -->
  1200. <pre class="example">     #!/usr/bin/sed -f
  1201.      
  1202.      # Put 80 spaces in the buffer
  1203.      1 {
  1204.        x
  1205.        s/^$/          /
  1206.        s/^.*$/&&&&&&&&/
  1207.        x
  1208.      }
  1209.      
  1210.      # del leading and trailing spaces
  1211.      y/<kbd>tab</kbd>/ /
  1212.      s/^ *//
  1213.      s/ *$//
  1214.      
  1215.      # add a newline and 80 spaces to end of line
  1216.      G
  1217.      
  1218.      # keep first 81 chars (80 + a newline)
  1219.      s/^\(.\{81\}\).*$/\1/
  1220.      
  1221.      # \2 matches half of the spaces, which are moved to the beginning
  1222.      s/^\(.*\)\n\(.*\)\2/\2\1/
  1223. </pre>
  1224.    <!-- end- -->
  1225. <div class="node">
  1226. <p><hr>
  1227. <a name="Increment-a-number"></a>
  1228. Next: <a rel="next" accesskey="n" href="#Rename-files-to-lower-case">Rename files to lower case</a>,
  1229. Previous: <a rel="previous" accesskey="p" href="#Centering-lines">Centering lines</a>,
  1230. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1231.  
  1232. </div>
  1233.  
  1234. <h3 class="section">4.2 Increment a Number</h3>
  1235.  
  1236. <p>This script is one of a few that demonstrate how to do arithmetic
  1237. in <samp><span class="command">sed</span></samp>.  This is indeed possible,<a rel="footnote" href="#fn-6" name="fnd-6"><sup>6</sup></a> but must be done manually.
  1238.  
  1239.    <p>To increment one number you just add 1 to last digit, replacing
  1240. it by the following digit.  There is one exception: when the digit
  1241. is a nine the previous digits must be also incremented until you
  1242. don't have a nine.
  1243.  
  1244.    <p>This solution by Bruno Haible is very clever and smart because
  1245. it uses a single buffer; if you don't have this limitation, the
  1246. algorithm used in <a href="#cat-_002dn">Numbering lines</a>, is faster. 
  1247. It works by replacing trailing nines with an underscore, then
  1248. using multiple <code>s</code> commands to increment the last digit,
  1249. and then again substituting underscores with zeros.
  1250.  
  1251. <!-- start- -->
  1252. <pre class="example">     #!/usr/bin/sed -f
  1253.      
  1254.      /[^0-9]/ d
  1255.      
  1256.      # replace all leading 9s by _ (any other character except digits, could
  1257.      # be used)
  1258.      :d
  1259.      s/9\(_*\)$/_\1/
  1260.      td
  1261.      
  1262.      # incr last digit only.  The first line adds a most-significant
  1263.      # digit of 1 if we have to add a digit.
  1264.      #
  1265.      # The <code>tn</code> commands are not necessary, but make the thing
  1266.      # faster
  1267.      
  1268.      s/^\(_*\)$/1\1/; tn
  1269.      s/8\(_*\)$/9\1/; tn
  1270.      s/7\(_*\)$/8\1/; tn
  1271.      s/6\(_*\)$/7\1/; tn
  1272.      s/5\(_*\)$/6\1/; tn
  1273.      s/4\(_*\)$/5\1/; tn
  1274.      s/3\(_*\)$/4\1/; tn
  1275.      s/2\(_*\)$/3\1/; tn
  1276.      s/1\(_*\)$/2\1/; tn
  1277.      s/0\(_*\)$/1\1/; tn
  1278.      
  1279.      :n
  1280.      y/_/0/
  1281. </pre>
  1282.    <!-- end- -->
  1283. <div class="node">
  1284. <p><hr>
  1285. <a name="Rename-files-to-lower-case"></a>
  1286. Next: <a rel="next" accesskey="n" href="#Print-bash-environment">Print bash environment</a>,
  1287. Previous: <a rel="previous" accesskey="p" href="#Increment-a-number">Increment a number</a>,
  1288. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1289.  
  1290. </div>
  1291.  
  1292. <h3 class="section">4.3 Rename Files to Lower Case</h3>
  1293.  
  1294. <p>This is a pretty strange use of <samp><span class="command">sed</span></samp>.  We transform text, and
  1295. transform it to be shell commands, then just feed them to shell. 
  1296. Don't worry, even worse hacks are done when using <samp><span class="command">sed</span></samp>; I have
  1297. seen a script converting the output of <samp><span class="command">date</span></samp> into a <samp><span class="command">bc</span></samp>
  1298. program!
  1299.  
  1300.    <p>The main body of this is the <samp><span class="command">sed</span></samp> script, which remaps the name
  1301. from lower to upper (or vice-versa) and even checks out
  1302. if the remapped name is the same as the original name. 
  1303. Note how the script is parameterized using shell
  1304. variables and proper quoting.
  1305.  
  1306. <!-- start- -->
  1307. <pre class="example">     #! /bin/sh
  1308.      # rename files to lower/upper case...
  1309.      #
  1310.      # usage:
  1311.      #    move-to-lower *
  1312.      #    move-to-upper *
  1313.      # or
  1314.      #    move-to-lower -R .
  1315.      #    move-to-upper -R .
  1316.      #
  1317.      
  1318.      help()
  1319.      {
  1320.          cat << eof
  1321.      Usage: $0 [-n] [-r] [-h] files...
  1322.      
  1323.      -n      do nothing, only see what would be done
  1324.      -R      recursive (use find)
  1325.      -h      this message
  1326.      files   files to remap to lower case
  1327.      
  1328.      Examples:
  1329.             $0 -n *        (see if everything is ok, then...)
  1330.             $0 *
  1331.      
  1332.             $0 -R .
  1333.      
  1334.      eof
  1335.      }
  1336.      
  1337.      apply_cmd='sh'
  1338.      finder='echo "$¨| tr " " "\n"'
  1339.      files_only=
  1340.      
  1341.      while :
  1342.      do
  1343.          case "$1" in
  1344.              -n) apply_cmd='cat' ;;
  1345.              -R) finder='find "$¨-type f';;
  1346.              -h) help ; exit 1 ;;
  1347.              *) break ;;
  1348.          esac
  1349.          shift
  1350.      done
  1351.      
  1352.      if [ -z "$1" ]; then
  1353.              echo Usage: $0 [-h] [-n] [-r] files...
  1354.              exit 1
  1355.      fi
  1356.      
  1357.      LOWER='abcdefghijklmnopqrstuvwxyz'
  1358.      UPPER='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  1359.      
  1360.      case `basename $0` in
  1361.              *upper*) TO=$UPPER; FROM=$LOWER ;;
  1362.              *)       FROM=$UPPER; TO=$LOWER ;;
  1363.      esac
  1364.      
  1365.      eval $finder | sed -n '
  1366.      
  1367.      # remove all trailing slashes
  1368.      s/\/*$//
  1369.      
  1370.      # add ./ if there is no path, only a filename
  1371.      /\//! s/^/.\//
  1372.      
  1373.      # save path+filename
  1374.      h
  1375.      
  1376.      # remove path
  1377.      s/.*\///
  1378.      
  1379.      # do conversion only on filename
  1380.      y/'$FROM'/'$TO'/
  1381.      
  1382.      # now line contains original path+file, while
  1383.      # hold space contains the new filename
  1384.      x
  1385.      
  1386.      # add converted file name to line, which now contains
  1387.      # path/file-name\nconverted-file-name
  1388.      G
  1389.      
  1390.      # check if converted file name is equal to original file name,
  1391.      # if it is, do not print nothing
  1392.      /^.*\/\(.*\)\n\1/b
  1393.      
  1394.      # now, transform path/fromfile\n, into
  1395.      # mv path/fromfile path/tofile and print it
  1396.      s/^\(.*\/\)\(.*\)\n\(.*\)$/mv \1\2 \1\3/p
  1397.      
  1398.      ' | $apply_cmd
  1399. </pre>
  1400.    <!-- end- -->
  1401. <div class="node">
  1402. <p><hr>
  1403. <a name="Print-bash-environment"></a>
  1404. Next: <a rel="next" accesskey="n" href="#Reverse-chars-of-lines">Reverse chars of lines</a>,
  1405. Previous: <a rel="previous" accesskey="p" href="#Rename-files-to-lower-case">Rename files to lower case</a>,
  1406. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1407.  
  1408. </div>
  1409.  
  1410. <h3 class="section">4.4 Print <samp><span class="command">bash</span></samp> Environment</h3>
  1411.  
  1412. <p>This script strips the definition of the shell functions
  1413. from the output of the <samp><span class="command">set</span></samp> Bourne-shell command.
  1414.  
  1415. <!-- start- -->
  1416. <pre class="example">     #!/bin/sh
  1417.      
  1418.      set | sed -n '
  1419.      :x
  1420.      
  1421.      # if no occurrence of `<samp><span class="samp">=()</span></samp>' print and load next line
  1422.      /=()/! { p; b; }
  1423.      / () $/! { p; b; }
  1424.      
  1425.      # possible start of functions section
  1426.      # save the line in case this is a var like FOO="() "
  1427.      h
  1428.      
  1429.      # if the next line has a brace, we quit because
  1430.      # nothing comes after functions
  1431.      n
  1432.      /^{/ q
  1433.      
  1434.      # print the old line
  1435.      x; p
  1436.      
  1437.      # work on the new line now
  1438.      x; bx
  1439.      '
  1440. </pre>
  1441.    <!-- end- -->
  1442. <div class="node">
  1443. <p><hr>
  1444. <a name="Reverse-chars-of-lines"></a>
  1445. Next: <a rel="next" accesskey="n" href="#tac">tac</a>,
  1446. Previous: <a rel="previous" accesskey="p" href="#Print-bash-environment">Print bash environment</a>,
  1447. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1448.  
  1449. </div>
  1450.  
  1451. <h3 class="section">4.5 Reverse Characters of Lines</h3>
  1452.  
  1453. <p>This script can be used to reverse the position of characters
  1454. in lines.  The technique moves two characters at a time, hence
  1455. it is faster than more intuitive implementations.
  1456.  
  1457.    <p>Note the <code>tx</code> command before the definition of the label. 
  1458. This is often needed to reset the flag that is tested by
  1459. the <code>t</code> command.
  1460.  
  1461.    <p>Imaginative readers will find uses for this script.  An example
  1462. is reversing the output of <samp><span class="command">banner</span></samp>.<a rel="footnote" href="#fn-7" name="fnd-7"><sup>7</sup></a>
  1463.  
  1464. <!-- start- -->
  1465. <pre class="example">     #!/usr/bin/sed -f
  1466.      
  1467.      /../! b
  1468.      
  1469.      # Reverse a line.  Begin embedding the line between two newlines
  1470.      s/^.*$/\
  1471.      &\
  1472.      /
  1473.      
  1474.      # Move first character at the end.  The regexp matches until
  1475.      # there are zero or one characters between the markers
  1476.      tx
  1477.      :x
  1478.      s/\(\n.\)\(.*\)\(.\n\)/\3\2\1/
  1479.      tx
  1480.      
  1481.      # Remove the newline markers
  1482.      s/\n//g
  1483. </pre>
  1484.    <!-- end- -->
  1485. <div class="node">
  1486. <p><hr>
  1487. <a name="tac"></a>
  1488. Next: <a rel="next" accesskey="n" href="#cat-_002dn">cat -n</a>,
  1489. Previous: <a rel="previous" accesskey="p" href="#Reverse-chars-of-lines">Reverse chars of lines</a>,
  1490. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1491.  
  1492. </div>
  1493.  
  1494. <h3 class="section">4.6 Reverse Lines of Files</h3>
  1495.  
  1496. <p>This one begins a series of totally useless (yet interesting)
  1497. scripts emulating various Unix commands.  This, in particular,
  1498. is a <samp><span class="command">tac</span></samp> workalike.
  1499.  
  1500.    <p>Note that on implementations other than <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  1501. this script might easily overflow internal buffers.
  1502.  
  1503. <!-- start- -->
  1504. <pre class="example">     #!/usr/bin/sed -nf
  1505.      
  1506.      # reverse all lines of input, i.e. first line became last, ...
  1507.      
  1508.      # from the second line, the buffer (which contains all previous lines)
  1509.      # is *appended* to current line, so, the order will be reversed
  1510.      1! G
  1511.      
  1512.      # on the last line we're done -- print everything
  1513.      $ p
  1514.      
  1515.      # store everything on the buffer again
  1516.      h
  1517. </pre>
  1518.    <!-- end- -->
  1519. <div class="node">
  1520. <p><hr>
  1521. <a name="cat--n"></a>
  1522. <a name="cat-_002dn"></a>
  1523. Next: <a rel="next" accesskey="n" href="#cat-_002db">cat -b</a>,
  1524. Previous: <a rel="previous" accesskey="p" href="#tac">tac</a>,
  1525. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1526.  
  1527. </div>
  1528.  
  1529. <h3 class="section">4.7 Numbering Lines</h3>
  1530.  
  1531. <p>This script replaces `<samp><span class="samp">cat -n</span></samp>'; in fact it formats its output
  1532. exactly like <acronym>GNU</acronym> <samp><span class="command">cat</span></samp> does.
  1533.  
  1534.    <p>Of course this is completely useless and for two reasons:  first,
  1535. because somebody else did it in C, second, because the following
  1536. Bourne-shell script could be used for the same purpose and would
  1537. be much faster:
  1538.  
  1539. <!-- start- -->
  1540. <pre class="example">     #! /bin/sh
  1541.      sed -e "=" $@ | sed -e '
  1542.        s/^/      /
  1543.        N
  1544.        s/^ *\(......\)\n/\1  /
  1545.      '
  1546. </pre>
  1547.    <!-- end- -->
  1548. <p>It uses <samp><span class="command">sed</span></samp> to print the line number, then groups lines two
  1549. by two using <code>N</code>.  Of course, this script does not teach as much as
  1550. the one presented below.
  1551.  
  1552.    <p>The algorithm used for incrementing uses both buffers, so the line
  1553. is printed as soon as possible and then discarded.  The number
  1554. is split so that changing digits go in a buffer and unchanged ones go
  1555. in the other; the changed digits are modified in a single step
  1556. (using a <code>y</code> command).  The line number for the next line
  1557. is then composed and stored in the hold space, to be used in the
  1558. next iteration.
  1559.  
  1560. <!-- start- -->
  1561. <pre class="example">     #!/usr/bin/sed -nf
  1562.      
  1563.      # Prime the pump on the first line
  1564.      x
  1565.      /^$/ s/^.*$/1/
  1566.      
  1567.      # Add the correct line number before the pattern
  1568.      G
  1569.      h
  1570.      
  1571.      # Format it and print it
  1572.      s/^/      /
  1573.      s/^ *\(......\)\n/\1  /p
  1574.      
  1575.      # Get the line number from hold space; add a zero
  1576.      # if we're going to add a digit on the next line
  1577.      g
  1578.      s/\n.*$//
  1579.      /^9*$/ s/^/0/
  1580.      
  1581.      # separate changing/unchanged digits with an x
  1582.      s/.9*$/x&/
  1583.      
  1584.      # keep changing digits in hold space
  1585.      h
  1586.      s/^.*x//
  1587.      y/0123456789/1234567890/
  1588.      x
  1589.      
  1590.      # keep unchanged digits in pattern space
  1591.      s/x.*$//
  1592.      
  1593.      # compose the new number, remove the newline implicitly added by G
  1594.      G
  1595.      s/\n//
  1596.      h
  1597. </pre>
  1598.    <!-- end- -->
  1599. <div class="node">
  1600. <p><hr>
  1601. <a name="cat--b"></a>
  1602. <a name="cat-_002db"></a>
  1603. Next: <a rel="next" accesskey="n" href="#wc-_002dc">wc -c</a>,
  1604. Previous: <a rel="previous" accesskey="p" href="#cat-_002dn">cat -n</a>,
  1605. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1606.  
  1607. </div>
  1608.  
  1609. <h3 class="section">4.8 Numbering Non-blank Lines</h3>
  1610.  
  1611. <p>Emulating `<samp><span class="samp">cat -b</span></samp>' is almost the same as `<samp><span class="samp">cat -n</span></samp>'—we only
  1612. have to select which lines are to be numbered and which are not.
  1613.  
  1614.    <p>The part that is common to this script and the previous one is
  1615. not commented to show how important it is to comment <samp><span class="command">sed</span></samp>
  1616. scripts properly...
  1617.  
  1618. <!-- start- -->
  1619. <pre class="example">     #!/usr/bin/sed -nf
  1620.      
  1621.      /^$/ {
  1622.        p
  1623.        b
  1624.      }
  1625.      
  1626.      # Same as cat -n from now
  1627.      x
  1628.      /^$/ s/^.*$/1/
  1629.      G
  1630.      h
  1631.      s/^/      /
  1632.      s/^ *\(......\)\n/\1  /p
  1633.      x
  1634.      s/\n.*$//
  1635.      /^9*$/ s/^/0/
  1636.      s/.9*$/x&/
  1637.      h
  1638.      s/^.*x//
  1639.      y/0123456789/1234567890/
  1640.      x
  1641.      s/x.*$//
  1642.      G
  1643.      s/\n//
  1644.      h
  1645. </pre>
  1646.    <!-- end- -->
  1647. <div class="node">
  1648. <p><hr>
  1649. <a name="wc--c"></a>
  1650. <a name="wc-_002dc"></a>
  1651. Next: <a rel="next" accesskey="n" href="#wc-_002dw">wc -w</a>,
  1652. Previous: <a rel="previous" accesskey="p" href="#cat-_002db">cat -b</a>,
  1653. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1654.  
  1655. </div>
  1656.  
  1657. <h3 class="section">4.9 Counting Characters</h3>
  1658.  
  1659. <p>This script shows another way to do arithmetic with <samp><span class="command">sed</span></samp>. 
  1660. In this case we have to add possibly large numbers, so implementing
  1661. this by successive increments would not be feasible (and possibly
  1662. even more complicated to contrive than this script).
  1663.  
  1664.    <p>The approach is to map numbers to letters, kind of an abacus
  1665. implemented with <samp><span class="command">sed</span></samp>.  `<samp><span class="samp">a</span></samp>'s are units, `<samp><span class="samp">b</span></samp>'s are
  1666. tenths and so on: we simply add the number of characters
  1667. on the current line as units, and then propagate the carry
  1668. to tenths, hundredths, and so on.
  1669.  
  1670.    <p>As usual, running totals are kept in hold space.
  1671.  
  1672.    <p>On the last line, we convert the abacus form back to decimal. 
  1673. For the sake of variety, this is done with a loop rather than
  1674. with some 80 <code>s</code> commands<a rel="footnote" href="#fn-8" name="fnd-8"><sup>8</sup></a>: first we
  1675. convert units, removing `<samp><span class="samp">a</span></samp>'s from the number; then we
  1676. rotate letters so that tenths become `<samp><span class="samp">a</span></samp>'s, and so on
  1677. until no more letters remain.
  1678.  
  1679. <!-- start- -->
  1680. <pre class="example">     #!/usr/bin/sed -nf
  1681.      
  1682.      # Add n+1 a's to hold space (+1 is for the newline)
  1683.      s/./a/g
  1684.      H
  1685.      x
  1686.      s/\n/a/
  1687.      
  1688.      # Do the carry.  The t's and b's are not necessary,
  1689.      # but they do speed up the thing
  1690.      t a
  1691.      : a;  s/aaaaaaaaaa/b/g; t b; b done
  1692.      : b;  s/bbbbbbbbbb/c/g; t c; b done
  1693.      : c;  s/cccccccccc/d/g; t d; b done
  1694.      : d;  s/dddddddddd/e/g; t e; b done
  1695.      : e;  s/eeeeeeeeee/f/g; t f; b done
  1696.      : f;  s/ffffffffff/g/g; t g; b done
  1697.      : g;  s/gggggggggg/h/g; t h; b done
  1698.      : h;  s/hhhhhhhhhh//g
  1699.      
  1700.      : done
  1701.      $! {
  1702.        h
  1703.        b
  1704.      }
  1705.      
  1706.      # On the last line, convert back to decimal
  1707.      
  1708.      : loop
  1709.      /a/! s/[b-h]*/&0/
  1710.      s/aaaaaaaaa/9/
  1711.      s/aaaaaaaa/8/
  1712.      s/aaaaaaa/7/
  1713.      s/aaaaaa/6/
  1714.      s/aaaaa/5/
  1715.      s/aaaa/4/
  1716.      s/aaa/3/
  1717.      s/aa/2/
  1718.      s/a/1/
  1719.      
  1720.      : next
  1721.      y/bcdefgh/abcdefg/
  1722.      /[a-h]/ b loop
  1723.      p
  1724. </pre>
  1725.    <!-- end- -->
  1726. <div class="node">
  1727. <p><hr>
  1728. <a name="wc--w"></a>
  1729. <a name="wc-_002dw"></a>
  1730. Next: <a rel="next" accesskey="n" href="#wc-_002dl">wc -l</a>,
  1731. Previous: <a rel="previous" accesskey="p" href="#wc-_002dc">wc -c</a>,
  1732. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1733.  
  1734. </div>
  1735.  
  1736. <h3 class="section">4.10 Counting Words</h3>
  1737.  
  1738. <p>This script is almost the same as the previous one, once each
  1739. of the words on the line is converted to a single `<samp><span class="samp">a</span></samp>'
  1740. (in the previous script each letter was changed to an `<samp><span class="samp">a</span></samp>').
  1741.  
  1742.    <p>It is interesting that real <samp><span class="command">wc</span></samp> programs have optimized
  1743. loops for `<samp><span class="samp">wc -c</span></samp>', so they are much slower at counting
  1744. words rather than characters.  This script's bottleneck,
  1745. instead, is arithmetic, and hence the word-counting one
  1746. is faster (it has to manage smaller numbers).
  1747.  
  1748.    <p>Again, the common parts are not commented to show the importance
  1749. of commenting <samp><span class="command">sed</span></samp> scripts.
  1750.  
  1751. <!-- start- -->
  1752. <pre class="example">     #!/usr/bin/sed -nf
  1753.      
  1754.      # Convert words to a's
  1755.      s/[ <kbd>tab</kbd>][ <kbd>tab</kbd>]*/ /g
  1756.      s/^/ /
  1757.      s/ [^ ][^ ]*/a /g
  1758.      s/ //g
  1759.      
  1760.      # Append them to hold space
  1761.      H
  1762.      x
  1763.      s/\n//
  1764.      
  1765.      # From here on it is the same as in wc -c.
  1766.      /aaaaaaaaaa/! bx;   s/aaaaaaaaaa/b/g
  1767.      /bbbbbbbbbb/! bx;   s/bbbbbbbbbb/c/g
  1768.      /cccccccccc/! bx;   s/cccccccccc/d/g
  1769.      /dddddddddd/! bx;   s/dddddddddd/e/g
  1770.      /eeeeeeeeee/! bx;   s/eeeeeeeeee/f/g
  1771.      /ffffffffff/! bx;   s/ffffffffff/g/g
  1772.      /gggggggggg/! bx;   s/gggggggggg/h/g
  1773.      s/hhhhhhhhhh//g
  1774.      :x
  1775.      $! { h; b; }
  1776.      :y
  1777.      /a/! s/[b-h]*/&0/
  1778.      s/aaaaaaaaa/9/
  1779.      s/aaaaaaaa/8/
  1780.      s/aaaaaaa/7/
  1781.      s/aaaaaa/6/
  1782.      s/aaaaa/5/
  1783.      s/aaaa/4/
  1784.      s/aaa/3/
  1785.      s/aa/2/
  1786.      s/a/1/
  1787.      y/bcdefgh/abcdefg/
  1788.      /[a-h]/ by
  1789.      p
  1790. </pre>
  1791.    <!-- end- -->
  1792. <div class="node">
  1793. <p><hr>
  1794. <a name="wc--l"></a>
  1795. <a name="wc-_002dl"></a>
  1796. Next: <a rel="next" accesskey="n" href="#head">head</a>,
  1797. Previous: <a rel="previous" accesskey="p" href="#wc-_002dw">wc -w</a>,
  1798. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1799.  
  1800. </div>
  1801.  
  1802. <h3 class="section">4.11 Counting Lines</h3>
  1803.  
  1804. <p>No strange things are done now, because <samp><span class="command">sed</span></samp> gives us
  1805. `<samp><span class="samp">wc -l</span></samp>' functionality for free!!! Look:
  1806.  
  1807. <!-- start- -->
  1808. <pre class="example">     #!/usr/bin/sed -nf
  1809.      $=
  1810. </pre>
  1811.    <!-- end- -->
  1812. <div class="node">
  1813. <p><hr>
  1814. <a name="head"></a>
  1815. Next: <a rel="next" accesskey="n" href="#tail">tail</a>,
  1816. Previous: <a rel="previous" accesskey="p" href="#wc-_002dl">wc -l</a>,
  1817. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1818.  
  1819. </div>
  1820.  
  1821. <h3 class="section">4.12 Printing the First Lines</h3>
  1822.  
  1823. <p>This script is probably the simplest useful <samp><span class="command">sed</span></samp> script. 
  1824. It displays the first 10 lines of input; the number of displayed
  1825. lines is right before the <code>q</code> command.
  1826.  
  1827. <!-- start- -->
  1828. <pre class="example">     #!/usr/bin/sed -f
  1829.      10q
  1830. </pre>
  1831.    <!-- end- -->
  1832. <div class="node">
  1833. <p><hr>
  1834. <a name="tail"></a>
  1835. Next: <a rel="next" accesskey="n" href="#uniq">uniq</a>,
  1836. Previous: <a rel="previous" accesskey="p" href="#head">head</a>,
  1837. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1838.  
  1839. </div>
  1840.  
  1841. <h3 class="section">4.13 Printing the Last Lines</h3>
  1842.  
  1843. <p>Printing the last <var>n</var> lines rather than the first is more complex
  1844. but indeed possible.  <var>n</var> is encoded in the second line, before
  1845. the bang character.
  1846.  
  1847.    <p>This script is similar to the <samp><span class="command">tac</span></samp> script in that it keeps the
  1848. final output in the hold space and prints it at the end:
  1849.  
  1850. <!-- start- -->
  1851. <pre class="example">     #!/usr/bin/sed -nf
  1852.      
  1853.      1! {; H; g; }
  1854.      1,10 !s/[^\n]*\n//
  1855.      $p
  1856.      h
  1857. </pre>
  1858.    <!-- end- -->
  1859. <p>Mainly, the scripts keeps a window of 10 lines and slides it
  1860. by adding a line and deleting the oldest (the substitution command
  1861. on the second line works like a <code>D</code> command but does not
  1862. restart the loop).
  1863.  
  1864.    <p>The “sliding window” technique is a very powerful way to write
  1865. efficient and complex <samp><span class="command">sed</span></samp> scripts, because commands like
  1866. <code>P</code> would require a lot of work if implemented manually.
  1867.  
  1868.    <p>To introduce the technique, which is fully demonstrated in the
  1869. rest of this chapter and is based on the <code>N</code>, <code>P</code>
  1870. and <code>D</code> commands, here is an implementation of <samp><span class="command">tail</span></samp>
  1871. using a simple “sliding window.”
  1872.  
  1873.    <p>This looks complicated but in fact the working is the same as
  1874. the last script: after we have kicked in the appropriate number
  1875. of lines, however, we stop using the hold space to keep inter-line
  1876. state, and instead use <code>N</code> and <code>D</code> to slide pattern
  1877. space by one line:
  1878.  
  1879. <!-- start- -->
  1880. <pre class="example">     #!/usr/bin/sed -f
  1881.      
  1882.      1h
  1883.      2,10 {; H; g; }
  1884.      $q
  1885.      1,9d
  1886.      N
  1887.      D
  1888. </pre>
  1889.    <!-- end- -->
  1890. <div class="node">
  1891. <p><hr>
  1892. <a name="uniq"></a>
  1893. Next: <a rel="next" accesskey="n" href="#uniq-_002dd">uniq -d</a>,
  1894. Previous: <a rel="previous" accesskey="p" href="#tail">tail</a>,
  1895. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1896.  
  1897. </div>
  1898.  
  1899. <h3 class="section">4.14 Make Duplicate Lines Unique</h3>
  1900.  
  1901. <p>This is an example of the art of using the <code>N</code>, <code>P</code>
  1902. and <code>D</code> commands, probably the most difficult to master.
  1903.  
  1904. <!-- start- -->
  1905. <pre class="example">     #!/usr/bin/sed -f
  1906.      h
  1907.      
  1908.      :b
  1909.      # On the last line, print and exit
  1910.      $b
  1911.      N
  1912.      /^\(.*\)\n\1$/ {
  1913.          # The two lines are identical.  Undo the effect of
  1914.          # the n command.
  1915.          g
  1916.          bb
  1917.      }
  1918.      
  1919.      # If the <code>N</code> command had added the last line, print and exit
  1920.      $b
  1921.      
  1922.      # The lines are different; print the first and go
  1923.      # back working on the second.
  1924.      P
  1925.      D
  1926. </pre>
  1927.    <!-- end- -->
  1928. <p>As you can see, we mantain a 2-line window using <code>P</code> and <code>D</code>. 
  1929. This technique is often used in advanced <samp><span class="command">sed</span></samp> scripts.
  1930.  
  1931. <div class="node">
  1932. <p><hr>
  1933. <a name="uniq--d"></a>
  1934. <a name="uniq-_002dd"></a>
  1935. Next: <a rel="next" accesskey="n" href="#uniq-_002du">uniq -u</a>,
  1936. Previous: <a rel="previous" accesskey="p" href="#uniq">uniq</a>,
  1937. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1938.  
  1939. </div>
  1940.  
  1941. <h3 class="section">4.15 Print Duplicated Lines of Input</h3>
  1942.  
  1943. <p>This script prints only duplicated lines, like `<samp><span class="samp">uniq -d</span></samp>'.
  1944.  
  1945. <!-- start- -->
  1946. <pre class="example">     #!/usr/bin/sed -nf
  1947.      
  1948.      $b
  1949.      N
  1950.      /^\(.*\)\n\1$/ {
  1951.          # Print the first of the duplicated lines
  1952.          s/.*\n//
  1953.          p
  1954.      
  1955.          # Loop until we get a different line
  1956.          :b
  1957.          $b
  1958.          N
  1959.          /^\(.*\)\n\1$/ {
  1960.              s/.*\n//
  1961.              bb
  1962.          }
  1963.      }
  1964.      
  1965.      # The last line cannot be followed by duplicates
  1966.      $b
  1967.      
  1968.      # Found a different one.  Leave it alone in the pattern space
  1969.      # and go back to the top, hunting its duplicates
  1970.      D
  1971. </pre>
  1972.    <!-- end- -->
  1973. <div class="node">
  1974. <p><hr>
  1975. <a name="uniq--u"></a>
  1976. <a name="uniq-_002du"></a>
  1977. Next: <a rel="next" accesskey="n" href="#cat-_002ds">cat -s</a>,
  1978. Previous: <a rel="previous" accesskey="p" href="#uniq-_002dd">uniq -d</a>,
  1979. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  1980.  
  1981. </div>
  1982.  
  1983. <h3 class="section">4.16 Remove All Duplicated Lines</h3>
  1984.  
  1985. <p>This script prints only unique lines, like `<samp><span class="samp">uniq -u</span></samp>'.
  1986.  
  1987. <!-- start- -->
  1988. <pre class="example">     #!/usr/bin/sed -f
  1989.      
  1990.      # Search for a duplicate line --- until that, print what you find.
  1991.      $b
  1992.      N
  1993.      /^\(.*\)\n\1$/ ! {
  1994.          P
  1995.          D
  1996.      }
  1997.      
  1998.      :c
  1999.      # Got two equal lines in pattern space.  At the
  2000.      # end of the file we simply exit
  2001.      $d
  2002.      
  2003.      # Else, we keep reading lines with <code>N</code> until we
  2004.      # find a different one
  2005.      s/.*\n//
  2006.      N
  2007.      /^\(.*\)\n\1$/ {
  2008.          bc
  2009.      }
  2010.      
  2011.      # Remove the last instance of the duplicate line
  2012.      # and go back to the top
  2013.      D
  2014. </pre>
  2015.    <!-- end- -->
  2016. <div class="node">
  2017. <p><hr>
  2018. <a name="cat--s"></a>
  2019. <a name="cat-_002ds"></a>
  2020. Previous: <a rel="previous" accesskey="p" href="#uniq-_002du">uniq -u</a>,
  2021. Up: <a rel="up" accesskey="u" href="#Examples">Examples</a>
  2022.  
  2023. </div>
  2024.  
  2025. <h3 class="section">4.17 Squeezing Blank Lines</h3>
  2026.  
  2027. <p>As a final example, here are three scripts, of increasing complexity
  2028. and speed, that implement the same function as `<samp><span class="samp">cat -s</span></samp>', that is
  2029. squeezing blank lines.
  2030.  
  2031.    <p>The first leaves a blank line at the beginning and end if there are
  2032. some already.
  2033.  
  2034. <!-- start- -->
  2035. <pre class="example">     #!/usr/bin/sed -f
  2036.      
  2037.      # on empty lines, join with next
  2038.      # Note there is a star in the regexp
  2039.      :x
  2040.      /^\n*$/ {
  2041.      N
  2042.      bx
  2043.      }
  2044.      
  2045.      # now, squeeze all '\n', this can be also done by:
  2046.      # s/^\(\n\)*/\1/
  2047.      s/\n*/\
  2048.      /
  2049. </pre>
  2050.    <!-- end- -->
  2051. <p>This one is a bit more complex and removes all empty lines
  2052. at the beginning.  It does leave a single blank line at end
  2053. if one was there.
  2054.  
  2055. <!-- start- -->
  2056. <pre class="example">     #!/usr/bin/sed -f
  2057.      
  2058.      # delete all leading empty lines
  2059.      1,/^./{
  2060.      /./!d
  2061.      }
  2062.      
  2063.      # on an empty line we remove it and all the following
  2064.      # empty lines, but one
  2065.      :x
  2066.      /./!{
  2067.      N
  2068.      s/^\n$//
  2069.      tx
  2070.      }
  2071. </pre>
  2072.    <!-- end- -->
  2073. <p>This removes leading and trailing blank lines.  It is also the
  2074. fastest.  Note that loops are completely done with <code>n</code> and
  2075. <code>b</code>, without exploting the fact that <samp><span class="command">sed</span></samp> cycles back
  2076. to the top of the script automatically at the end of a line.
  2077.  
  2078. <!-- start- -->
  2079. <pre class="example">     #!/usr/bin/sed -nf
  2080.      
  2081.      # delete all (leading) blanks
  2082.      /./!d
  2083.      
  2084.      # get here: so there is a non empty
  2085.      :x
  2086.      # print it
  2087.      p
  2088.      # get next
  2089.      n
  2090.      # got chars? print it again, etc...
  2091.      /./bx
  2092.      
  2093.      # no, don't have chars: got an empty line
  2094.      :z
  2095.      # get next, if last line we finish here so no trailing
  2096.      # empty lines are written
  2097.      n
  2098.      # also empty? then ignore it, and get next... this will
  2099.      # remove ALL empty lines
  2100.      /./!bz
  2101.      
  2102.      # all empty lines were deleted/ignored, but we have a non empty.  As
  2103.      # what we want to do is to squeeze, insert a blank line artificially
  2104.      i\
  2105.      
  2106.      bx
  2107. </pre>
  2108.    <!-- end- -->
  2109. <div class="node">
  2110. <p><hr>
  2111. <a name="Limitations"></a>
  2112. Next: <a rel="next" accesskey="n" href="#Other-Resources">Other Resources</a>,
  2113. Previous: <a rel="previous" accesskey="p" href="#Examples">Examples</a>,
  2114. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2115.  
  2116. </div>
  2117.  
  2118. <h2 class="chapter">5 <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>'s Limitations and Non-limitations</h2>
  2119.  
  2120. <p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-unlimited-line-length-214"></a><a name="index-Portability_002c-line-length-limitations-215"></a>For those who want to write portable <samp><span class="command">sed</span></samp> scripts,
  2121. be aware that some implementations have been known to
  2122. limit line lengths (for the pattern and hold spaces)
  2123. to be no more than 4000 bytes. 
  2124. The <span class="sc">posix</span> standard specifies that conforming <samp><span class="command">sed</span></samp>
  2125. implementations shall support at least 8192 byte line lengths. 
  2126. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> has no built-in limit on line length;
  2127. as long as it can <code>malloc()</code> more (virtual) memory,
  2128. you can feed or construct lines as long as you like.
  2129.  
  2130.    <p>However, recursion is used to handle subpatterns and indefinite
  2131. repetition.  This means that the available stack space may limit
  2132. the size of the buffer that can be processed by certain patterns.
  2133.  
  2134. <div class="node">
  2135. <p><hr>
  2136. <a name="Other-Resources"></a>
  2137. Next: <a rel="next" accesskey="n" href="#Reporting-Bugs">Reporting Bugs</a>,
  2138. Previous: <a rel="previous" accesskey="p" href="#Limitations">Limitations</a>,
  2139. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2140.  
  2141. </div>
  2142.  
  2143. <h2 class="chapter">6 Other Resources for Learning About <samp><span class="command">sed</span></samp></h2>
  2144.  
  2145. <p><a name="index-Additional-reading-about-_0040command_007bsed_007d-216"></a>In addition to several books that have been written about <samp><span class="command">sed</span></samp>
  2146. (either specifically or as chapters in books which discuss
  2147. shell programming), one can find out more about <samp><span class="command">sed</span></samp>
  2148. (including suggestions of a few books) from the FAQ
  2149. for the <code>sed-users</code> mailing list, available from any of:
  2150. <pre class="display">      <a href="http://www.student.northpark.edu/pemente/sed/sedfaq.html">http://www.student.northpark.edu/pemente/sed/sedfaq.html</a>
  2151.       <a href="http://sed.sf.net/grabbag/tutorials/sedfaq.html">http://sed.sf.net/grabbag/tutorials/sedfaq.html</a>
  2152. </pre>
  2153.    <p>Also of interest are
  2154. <a href="http://www.student.northpark.edu/pemente/sed/index.htm">http://www.student.northpark.edu/pemente/sed/index.htm</a>
  2155. and <a href="http://sed.sf.net/grabbag">http://sed.sf.net/grabbag</a>,
  2156. which include <samp><span class="command">sed</span></samp> tutorials and other <samp><span class="command">sed</span></samp>-related goodies.
  2157.  
  2158.    <p>The <code>sed-users</code> mailing list itself maintained by Sven Guckes. 
  2159. To subscribe, visit <a href="http://groups.yahoo.com">http://groups.yahoo.com</a> and search
  2160. for the <code>sed-users</code> mailing list.
  2161.  
  2162. <div class="node">
  2163. <p><hr>
  2164. <a name="Reporting-Bugs"></a>
  2165. Next: <a rel="next" accesskey="n" href="#Extended-regexps">Extended regexps</a>,
  2166. Previous: <a rel="previous" accesskey="p" href="#Other-Resources">Other Resources</a>,
  2167. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2168.  
  2169. </div>
  2170.  
  2171. <h2 class="chapter">7 Reporting Bugs</h2>
  2172.  
  2173. <p><a name="index-Bugs_002c-reporting-217"></a>Email bug reports to <a href="mailto:bonzini@gnu.org">bonzini@gnu.org</a>. 
  2174. Be sure to include the word “sed” somewhere in the <code>Subject:</code> field. 
  2175. Also, please include the output of `<samp><span class="samp">sed --version</span></samp>' in the body
  2176. of your report if at all possible.
  2177.  
  2178.    <p>Please do not send a bug report like this:
  2179.  
  2180. <pre class="example">     <i>while building frobme-1.3.4</i>
  2181.      $ configure
  2182.      error--> sed: file sedscr line 1: Unknown option to 's'
  2183. </pre>
  2184.    <p>If <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> doesn't configure your favorite package, take a
  2185. few extra minutes to identify the specific problem and make a stand-alone
  2186. test case.  Unlike other programs such as C compilers, making such test
  2187. cases for <samp><span class="command">sed</span></samp> is quite simple.
  2188.  
  2189.    <p>A stand-alone test case includes all the data necessary to perform the
  2190. test, and the specific invocation of <samp><span class="command">sed</span></samp> that causes the problem. 
  2191. The smaller a stand-alone test case is, the better.  A test case should
  2192. not involve something as far removed from <samp><span class="command">sed</span></samp> as “try to configure
  2193. frobme-1.3.4”.  Yes, that is in principle enough information to look
  2194. for the bug, but that is not a very practical prospect.
  2195.  
  2196.    <p>Here are a few commonly reported bugs that are not bugs.
  2197.  
  2198.      <dl>
  2199. <dt>`<samp><span class="samp">sed -n</span></samp>' and `<samp><span class="samp">s/</span><var>regex</var><span class="samp">/`<samp></span><span class="samp">replace</span><span class="samp"></samp>'/p</span></samp>'<dd><a name="index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-218"></a><a name="index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-219"></a>Some versions of <samp><span class="command">sed</span></samp> ignore the <code>p</code> (print) option of an <code>s</code> command
  2200. unless the <samp><span class="option">-n</span></samp> command-line option has been specified.  Other versions
  2201. always honor the <code>p</code> option. 
  2202. <!-- *** CHECK THE CURRENT STANDARD. ADR. -->
  2203. Both approaches are allowed by <span class="sc">posix</span>
  2204. and <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  2205. is the
  2206. better when you write complex scripts and also more intuitive, but
  2207. portable scripts should be written to work correctly with either
  2208. behavior.
  2209.  
  2210.      <br><dt><code>N</code> command on the last line<dd><a name="index-Portability_002c-_0040code_007bN_007d-command-on-the-last-line-220"></a><a name="index-Non_002dbugs_002c-_0040code_007bN_007d-command-on-the-last-line-221"></a>
  2211. Most versions of <samp><span class="command">sed</span></samp> exit without printing anything when
  2212. the <samp><span class="command">N</span></samp> command is issued on the last line of a file. 
  2213. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp> prints pattern space before exiting unless of course
  2214. the <samp><span class="command">-n</span></samp> command switch has been specified.  This choice is
  2215. by design.
  2216.  
  2217.      <p>For example, the behavior of
  2218.      <pre class="example">          sed N foo bar
  2219.      </pre>
  2220.      <p class="noindent">would depend on whether foo has an even or an odd number of
  2221. lines<a rel="footnote" href="#fn-9" name="fnd-9"><sup>9</sup></a>.  Or, when writing a script to read the
  2222. next few lines following a pattern match, traditional
  2223. implementations of <code>sed</code> would force you to write
  2224. something like
  2225.      <pre class="example">          /foo/{ $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N; $!N }
  2226.      </pre>
  2227.      <p class="noindent">instead of just
  2228.      <pre class="example">          /foo/{ N;N;N;N;N;N;N;N;N; }
  2229.      </pre>
  2230.      <p><a name="index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-_0040code_007bN_007d-command-222"></a>In any case, the simplest workaround is to use <code>$d;N</code> in
  2231. scripts that rely on the traditional behavior, or to set
  2232. the <code>POSIXLY_CORRECT</code> variable to a non-empty value.
  2233.  
  2234.      <br><dt>Regex syntax clashes<dd><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-223"></a><a name="index-Non_002dbugs_002c-regex-syntax-clashes-224"></a><samp><span class="command">sed</span></samp> uses the <span class="sc">posix</span> basic regular expression syntax.  According to
  2235. the standard, the meaning of some escape sequences is undefined in
  2236. this syntax;  notable in the case of <samp><span class="command">sed</span></samp> are <code>\|</code>,
  2237. <code>\+</code>, <code>\?</code>, <code>\`</code>, <code>\'</code>, <code>\<</code>,
  2238. <code>\></code>, <code>\b</code>, <code>\B</code>, <code>\w</code>, and <code>\W</code>.
  2239.  
  2240.      <p>As in all GNU programs that use <span class="sc">posix</span> basic regular expressions, <samp><span class="command">sed</span></samp>
  2241. interprets these escape sequences as special characters.  So, <code>x\+</code>
  2242. matches one or more occurrences of `<samp><span class="samp">x</span></samp>'.  <code>abc\|def</code> matches
  2243. either `<samp><span class="samp">abc</span></samp>' or `<samp><span class="samp">def</span></samp>'.
  2244.  
  2245.      <p>This syntax may cause problems when running scripts written for other
  2246. <samp><span class="command">sed</span></samp>s.  Some <samp><span class="command">sed</span></samp> programs have been written with the
  2247. assumption that <code>\|</code> and <code>\+</code> match the literal characters
  2248. <code>|</code> and <code>+</code>.  Such scripts must be modified by removing the
  2249. spurious backslashes if they are to be used with modern implementations
  2250. of <samp><span class="command">sed</span></samp>, like
  2251. <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>.
  2252.  
  2253.      <p><a name="index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-225"></a>In addition, this version of <samp><span class="command">sed</span></samp> supports several escape characters
  2254. (some of which are multi-character) to insert non-printable characters
  2255. in scripts (<code>\a</code>, <code>\c</code>, <code>\d</code>, <code>\o</code>, <code>\r</code>,
  2256. <code>\t</code>, <code>\v</code>, <code>\x</code>).  These can cause similar problems
  2257. with scripts written for other <samp><span class="command">sed</span></samp>s.
  2258.  
  2259.      <br><dt><samp><span class="option">-i</span></samp> clobbers read-only files<dd><a name="index-In_002dplace-editing-226"></a><a name="index-g_t_0040value_007bSSEDEXT_007d_002c-in_002dplace-editing-227"></a><a name="index-Non_002dbugs_002c-in_002dplace-editing-228"></a>
  2260. In short, `<samp><span class="samp">sed -i</span></samp>' will let you delete the contents of
  2261. a read-only file, and in general the <samp><span class="option">-i</span></samp> option
  2262. (see <a href="#Invoking-sed">Invocation</a>) lets you clobber
  2263. protected files.  This is not a bug, but rather a consequence
  2264. of how the Unix filesystem works.
  2265.  
  2266.      <p>The permissions on a file say what can happen to the data
  2267. in that file, while the permissions on a directory say what can
  2268. happen to the list of files in that directory.  `<samp><span class="samp">sed -i</span></samp>'
  2269. will not ever open for writing  a file that is already on disk. 
  2270. Rather, it will work on a temporary file that is finally renamed
  2271. to the original name: if you rename or delete files, you're actually
  2272. modifying the contents of the directory, so the operation depends on
  2273. the permissions of the directory, not of the file.  For this same
  2274. reason, <samp><span class="command">sed</span></samp> does not let you use <samp><span class="option">-i</span></samp> on a writeable file
  2275. in a read-only directory (but unbelievably nobody reports that as a
  2276. bug<small class="dots">...</small>). 
  2277. </dl>
  2278.  
  2279. <div class="node">
  2280. <p><hr>
  2281. <a name="Extended-regexps"></a>
  2282. Next: <a rel="next" accesskey="n" href="#Concept-Index">Concept Index</a>,
  2283. Previous: <a rel="previous" accesskey="p" href="#Reporting-Bugs">Reporting Bugs</a>,
  2284. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2285.  
  2286. </div>
  2287.  
  2288. <h2 class="appendix">Appendix A Extended regular expressions</h2>
  2289.  
  2290. <p><a name="index-Extended-regular-expressions_002c-syntax-229"></a>
  2291. The only difference between basic and extended regular expressions is in
  2292. the behavior of a few characters: `<samp><span class="samp">?</span></samp>', `<samp><span class="samp">+</span></samp>', parentheses,
  2293. and braces (`<samp><span class="samp">{}</span></samp>').  While basic regular expressions require
  2294. these to be escaped if you want them to behave as special characters,
  2295. when using extended regular expressions you must escape them if
  2296. you want them <em>to match a literal character</em>.
  2297.  
  2298. <p class="noindent">Examples:
  2299.      <dl>
  2300. <dt><code>abc?</code><dd>becomes `<samp><span class="samp">abc\?</span></samp>' when using extended regular expressions.  It matches
  2301. the literal string `<samp><span class="samp">abc?</span></samp>'.
  2302.  
  2303.      <br><dt><code>c\+</code><dd>becomes `<samp><span class="samp">c+</span></samp>' when using extended regular expressions.  It matches
  2304. one or more `<samp><span class="samp">c</span></samp>'s.
  2305.  
  2306.      <br><dt><code>a\{3,\}</code><dd>becomes `<samp><span class="samp">a{3,}</span></samp>' when using extended regular expressions.  It matches
  2307. three or more `<samp><span class="samp">a</span></samp>'s.
  2308.  
  2309.      <br><dt><code>\(abc\)\{2,3\}</code><dd>becomes `<samp><span class="samp">(abc){2,3}</span></samp>' when using extended regular expressions.  It
  2310. matches either `<samp><span class="samp">abcabc</span></samp>' or `<samp><span class="samp">abcabcabc</span></samp>'.
  2311.  
  2312.      <br><dt><code>\(abc*\)\1</code><dd>becomes `<samp><span class="samp">(abc*)\1</span></samp>' when using extended regular expressions. 
  2313. Backreferences must still be escaped when using extended regular
  2314. expressions. 
  2315. </dl>
  2316.  
  2317. <div class="node">
  2318. <p><hr>
  2319. <a name="Concept-Index"></a>
  2320. Next: <a rel="next" accesskey="n" href="#Command-and-Option-Index">Command and Option Index</a>,
  2321. Previous: <a rel="previous" accesskey="p" href="#Extended-regexps">Extended regexps</a>,
  2322. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2323.  
  2324. </div>
  2325.  
  2326. <h2 class="unnumbered">Concept Index</h2>
  2327.  
  2328. <p>This is a general index of all issues discussed in this manual, with the
  2329. exception of the <samp><span class="command">sed</span></samp> commands and command-line options.
  2330.  
  2331. <ul class="index-cp" compact>
  2332. <li><a href="#index-Additional-reading-about-_0040command_007bsed_007d-216">Additional reading about <samp><span class="command">sed</span></samp></a>: <a href="#Other-Resources">Other Resources</a></li>
  2333. <li><a href="#index-g_t_0040var_007baddr1_007d_002c_002bN-61"><var>addr1</var>,+N</a>: <a href="#Addresses">Addresses</a></li>
  2334. <li><a href="#index-g_t_0040var_007baddr1_007d_002c_007eN-62"><var>addr1</var>,~N</a>: <a href="#Addresses">Addresses</a></li>
  2335. <li><a href="#index-Address_002c-as-a-regular-expression-46">Address, as a regular expression</a>: <a href="#Addresses">Addresses</a></li>
  2336. <li><a href="#index-Address_002c-last-line-43">Address, last line</a>: <a href="#Addresses">Addresses</a></li>
  2337. <li><a href="#index-Address_002c-numeric-40">Address, numeric</a>: <a href="#Addresses">Addresses</a></li>
  2338. <li><a href="#index-Addresses_002c-in-_0040command_007bsed_007d-scripts-37">Addresses, in <samp><span class="command">sed</span></samp> scripts</a>: <a href="#Addresses">Addresses</a></li>
  2339. <li><a href="#index-Append-hold-space-to-pattern-space-168">Append hold space to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2340. <li><a href="#index-Append-next-input-line-to-pattern-space-153">Append next input line to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2341. <li><a href="#index-Append-pattern-space-to-hold-space-161">Append pattern space to hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2342. <li><a href="#index-Appending-text-after-a-line-124">Appending text after a line</a>: <a href="#Other-Commands">Other Commands</a></li>
  2343. <li><a href="#index-Backreferences_002c-in-regular-expressions-99">Backreferences, in regular expressions</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2344. <li><a href="#index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-failed-204">Branch to a label, if <code>s///</code> failed</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2345. <li><a href="#index-Branch-to-a-label_002c-if-_0040code_007bs_002f_002f_002f_007d-succeeded-180">Branch to a label, if <code>s///</code> succeeded</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2346. <li><a href="#index-Branch-to-a-label_002c-unconditionally-177">Branch to a label, unconditionally</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2347. <li><a href="#index-Buffer-spaces_002c-pattern-and-hold-75">Buffer spaces, pattern and hold</a>: <a href="#Data-Spaces">Data Spaces</a></li>
  2348. <li><a href="#index-Bugs_002c-reporting-217">Bugs, reporting</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2349. <li><a href="#index-Case_002dinsensitive-matching-118">Case-insensitive matching</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2350. <li><a href="#index-Caveat-_002d_002d_002d-_0023n-on-first-line-83">Caveat — #n on first line</a>: <a href="#Common-Commands">Common Commands</a></li>
  2351. <li><a href="#index-Command-groups-98">Command groups</a>: <a href="#Common-Commands">Common Commands</a></li>
  2352. <li><a href="#index-Comments_002c-in-scripts-80">Comments, in scripts</a>: <a href="#Common-Commands">Common Commands</a></li>
  2353. <li><a href="#index-Conditional-branch-205">Conditional branch</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2354. <li><a href="#index-Conditional-branch-181">Conditional branch</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2355. <li><a href="#index-Copy-hold-space-into-pattern-space-164">Copy hold space into pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2356. <li><a href="#index-Copy-pattern-space-into-hold-space-157">Copy pattern space into hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2357. <li><a href="#index-Delete-first-line-from-pattern-space-150">Delete first line from pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2358. <li><a href="#index-Disabling-autoprint_002c-from-command-line-11">Disabling autoprint, from command line</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2359. <li><a href="#index-empty-regular-expression-48">empty regular expression</a>: <a href="#Addresses">Addresses</a></li>
  2360. <li><a href="#index-Evaluate-Bourne_002dshell-commands-183">Evaluate Bourne-shell commands</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2361. <li><a href="#index-Evaluate-Bourne_002dshell-commands_002c-after-substitution-113">Evaluate Bourne-shell commands, after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2362. <li><a href="#index-Exchange-hold-space-with-pattern-space-171">Exchange hold space with pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2363. <li><a href="#index-Excluding-lines-67">Excluding lines</a>: <a href="#Addresses">Addresses</a></li>
  2364. <li><a href="#index-Extended-regular-expressions_002c-choosing-21">Extended regular expressions, choosing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2365. <li><a href="#index-Extended-regular-expressions_002c-syntax-229">Extended regular expressions, syntax</a>: <a href="#Extended-regexps">Extended regexps</a></li>
  2366. <li><a href="#index-Files-to-be-processed-as-input-33">Files to be processed as input</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2367. <li><a href="#index-Flow-of-control-in-scripts-173">Flow of control in scripts</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2368. <li><a href="#index-Global-substitution-104">Global substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2369. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-148"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stderr</span></samp> file</a>: <a href="#Other-Commands">Other Commands</a></li>
  2370. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstderr_007d-file-112"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stderr</span></samp> file</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2371. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-201"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stdin</span></samp> file</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2372. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdin_007d-file-144"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stdin</span></samp> file</a>: <a href="#Other-Commands">Other Commands</a></li>
  2373. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-230"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stdout</span></samp> file</a>: <a href="#Footnotes">Footnotes</a></li>
  2374. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-147"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stdout</span></samp> file</a>: <a href="#Other-Commands">Other Commands</a></li>
  2375. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-111"><acronym>GNU</acronym> extensions, <samp><span class="file">/dev/stdout</span></samp> file</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2376. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007b0_007d-address-57"><acronym>GNU</acronym> extensions, <code>0</code> address</a>: <a href="#Addresses">Addresses</a></li>
  2377. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-0_002c_0040var_007baddr2_007d-addressing-64"><acronym>GNU</acronym> extensions, 0,<var>addr2</var> addressing</a>: <a href="#Addresses">Addresses</a></li>
  2378. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_002b_0040var_007bN_007d-addressing-65"><acronym>GNU</acronym> extensions, <var>addr1</var>,+<var>N</var> addressing</a>: <a href="#Addresses">Addresses</a></li>
  2379. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040var_007baddr1_007d_002c_007e_0040var_007bN_007d-addressing-66"><acronym>GNU</acronym> extensions, <var>addr1</var>,~<var>N</var> addressing</a>: <a href="#Addresses">Addresses</a></li>
  2380. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-branch-if-_0040code_007bs_002f_002f_002f_007d-failed-203"><acronym>GNU</acronym> extensions, branch if <code>s///</code> failed</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2381. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-case-modifiers-in-_0040code_007bs_007d-commands-101"><acronym>GNU</acronym> extensions, case modifiers in <code>s</code> commands</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2382. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-checking-for-their-presence-207"><acronym>GNU</acronym> extensions, checking for their presence</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2383. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-185"><acronym>GNU</acronym> extensions, evaluating Bourne-shell commands</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2384. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-evaluating-Bourne_002dshell-commands-115"><acronym>GNU</acronym> extensions, evaluating Bourne-shell commands</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2385. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-extended-regular-expressions-22"><acronym>GNU</acronym> extensions, extended regular expressions</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2386. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifier-interaction-in-_0040code_007bs_007d-command-107"><acronym>GNU</acronym> extensions, <code>g</code> and <var>number</var> modifier interaction in <code>s</code> command</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2387. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-117"><acronym>GNU</acronym> extensions, <code>I</code> modifier</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2388. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040code_007bI_007d-modifier-53"><acronym>GNU</acronym> extensions, <code>I</code> modifier</a>: <a href="#Addresses">Addresses</a></li>
  2389. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-in_002dplace-editing-227"><acronym>GNU</acronym> extensions, in-place editing</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2390. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-in_002dplace-editing-15"><acronym>GNU</acronym> extensions, in-place editing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2391. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bL_007d-command-192"><acronym>GNU</acronym> extensions, <code>L</code> command</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2392. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bM_007d-modifier-119"><acronym>GNU</acronym> extensions, <code>M</code> modifier</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2393. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-modifiers-and-the-empty-regular-expression-50"><acronym>GNU</acronym> extensions, modifiers and the empty regular expression</a>: <a href="#Addresses">Addresses</a></li>
  2394. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-_0040samp_007b_0040var_007bn_007d_007e_0040var_007bm_007d_007d-addresses-42"><acronym>GNU</acronym> extensions, `<samp><var>n</var><span class="samp">~</span><var>m</var></samp>' addresses</a>: <a href="#Addresses">Addresses</a></li>
  2395. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-quitting-silently-194"><acronym>GNU</acronym> extensions, quitting silently</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2396. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-_0040code_007bR_007d-command-200"><acronym>GNU</acronym> extensions, <code>R</code> command</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2397. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-reading-a-file-a-line-at-a-time-199"><acronym>GNU</acronym> extensions, reading a file a line at a time</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2398. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-reformatting-paragraphs-191"><acronym>GNU</acronym> extensions, reformatting paragraphs</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2399. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-195"><acronym>GNU</acronym> extensions, returning an exit code</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2400. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-returning-an-exit-code-85"><acronym>GNU</acronym> extensions, returning an exit code</a>: <a href="#Common-Commands">Common Commands</a></li>
  2401. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-setting-line-length-140"><acronym>GNU</acronym> extensions, setting line length</a>: <a href="#Other-Commands">Other Commands</a></li>
  2402. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-225"><acronym>GNU</acronym> extensions, special escapes</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2403. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-escapes-212"><acronym>GNU</acronym> extensions, special escapes</a>: <a href="#Escapes">Escapes</a></li>
  2404. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-special-two_002daddress-forms-63"><acronym>GNU</acronym> extensions, special two-address forms</a>: <a href="#Addresses">Addresses</a></li>
  2405. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-186"><acronym>GNU</acronym> extensions, subprocesses</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2406. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-subprocesses-116"><acronym>GNU</acronym> extensions, subprocesses</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2407. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-223"><acronym>GNU</acronym> extensions, to basic regular expressions</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2408. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-to-basic-regular-expressions-69"><acronym>GNU</acronym> extensions, to basic regular expressions</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
  2409. <li><a href="#index-g_t_0040acronym_007bGNU_007d-extensions_002c-unlimited-line-length-214"><acronym>GNU</acronym> extensions, unlimited line length</a>: <a href="#Limitations">Limitations</a></li>
  2410. <li><a href="#index-g_t_0040value_007bSSEDEXT_007d_002c-writing-first-line-to-a-file-211"><acronym>GNU</acronym> extensions, writing first line to a file</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2411. <li><a href="#index-Goto_002c-in-scripts-178">Goto, in scripts</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2412. <li><a href="#index-Greedy-regular-expression-matching-74">Greedy regular expression matching</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
  2413. <li><a href="#index-Grouping-commands-97">Grouping commands</a>: <a href="#Common-Commands">Common Commands</a></li>
  2414. <li><a href="#index-Hold-space_002c-appending-from-pattern-space-162">Hold space, appending from pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2415. <li><a href="#index-Hold-space_002c-appending-to-pattern-space-169">Hold space, appending to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2416. <li><a href="#index-Hold-space_002c-copy-into-pattern-space-166">Hold space, copy into pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2417. <li><a href="#index-Hold-space_002c-copying-pattern-space-into-159">Hold space, copying pattern space into</a>: <a href="#Other-Commands">Other Commands</a></li>
  2418. <li><a href="#index-Hold-space_002c-definition-78">Hold space, definition</a>: <a href="#Data-Spaces">Data Spaces</a></li>
  2419. <li><a href="#index-Hold-space_002c-exchange-with-pattern-space-172">Hold space, exchange with pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2420. <li><a href="#index-In_002dplace-editing-226">In-place editing</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2421. <li><a href="#index-In_002dplace-editing-14">In-place editing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2422. <li><a href="#index-Inserting-text-before-a-line-128">Inserting text before a line</a>: <a href="#Other-Commands">Other Commands</a></li>
  2423. <li><a href="#index-Labels_002c-in-scripts-175">Labels, in scripts</a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2424. <li><a href="#index-Last-line_002c-selecting-44">Last line, selecting</a>: <a href="#Addresses">Addresses</a></li>
  2425. <li><a href="#index-Line-length_002c-setting-139">Line length, setting</a>: <a href="#Other-Commands">Other Commands</a></li>
  2426. <li><a href="#index-Line-length_002c-setting-18">Line length, setting</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2427. <li><a href="#index-Line-number_002c-printing-135">Line number, printing</a>: <a href="#Other-Commands">Other Commands</a></li>
  2428. <li><a href="#index-Line-selection-38">Line selection</a>: <a href="#Addresses">Addresses</a></li>
  2429. <li><a href="#index-Line_002c-selecting-by-number-41">Line, selecting by number</a>: <a href="#Addresses">Addresses</a></li>
  2430. <li><a href="#index-Line_002c-selecting-by-regular-expression-match-47">Line, selecting by regular expression match</a>: <a href="#Addresses">Addresses</a></li>
  2431. <li><a href="#index-Line_002c-selecting-last-45">Line, selecting last</a>: <a href="#Addresses">Addresses</a></li>
  2432. <li><a href="#index-List-pattern-space-137">List pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2433. <li><a href="#index-Mixing-_0040code_007bg_007d-and-_0040var_007bnumber_007d-modifiers-in-the-_0040code_007bs_007d-command-108">Mixing <code>g</code> and <var>number</var> modifiers in the <code>s</code> command</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2434. <li><a href="#index-Next-input-line_002c-append-to-pattern-space-152">Next input line, append to pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2435. <li><a href="#index-Next-input-line_002c-replace-pattern-space-with-94">Next input line, replace pattern space with</a>: <a href="#Common-Commands">Common Commands</a></li>
  2436. <li><a href="#index-Non_002dbugs_002c-in_002dplace-editing-228">Non-bugs, in-place editing</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2437. <li><a href="#index-Non_002dbugs_002c-_0040code_007bN_007d-command-on-the-last-line-221">Non-bugs, <code>N</code> command on the last line</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2438. <li><a href="#index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-219">Non-bugs, <code>p</code> command and <samp><span class="option">-n</span></samp> flag</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2439. <li><a href="#index-Non_002dbugs_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-91">Non-bugs, <code>p</code> command and <samp><span class="option">-n</span></samp> flag</a>: <a href="#Common-Commands">Common Commands</a></li>
  2440. <li><a href="#index-Non_002dbugs_002c-regex-syntax-clashes-224">Non-bugs, regex syntax clashes</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2441. <li><a href="#index-Parenthesized-substrings-100">Parenthesized substrings</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2442. <li><a href="#index-Pattern-space_002c-definition-77">Pattern space, definition</a>: <a href="#Data-Spaces">Data Spaces</a></li>
  2443. <li><a href="#index-Perl_002dstyle-regular-expressions_002c-multiline-54">Perl-style regular expressions, multiline</a>: <a href="#Addresses">Addresses</a></li>
  2444. <li><a href="#index-Portability_002c-comments-81">Portability, comments</a>: <a href="#Common-Commands">Common Commands</a></li>
  2445. <li><a href="#index-Portability_002c-line-length-limitations-215">Portability, line length limitations</a>: <a href="#Limitations">Limitations</a></li>
  2446. <li><a href="#index-Portability_002c-_0040code_007bN_007d-command-on-the-last-line-220">Portability, <code>N</code> command on the last line</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2447. <li><a href="#index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-218">Portability, <code>p</code> command and <samp><span class="option">-n</span></samp> flag</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2448. <li><a href="#index-Portability_002c-_0040code_007bp_007d-command-and-_0040option_007b_002dn_007d-flag-92">Portability, <code>p</code> command and <samp><span class="option">-n</span></samp> flag</a>: <a href="#Common-Commands">Common Commands</a></li>
  2449. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-bracket-expressions-72"><code>POSIXLY_CORRECT</code> behavior, bracket expressions</a>: <a href="#Regular-Expressions">Regular Expressions</a></li>
  2450. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-empty-regular-expression-49"><code>POSIXLY_CORRECT</code> behavior, empty regular expression</a>: <a href="#Addresses">Addresses</a></li>
  2451. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-escapes-213"><code>POSIXLY_CORRECT</code> behavior, escapes</a>: <a href="#Escapes">Escapes</a></li>
  2452. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-_0040code_007bN_007d-command-222"><code>POSIXLY_CORRECT</code> behavior, <code>N</code> command</a>: <a href="#Reporting-Bugs">Reporting Bugs</a></li>
  2453. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-187"><code>POSIXLY_CORRECT</code> behavior, two addresses</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2454. <li><a href="#index-g_t_0040code_007bPOSIXLY_005fCORRECT_007d-behavior_002c-two-addresses-122"><code>POSIXLY_CORRECT</code> behavior, two addresses</a>: <a href="#Other-Commands">Other Commands</a></li>
  2455. <li><a href="#index-Print-first-line-from-pattern-space-155">Print first line from pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2456. <li><a href="#index-Printing-line-number-134">Printing line number</a>: <a href="#Other-Commands">Other Commands</a></li>
  2457. <li><a href="#index-Printing-text-unambiguously-138">Printing text unambiguously</a>: <a href="#Other-Commands">Other Commands</a></li>
  2458. <li><a href="#index-Quitting-196">Quitting</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2459. <li><a href="#index-Quitting-86">Quitting</a>: <a href="#Common-Commands">Common Commands</a></li>
  2460. <li><a href="#index-Range-of-lines-55">Range of lines</a>: <a href="#Addresses">Addresses</a></li>
  2461. <li><a href="#index-Range-with-start-address-of-zero-59">Range with start address of zero</a>: <a href="#Addresses">Addresses</a></li>
  2462. <li><a href="#index-Read-next-input-line-95">Read next input line</a>: <a href="#Common-Commands">Common Commands</a></li>
  2463. <li><a href="#index-Read-text-from-a-file-198">Read text from a file</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2464. <li><a href="#index-Read-text-from-a-file-143">Read text from a file</a>: <a href="#Other-Commands">Other Commands</a></li>
  2465. <li><a href="#index-Reformat-pattern-space-189">Reformat pattern space</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2466. <li><a href="#index-Reformatting-paragraphs-190">Reformatting paragraphs</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2467. <li><a href="#index-Replace-hold-space-with-copy-of-pattern-space-158">Replace hold space with copy of pattern space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2468. <li><a href="#index-Replace-pattern-space-with-copy-of-hold-space-165">Replace pattern space with copy of hold space</a>: <a href="#Other-Commands">Other Commands</a></li>
  2469. <li><a href="#index-Replacing-all-text-matching-regexp-in-a-line-105">Replacing all text matching regexp in a line</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2470. <li><a href="#index-Replacing-only-_0040var_007bn_007dth-match-of-regexp-in-a-line-106">Replacing only <var>n</var>th match of regexp in a line</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2471. <li><a href="#index-Replacing-selected-lines-with-other-text-131">Replacing selected lines with other text</a>: <a href="#Other-Commands">Other Commands</a></li>
  2472. <li><a href="#index-Requiring-_0040value_007bSSED_007d-208">Requiring <acronym>GNU</acronym> <samp><span class="command">sed</span></samp></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2473. <li><a href="#index-Script-structure-36">Script structure</a>: <a href="#sed-Programs">sed Programs</a></li>
  2474. <li><a href="#index-Script_002c-from-a-file-32">Script, from a file</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2475. <li><a href="#index-Script_002c-from-command-line-29">Script, from command line</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2476. <li><a href="#index-g_t_0040command_007bsed_007d-program-structure-35"><samp><span class="command">sed</span></samp> program structure</a>: <a href="#sed-Programs">sed Programs</a></li>
  2477. <li><a href="#index-Selecting-lines-to-process-39">Selecting lines to process</a>: <a href="#Addresses">Addresses</a></li>
  2478. <li><a href="#index-Selecting-non_002dmatching-lines-68">Selecting non-matching lines</a>: <a href="#Addresses">Addresses</a></li>
  2479. <li><a href="#index-Several-lines_002c-selecting-56">Several lines, selecting</a>: <a href="#Addresses">Addresses</a></li>
  2480. <li><a href="#index-Slash-character_002c-in-regular-expressions-52">Slash character, in regular expressions</a>: <a href="#Addresses">Addresses</a></li>
  2481. <li><a href="#index-Spaces_002c-pattern-and-hold-76">Spaces, pattern and hold</a>: <a href="#Data-Spaces">Data Spaces</a></li>
  2482. <li><a href="#index-Special-addressing-forms-58">Special addressing forms</a>: <a href="#Addresses">Addresses</a></li>
  2483. <li><a href="#index-Standard-input_002c-processing-as-input-34">Standard input, processing as input</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2484. <li><a href="#index-Stream-editor-1">Stream editor</a>: <a href="#Introduction">Introduction</a></li>
  2485. <li><a href="#index-Subprocesses-184">Subprocesses</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2486. <li><a href="#index-Subprocesses-114">Subprocesses</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2487. <li><a href="#index-Substitution-of-text_002c-options-103">Substitution of text, options</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2488. <li><a href="#index-Text_002c-appending-125">Text, appending</a>: <a href="#Other-Commands">Other Commands</a></li>
  2489. <li><a href="#index-Text_002c-deleting-88">Text, deleting</a>: <a href="#Common-Commands">Common Commands</a></li>
  2490. <li><a href="#index-Text_002c-insertion-129">Text, insertion</a>: <a href="#Other-Commands">Other Commands</a></li>
  2491. <li><a href="#index-Text_002c-printing-90">Text, printing</a>: <a href="#Common-Commands">Common Commands</a></li>
  2492. <li><a href="#index-Text_002c-printing-after-substitution-109">Text, printing after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2493. <li><a href="#index-Text_002c-writing-to-a-file-after-substitution-110">Text, writing to a file after substitution</a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2494. <li><a href="#index-Transliteration-121">Transliteration</a>: <a href="#Other-Commands">Other Commands</a></li>
  2495. <li><a href="#index-Unbuffered-I_002fO_002c-choosing-26">Unbuffered I/O, choosing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2496. <li><a href="#index-Usage-summary_002c-printing-7">Usage summary, printing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2497. <li><a href="#index-Version_002c-printing-4">Version, printing</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2498. <li><a href="#index-Working-on-separate-files-23">Working on separate files</a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2499. <li><a href="#index-Write-first-line-to-a-file-210">Write first line to a file</a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2500. <li><a href="#index-Write-to-a-file-146">Write to a file</a>: <a href="#Other-Commands">Other Commands</a></li>
  2501. <li><a href="#index-Zero_002c-as-range-start-address-60">Zero, as range start address</a>: <a href="#Addresses">Addresses</a></li>
  2502.    </ul><div class="node">
  2503. <p><hr>
  2504. <a name="Command-and-Option-Index"></a>
  2505. Previous: <a rel="previous" accesskey="p" href="#Concept-Index">Concept Index</a>,
  2506. Up: <a rel="up" accesskey="u" href="#Top">Top</a>
  2507.  
  2508. </div>
  2509.  
  2510. <h2 class="unnumbered">Command and Option Index</h2>
  2511.  
  2512. <p>This is an alphabetical list of all <samp><span class="command">sed</span></samp> commands and command-line
  2513. options.
  2514.  
  2515.  
  2516.  
  2517. <ul class="index-fn" compact>
  2518. <li><a href="#index-g_t_0023-_0028comments_0029-79"><code># (comments)</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2519. <li><a href="#index-g_t_002d_002dexpression-28"><code>--expression</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2520. <li><a href="#index-g_t_002d_002dfile-31"><code>--file</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2521. <li><a href="#index-g_t_002d_002dhelp-6"><code>--help</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2522. <li><a href="#index-g_t_002d_002din_002dplace-13"><code>--in-place</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2523. <li><a href="#index-g_t_002d_002dline_002dlength-17"><code>--line-length</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2524. <li><a href="#index-g_t_002d_002dquiet-9"><code>--quiet</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2525. <li><a href="#index-g_t_002d_002dregexp_002dextended-20"><code>--regexp-extended</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2526. <li><a href="#index-g_t_002d_002dsilent-10"><code>--silent</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2527. <li><a href="#index-g_t_002d_002dunbuffered-25"><code>--unbuffered</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2528. <li><a href="#index-g_t_002d_002dversion-3"><code>--version</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2529. <li><a href="#index-g_t_002de-27"><code>-e</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2530. <li><a href="#index-g_t_002df-30"><code>-f</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2531. <li><a href="#index-g_t_002dh-5"><code>-h</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2532. <li><a href="#index-g_t_002di-12"><code>-i</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2533. <li><a href="#index-g_t_002dl-16"><code>-l</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2534. <li><a href="#index-g_t_002dn-8"><code>-n</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2535. <li><a href="#index-g_t_002dn_002c-forcing-from-within-a-script-82"><code>-n, forcing from within a script</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2536. <li><a href="#index-g_t_002dr-19"><code>-r</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2537. <li><a href="#index-g_t_002du-24"><code>-u</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2538. <li><a href="#index-g_t_002dV-2"><code>-V</code></a>: <a href="#Invoking-sed">Invoking sed</a></li>
  2539. <li><a href="#index-g_t_003a-_0028label_0029-command-174"><code>: (label) command</code></a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2540. <li><a href="#index-g_t_003d-_0028print-line-number_0029-command-133"><code>= (print line number) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2541. <li><a href="#index-a-_0028append-text-lines_0029-command-123"><code>a (append text lines) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2542. <li><a href="#index-b-_0028branch_0029-command-176"><code>b (branch) command</code></a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2543. <li><a href="#index-c-_0028change-to-text-lines_0029-command-130"><code>c (change to text lines) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2544. <li><a href="#index-D-_0028delete-first-line_0029-command-149"><code>D (delete first line) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2545. <li><a href="#index-d-_0028delete_0029-command-87"><code>d (delete) command</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2546. <li><a href="#index-e-_0028evaluate_0029-command-182"><code>e (evaluate) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2547. <li><a href="#index-G-_0028appending-Get_0029-command-167"><code>G (appending Get) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2548. <li><a href="#index-g-_0028get_0029-command-163"><code>g (get) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2549. <li><a href="#index-H-_0028append-Hold_0029-command-160"><code>H (append Hold) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2550. <li><a href="#index-h-_0028hold_0029-command-156"><code>h (hold) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2551. <li><a href="#index-i-_0028insert-text-lines_0029-command-127"><code>i (insert text lines) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2552. <li><a href="#index-L-_0028fLow-paragraphs_0029-command-188"><code>L (fLow paragraphs) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2553. <li><a href="#index-l-_0028list-unambiguously_0029-command-136"><code>l (list unambiguously) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2554. <li><a href="#index-N-_0028append-Next-line_0029-command-151"><code>N (append Next line) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2555. <li><a href="#index-n-_0028next_002dline_0029-command-93"><code>n (next-line) command</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2556. <li><a href="#index-P-_0028print-first-line_0029-command-154"><code>P (print first line) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2557. <li><a href="#index-p-_0028print_0029-command-89"><code>p (print) command</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2558. <li><a href="#index-q-_0028quit_0029-command-84"><code>q (quit) command</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2559. <li><a href="#index-Q-_0028silent-Quit_0029-command-193"><code>Q (silent Quit) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2560. <li><a href="#index-r-_0028read-file_0029-command-142"><code>r (read file) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2561. <li><a href="#index-R-_0028read-line_0029-command-197"><code>R (read line) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2562. <li><a href="#index-s-command_002c-option-flags-102"><code>s command, option flags</code></a>: <a href="#The-_0022s_0022-Command">The "s" Command</a></li>
  2563. <li><a href="#index-T-_0028test-and-branch-if-failed_0029-command-202"><code>T (test and branch if failed) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2564. <li><a href="#index-t-_0028test-and-branch-if-successful_0029-command-179"><code>t (test and branch if successful) command</code></a>: <a href="#Programming-Commands">Programming Commands</a></li>
  2565. <li><a href="#index-v-_0028version_0029-command-206"><code>v (version) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2566. <li><a href="#index-w-_0028write-file_0029-command-145"><code>w (write file) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2567. <li><a href="#index-W-_0028write-first-line_0029-command-209"><code>W (write first line) command</code></a>: <a href="#Extended-Commands">Extended Commands</a></li>
  2568. <li><a href="#index-x-_0028eXchange_0029-command-170"><code>x (eXchange) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2569. <li><a href="#index-y-_0028transliterate_0029-command-120"><code>y (transliterate) command</code></a>: <a href="#Other-Commands">Other Commands</a></li>
  2570. <li><a href="#index-g_t_0040_007b_0040_007d-command-grouping-96"><code>{} command grouping</code></a>: <a href="#Common-Commands">Common Commands</a></li>
  2571.    </ul>
  2572. <div class="contents">
  2573. <h2>Table of Contents</h2>
  2574. <ul>
  2575. <li><a name="toc_Top" href="#Top">sed, a stream editor</a>
  2576. <li><a name="toc_Introduction" href="#Introduction">1 Introduction</a>
  2577. <li><a name="toc_Invoking-sed" href="#Invoking-sed">2 Invocation</a>
  2578. <li><a name="toc_sed-Programs" href="#sed-Programs">3 <samp><span class="command">sed</span></samp> Programs</a>
  2579. <ul>
  2580. <li><a href="#Addresses">3.1 Selecting lines with <samp><span class="command">sed</span></samp></a>
  2581. <li><a href="#Regular-Expressions">3.2 Overview of Regular Expression Syntax</a>
  2582. <li><a href="#Data-Spaces">3.3 Where <samp><span class="command">sed</span></samp> Buffers Data</a>
  2583. <li><a href="#Common-Commands">3.4 Often-Used Commands</a>
  2584. <li><a href="#The-_0022s_0022-Command">3.5 The <code>s</code> Command</a>
  2585. <li><a href="#Other-Commands">3.6 Less Frequently-Used Commands</a>
  2586. <li><a href="#Programming-Commands">3.7 Commands for <samp><span class="command">sed</span></samp> gurus</a>
  2587. <li><a href="#Extended-Commands">3.8 Commands Specific to <acronym>GNU</acronym> <samp><span class="command">sed</span></samp></a>
  2588. <li><a href="#Escapes">3.9 <acronym>GNU</acronym> Extensions for Escapes in Regular Expressions</a>
  2589. </li></ul>
  2590. <li><a name="toc_Examples" href="#Examples">4 Some Sample Scripts</a>
  2591. <ul>
  2592. <li><a href="#Centering-lines">4.1 Centering Lines</a>
  2593. <li><a href="#Increment-a-number">4.2 Increment a Number</a>
  2594. <li><a href="#Rename-files-to-lower-case">4.3 Rename Files to Lower Case</a>
  2595. <li><a href="#Print-bash-environment">4.4 Print <samp><span class="command">bash</span></samp> Environment</a>
  2596. <li><a href="#Reverse-chars-of-lines">4.5 Reverse Characters of Lines</a>
  2597. <li><a href="#tac">4.6 Reverse Lines of Files</a>
  2598. <li><a href="#cat-_002dn">4.7 Numbering Lines</a>
  2599. <li><a href="#cat-_002db">4.8 Numbering Non-blank Lines</a>
  2600. <li><a href="#wc-_002dc">4.9 Counting Characters</a>
  2601. <li><a href="#wc-_002dw">4.10 Counting Words</a>
  2602. <li><a href="#wc-_002dl">4.11 Counting Lines</a>
  2603. <li><a href="#head">4.12 Printing the First Lines</a>
  2604. <li><a href="#tail">4.13 Printing the Last Lines</a>
  2605. <li><a href="#uniq">4.14 Make Duplicate Lines Unique</a>
  2606. <li><a href="#uniq-_002dd">4.15 Print Duplicated Lines of Input</a>
  2607. <li><a href="#uniq-_002du">4.16 Remove All Duplicated Lines</a>
  2608. <li><a href="#cat-_002ds">4.17 Squeezing Blank Lines</a>
  2609. </li></ul>
  2610. <li><a name="toc_Limitations" href="#Limitations">5 <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>'s Limitations and Non-limitations</a>
  2611. <li><a name="toc_Other-Resources" href="#Other-Resources">6 Other Resources for Learning About <samp><span class="command">sed</span></samp></a>
  2612. <li><a name="toc_Reporting-Bugs" href="#Reporting-Bugs">7 Reporting Bugs</a>
  2613. <li><a name="toc_Extended-regexps" href="#Extended-regexps">Appendix A Extended regular expressions</a>
  2614. <li><a name="toc_Concept-Index" href="#Concept-Index">Concept Index</a>
  2615. <li><a name="toc_Command-and-Option-Index" href="#Command-and-Option-Index">Command and Option Index</a>
  2616. </li></ul>
  2617. </div>
  2618.  
  2619.    <div class="footnote">
  2620. <hr>
  2621. <a name="texinfo-footnotes-in-document"></a><h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> This applies to commands such as <code>=</code>,
  2622. <code>a</code>, <code>c</code>, <code>i</code>, <code>l</code>, <code>p</code>.  You can
  2623. still write to the standard output by using the <code>w</code>
  2624. <a name="index-g_t_0040value_007bSSEDEXT_007d_002c-_0040file_007b_002fdev_002fstdout_007d-file-230"></a>or <code>W</code> commands together with the <samp><span class="file">/dev/stdout</span></samp>
  2625. special file</p>
  2626.  
  2627.    <p class="footnote"><small>[<a name="fn-2" href="#fnd-2">2</a>]</small> Note that <acronym>GNU</acronym> <samp><span class="command">sed</span></samp>
  2628.     creates the backup file whether
  2629.     or not any output is actually changed.</p>
  2630.  
  2631.    <p class="footnote"><small>[<a name="fn-3" href="#fnd-3">3</a>]</small> This is equivalent to <code>p</code> unless the <samp><span class="option">-i</span></samp>
  2632. option is being used.</p>
  2633.  
  2634.    <p class="footnote"><small>[<a name="fn-4" href="#fnd-4">4</a>]</small> This is equivalent to <code>p</code> unless the <samp><span class="option">-i</span></samp>
  2635. option is being used.</p>
  2636.  
  2637.    <p class="footnote"><small>[<a name="fn-5" href="#fnd-5">5</a>]</small> All
  2638. the escapes introduced here are <acronym>GNU</acronym>
  2639. extensions, with the exception of <code>\n</code>.</p>
  2640.  
  2641.    <p class="footnote"><small>[<a name="fn-6" href="#fnd-6">6</a>]</small> <samp><span class="command">sed</span></samp> guru Greg
  2642. Ubben wrote an implementation of the <samp><span class="command">dc</span></samp> <span class="sc">rpn</span> calculator! 
  2643. It is distributed together with sed.</p>
  2644.  
  2645.    <p class="footnote"><small>[<a name="fn-7" href="#fnd-7">7</a>]</small> This requires
  2646. another script to pad the output of banner; for example
  2647.  
  2648. <pre class="example">     #! /bin/sh
  2649.      
  2650.      banner -w $1 $2 $3 $4 |
  2651.        sed -e :a -e '/^.\{0,'$1'\}$/ { s/$/ /; ba; }' |
  2652.        ~/sedscripts/reverseline.sed
  2653. </pre>
  2654.    <p></p>
  2655.  
  2656.    <p class="footnote"><small>[<a name="fn-8" href="#fnd-8">8</a>]</small> Some implementations
  2657. have a limit of 199 commands per script</p>
  2658.  
  2659.    <p class="footnote"><small>[<a name="fn-9" href="#fnd-9">9</a>]</small> which is the actual “bug” that prompted the
  2660. change in behavior</p>
  2661.  
  2662.    <p><hr></div>
  2663.  
  2664. </body></html>
  2665.  
  2666.