home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / !gcc / docs / sed < prev    next >
Encoding:
Text File  |  2004-09-05  |  89.5 KB  |  2,438 lines

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