sed_summary.htm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <html>
  2. <head><title>Command Summary for sed (sed & awk, Second Edition)</title>
  3. </head>
  4. <body>
  5. <h2>Command Summary for sed</h2>
  6. <dl>
  7. <dt><b>: </b> <b> :</b><em>label</em></dt>
  8. <dd>Label a line in the script for the transfer of control by
  9. <b>b</b> or <b>t</b>.
  10. <em>label</em> may contain up to seven characters.
  11. (The POSIX standard says that an implementation can allow longer
  12. labels if it wishes to. GNU sed allows labels to be of any length.)
  13. </p></dd>
  14. <dt><b>=</b> [<em>address</em>]<b>=</b></dt>
  15. <dd>Write to standard output the line number of addressed line.</p></dd>
  16. <dt><b>a</b> [<em>address</em>]<b>a\</b></dt>
  17. <dd><em>text</em></p>
  18. <p>Append <em>text</em>
  19. following each line matched by <em>address</em>. If
  20. <em>text</em> goes over more than one line, newlines
  21. must be "hidden" by preceding them with a backslash. The
  22. <em>text</em> will be terminated by the first
  23. newline that is not hidden in this way. The
  24. <em>text</em> is not available in the pattern space
  25. and subsequent commands cannot be applied to it. The results of this
  26. command are sent to standard output when the list of editing commands
  27. is finished, regardless of what happens to the current line in the
  28. pattern space.</p></dd>
  29. <dt><b>b</b> [<em>address1</em>[,<em>address2</em>]]<b>b</b>[<em>label</em>]</dt>
  30. <dd>Transfer control unconditionally (branch) to
  31. <b>:</b><em>label</em> elsewhere in
  32. script. That is, the command following the
  33. <em>label</em> is the next command applied to the
  34. current line. If no <em>label</em> is specified,
  35. control falls through to the end of the script, so no more commands
  36. are applied to the current line.</p></dd>
  37. <dt><b>c</b> [<em>address1</em>[,<em>address2</em>]]<b>c\</b></dt>
  38. <dd><em>text</em></p>
  39. <p>Replace (change) the lines selected by the address with
  40. <em>text</em>. When a range of lines is specified,
  41. all lines as a group are replaced by a single copy of
  42. <em>text</em>. The newline following each line of
  43. <em>text</em> must be escaped by a backslash, except
  44. the last line. The contents of the pattern space are, in effect,
  45. deleted and no subsequent editing commands can be applied to it (or to
  46. <em>text</em>).</p></dd>
  47. <dt><b>d</b> [<em>address1</em>[,<em>address2</em>]]<b>d</b></dt>
  48. <dd>Delete line(s) from pattern space. Thus, the line is not passed to standard
  49. output. A new line of input is read and editing resumes with first
  50. command in script.</p></dd>
  51. <dt><b>D</b> [<em>address1</em>[,<em>address2</em>]]<b>D</b></dt>
  52. <dd>Delete first part (up to embedded newline) of multiline pattern space created
  53. by <b>N</b> command and resume editing with first command in
  54. script. If this command empties the pattern space, then a new line
  55. of input is read, as if the <b>d</b> command had been executed.</p></dd>
  56. <dt><b>g</b> [<em>address1</em>[,<em>address2</em>]]<b>g</b></dt>
  57. <dd>Copy (get) contents of hold space (see <b>h</b> or
  58. <b>H</b> command) into the pattern space, wiping out
  59. previous contents.</p></dd>
  60. <dt><b>G</b> [<em>address1</em>[,<em>address2</em>]]<b>G</b></dt>
  61. <dd>Append newline followed by contents of hold space (see
  62. <b>h</b> or <b>H</b> command) to contents of
  63. the pattern space. If hold space is empty, a newline is still
  64. appended to the pattern space.</p></dd>
  65. <dt><b>h</b> [<em>address1</em>[,<em>address2</em>]]<b>h</b></dt>
  66. <dd>Copy pattern space into hold space, a special temporary buffer.
  67. Previous contents of hold space are wiped out.</p></dd>
  68. <dt><b>H</b> [<em>address1</em>[,<em>address2</em>]]<b>H</b></dt>
  69. <dd>Append newline and contents of pattern space to contents of the hold
  70. space. Even if hold space is empty, this command still appends the
  71. newline first.</p></dd>
  72. <dt><b>i</b> [<em>address1</em>]<b>i\</b></dt>
  73. <dd><em>text</em></p>
  74. <p>Insert <em>text</em> before each line matched by
  75. <em>address</em>. (See <b>a</b> for
  76. details on <em>text</em>.)</p></dd>
  77. <dt><b>l</b> [<em>address1</em>[,<em>address2</em>]]<b>l</b></dt>
  78. <dd>List the contents of the pattern space, showing nonprinting characters
  79. as ASCII codes. Long lines are wrapped.</p></dd>
  80. <dt><b>n</b> [<em>address1</em>[,<em>address2</em>]]<b>n</b></dt>
  81. <dd>Read next line of input into pattern space. Current line is sent to
  82. standard output. New line becomes current line and increments line
  83. counter. Control passes to command following <b>n</b>
  84. instead of resuming at the top of the script.</p></dd>
  85. <dt><b>N</b> [<em>address1</em>[,<em>address2</em>]]<b>N</b></dt>
  86. <dd>Append next input line to contents of pattern space; the new line is
  87. separated from the previous contents of the pattern space by a newline.
  88. (This command is designed to allow pattern matches across two
  89. lines. Using \n to match the embedded newline, you can match
  90. patterns across multiple lines.)</p></dd>
  91. <dt><b>p</b> [<em>address1</em>[,<em>address2</em>]]<b>p</b></dt>
  92. <dd>Print the addressed line(s). Note that this can result in duplicate
  93. output unless default output is suppressed by using "#n" or
  94. the <span class="option">-n</span>
  95. command-line option. Typically used before commands that change flow
  96. control (<b>d</b>, <b>n</b>,
  97. <b>b</b>) and might prevent the current line from being
  98. output.</p></dd>
  99. <dt><b>P</b> [<em>address1</em>[,<em>address2</em>]]<b>P</b></dt>
  100. <dd>Print first part (up to embedded newline) of multiline pattern space
  101. created by <b>N</b> command. Same as <b>p</b>
  102. if <b>N</b> has not been applied to a line.</p></dd>
  103. <dt><b>q</b> [<em>address</em>]<b>q</b></dt>
  104. <dd>Quit when <em>address</em> is encountered. The
  105. addressed line is first written to output (if default output is not
  106. suppressed), along with any text appended to it by previous
  107. <b>a</b> or <b>r</b> commands.</p></dd>
  108. <dt><b>r</b> [<em>address</em>]<b>r</b> <em>file</em></dt>
  109. <dd>Read contents of <em>file</em> and append after the
  110. contents of the pattern space. Exactly one space must be put between
  111. <b>r</b> and the filename.</p></dd>
  112. <dt><b>s</b> [<em>address1</em>[,<em>address2</em>]]<b>s</b>/<em>pattern</em>/<em>replacement</em>/[<em>flags</em>]</dt>
  113. <dd>Substitute <em>replacement</em> for
  114. <em>pattern</em> on each addressed line. If pattern
  115. addresses are used, the pattern <b>//</b> represents the
  116. last pattern address specified. The following flags can be specified:</p>
  117. <dl>
  118. <dt><b>n</b></dt>
  119. <dd>Replace <em>n</em>th instance of
  120. /<em>pattern</em>/ on each addressed line.
  121. <em>n</em> is any number in the range 1 to 512, and
  122. the default is 1.</p></dd>
  123. <dt><b>g</b></dt>
  124. <dd>Replace all instances of /<em>pattern</em>/ on each
  125. addressed line, not just the first instance.</p></dd>
  126. <dt><b>I</b></dt>
  127. <dd>Matching is case-insensitive.<p></p></dd>
  128. <dt><b>p</b></dt>
  129. <dd>Print the line if a successful substitution is done. If several
  130. successful substitutions are done, multiple copies of the line will be
  131. printed.</p></dd>
  132. <dt><b>w</b> <em>file</em></dt>
  133. <dd>Write the line to <em>file</em> if a replacement
  134. was done. A maximum of 10 different <em>files</em> can be opened.</p></dd>
  135. </dl>
  136. </dd>
  137. <dt><b>t</b> [<em>address1</em>[,<em>address2</em>]]<b>t </b>[<em>label</em>]</dt>
  138. <dd>Test if successful substitutions have been made on addressed lines,
  139. and if so, branch to line marked by :<em>label</em>.
  140. (See <b>b</b> and <b>:</b>.) If label is not
  141. specified, control falls through to bottom of script.</p></dd>
  142. <dt><b>w</b> [<em>address1</em>[,<em>address2</em>]]<b>w</b> <em>file</em></dt>
  143. <dd>Append contents of pattern space to <em>file</em>.
  144. This action occurs when the command is encountered rather than when
  145. the pattern space is output. Exactly one space must separate the
  146. <b>w</b> and the filename. A maximum of 10 different
  147. files can be opened in a script. This command will create the file if
  148. it does not exist; if the file exists, its contents will be
  149. overwritten each time the script is executed. Multiple write commands
  150. that direct output to the same file append to the end of the file.</p></dd>
  151. <dt><b>x</b> [<em>address1</em>[,<em>address2</em>]]<b>x</b></dt>
  152. <dd>Exchange contents of the pattern space with the contents of the hold
  153. space.</p></dd>
  154. <dt><b>y</b> [<em>address1</em>[,<em>address2</em>]]<b>y</b>/<em>abc</em>/<em>xyz</em>/</dt>
  155. <dd>Transform each character by position in string
  156. <em>abc</em> to its equivalent in string
  157. <em>xyz</em>.</p></dd>
  158. </dl>