1 | \input texinfo @c -*-texinfo-*- |
---|
2 | @c %**start of header |
---|
3 | @setfilename sed.info |
---|
4 | @settitle sed, a stream editor |
---|
5 | @c %**end of header |
---|
6 | |
---|
7 | @c This file has the new style title page commands. |
---|
8 | @c Run `makeinfo' rather than `texinfo-format-buffer'. |
---|
9 | |
---|
10 | @c smallbook |
---|
11 | |
---|
12 | @c tex |
---|
13 | @c \overfullrule=0pt |
---|
14 | @c end tex |
---|
15 | |
---|
16 | @include version.texi |
---|
17 | |
---|
18 | @c Combine indices. |
---|
19 | @syncodeindex ky cp |
---|
20 | @syncodeindex pg cp |
---|
21 | @syncodeindex tp cp |
---|
22 | |
---|
23 | @defcodeindex op |
---|
24 | @syncodeindex op fn |
---|
25 | |
---|
26 | @ifinfo |
---|
27 | @direntry |
---|
28 | * sed: (sed). Stream EDitor. |
---|
29 | @end direntry |
---|
30 | This file documents @sc{sed}, a stream editor. |
---|
31 | |
---|
32 | |
---|
33 | Published by the Free Software Foundation, |
---|
34 | 59 Temple Place - Suite 330 |
---|
35 | Boston, MA 02111-1307, USA |
---|
36 | |
---|
37 | Copyright (C) 1998 Free Software Foundation, Inc. |
---|
38 | |
---|
39 | Permission is granted to make and distribute verbatim copies of |
---|
40 | this manual provided the copyright notice and this permission notice |
---|
41 | are preserved on all copies. |
---|
42 | |
---|
43 | @ignore |
---|
44 | Permission is granted to process this file through TeX and print the |
---|
45 | results, provided the printed document carries copying permission |
---|
46 | notice identical to this one except for the removal of this paragraph |
---|
47 | (this paragraph not being relevant to the printed manual). |
---|
48 | |
---|
49 | @end ignore |
---|
50 | Permission is granted to copy and distribute modified versions of this |
---|
51 | manual under the conditions for verbatim copying, provided that the entire |
---|
52 | resulting derived work is distributed under the terms of a permission |
---|
53 | notice identical to this one. |
---|
54 | |
---|
55 | Permission is granted to copy and distribute translations of this manual |
---|
56 | into another language, under the above conditions for modified versions, |
---|
57 | except that this permission notice may be stated in a translation approved |
---|
58 | by the Foundation. |
---|
59 | @end ifinfo |
---|
60 | |
---|
61 | @setchapternewpage off |
---|
62 | |
---|
63 | @titlepage |
---|
64 | @title sed, a stream editor |
---|
65 | @subtitle version @value{VERSION}, @value{UPDATED} |
---|
66 | @author by Ken Pizzini |
---|
67 | |
---|
68 | @page |
---|
69 | @vskip 0pt plus 1filll |
---|
70 | Copyright @copyright{} 1998 Free Software Foundation, Inc. |
---|
71 | |
---|
72 | @sp 2 |
---|
73 | Published by the Free Software Foundation, @* |
---|
74 | 59 Temple Place - Suite 330, @* |
---|
75 | Boston, MA 02111-1307, USA |
---|
76 | |
---|
77 | Permission is granted to make and distribute verbatim copies of |
---|
78 | this manual provided the copyright notice and this permission notice |
---|
79 | are preserved on all copies. |
---|
80 | |
---|
81 | Permission is granted to copy and distribute modified versions of this |
---|
82 | manual under the conditions for verbatim copying, provided that the entire |
---|
83 | resulting derived work is distributed under the terms of a permission |
---|
84 | notice identical to this one. |
---|
85 | |
---|
86 | Permission is granted to copy and distribute translations of this manual |
---|
87 | into another language, under the above conditions for modified versions, |
---|
88 | except that this permission notice may be stated in a translation approved |
---|
89 | by the Foundation. |
---|
90 | |
---|
91 | @end titlepage |
---|
92 | @page |
---|
93 | |
---|
94 | |
---|
95 | @node Top, Introduction, (dir), (dir) |
---|
96 | @comment node-name, next, previous, up |
---|
97 | |
---|
98 | @ifinfo |
---|
99 | This document was produced for version @value{VERSION} of @sc{GNU} @sc{sed}. |
---|
100 | @end ifinfo |
---|
101 | |
---|
102 | @menu |
---|
103 | * Introduction:: Introduction |
---|
104 | * Invoking SED:: Invocation |
---|
105 | * sed Programs:: @sc{sed} programs |
---|
106 | * Examples:: Some sample scripts |
---|
107 | * Limitations:: About the (non-)limitations on line length |
---|
108 | * Other Resources:: Other resources for learning about @sc{sed} |
---|
109 | * Reporting Bugs:: Reporting bugs |
---|
110 | * Concept Index:: A menu with all the topics in this manual. |
---|
111 | * Command and Option Index:: A menu with all @sc{sed} commands and |
---|
112 | command-line options. |
---|
113 | @end menu |
---|
114 | |
---|
115 | |
---|
116 | @node Introduction, Invoking SED, Top, Top |
---|
117 | @chapter Introduction |
---|
118 | |
---|
119 | @cindex Stream editor |
---|
120 | @sc{sed} is a stream editor. |
---|
121 | A stream editor is used to perform basic text |
---|
122 | transformations on an input stream |
---|
123 | (a file or input from a pipeline). |
---|
124 | While in some ways similar to an editor which |
---|
125 | permits scripted edits (such as @sc{ed}), |
---|
126 | @sc{sed} works by making only one pass over the |
---|
127 | input(s), and is consequently more efficient. |
---|
128 | But it is @sc{sed}'s ability to filter text in a pipeline |
---|
129 | which particularly distinguishes it from other types of |
---|
130 | editors. |
---|
131 | |
---|
132 | |
---|
133 | @node Invoking SED, sed Programs, Introduction, Top |
---|
134 | @chapter Invocation |
---|
135 | |
---|
136 | @sc{sed} may be invoked with the following command-line options: |
---|
137 | |
---|
138 | @table @samp |
---|
139 | @item -V |
---|
140 | @itemx --version |
---|
141 | @opindex -V |
---|
142 | @opindex --version |
---|
143 | @cindex Version, printing |
---|
144 | Print out the version of @sc{sed} that is being run and a copyright notice, |
---|
145 | then exit. |
---|
146 | |
---|
147 | @item -h |
---|
148 | @itemx --help |
---|
149 | @opindex -h |
---|
150 | @opindex --help |
---|
151 | @cindex Usage summary, printing |
---|
152 | Print a usage message briefly summarizing these command-line options |
---|
153 | and the bug-reporting address, |
---|
154 | then exit. |
---|
155 | |
---|
156 | @item -n |
---|
157 | @itemx --quiet |
---|
158 | @itemx --silent |
---|
159 | @opindex -n |
---|
160 | @opindex --quiet |
---|
161 | @opindex --silent |
---|
162 | By default, @sc{sed} will print out the pattern space |
---|
163 | at then end of each cycle through the script. |
---|
164 | These options disable this automatic printing, |
---|
165 | and @sc{sed} will only produce output when explicitly told to |
---|
166 | via the @code{p} command. |
---|
167 | |
---|
168 | @item -e @var{script} |
---|
169 | @itemx --expression=@var{script} |
---|
170 | @opindex -e |
---|
171 | @opindex --expression |
---|
172 | @cindex Script, from command line |
---|
173 | Add the commands in @var{script} to the set of commands to be |
---|
174 | run while processing the input. |
---|
175 | |
---|
176 | @item -f @var{script-file} |
---|
177 | @itemx --file=@var{script-file} |
---|
178 | @opindex -f |
---|
179 | @opindex --file |
---|
180 | @cindex Script, from a file |
---|
181 | Add the commands contained in the file @var{script-file} |
---|
182 | to the set of commands to be run while processing the input. |
---|
183 | |
---|
184 | @end table |
---|
185 | |
---|
186 | If no @code{-e}, @code{-f}, @code{--expression}, or @code{--file} |
---|
187 | options are given on the command-line, |
---|
188 | then the first non-option argument on the command line is |
---|
189 | taken to be the @var{script} to be executed. |
---|
190 | |
---|
191 | @cindex Files to be processed as input |
---|
192 | If any command-line parameters remain after processing the above, |
---|
193 | these parameters are interpreted as the names of input files to |
---|
194 | be processed. |
---|
195 | @cindex Standard input, processing as input |
---|
196 | A file name of @code{-} refers to the standard input stream. |
---|
197 | The standard input will processed if no file names are specified. |
---|
198 | |
---|
199 | |
---|
200 | @node sed Programs, Examples, Invoking SED, Top |
---|
201 | @chapter @sc{sed} Programs |
---|
202 | |
---|
203 | @cindex @sc{sed} program structure |
---|
204 | @cindex Script structure |
---|
205 | A @sc{sed} program consists of one or more @sc{sed} commands, |
---|
206 | passed in by one or more of the |
---|
207 | @code{-e}, @code{-f}, @code{--expression}, and @code{--file} |
---|
208 | options, or the first non-option argument if zero of these |
---|
209 | options are used. |
---|
210 | This document will refer to ``the'' @sc{sed} script; |
---|
211 | this will be understood to mean the in-order catenation |
---|
212 | of all of the @var{script}s and @var{script-file}s passed in. |
---|
213 | |
---|
214 | Each @sc{sed} command consists of an optional address or |
---|
215 | address range, followed by a one-character command name |
---|
216 | and any additional command-specific code. |
---|
217 | |
---|
218 | @menu |
---|
219 | * Addresses:: Selecting lines with @sc{sed} |
---|
220 | * Regular Expressions:: Overview of regular expression syntax |
---|
221 | * Data Spaces:: Where @sc{sed} buffers data |
---|
222 | * Common Commands:: Often used commands |
---|
223 | * Other Commands:: Less frequently used commands |
---|
224 | * Programming Commands:: Commands for die-hard @sc{sed} programmers |
---|
225 | @end menu |
---|
226 | |
---|
227 | |
---|
228 | @node Addresses, Regular Expressions, sed Programs, sed Programs |
---|
229 | @section Selecting lines with @sc{sed} |
---|
230 | @cindex Addresses, in @sc{sed} scripts |
---|
231 | @cindex Line selection |
---|
232 | @cindex Selecting lines to process |
---|
233 | |
---|
234 | Addresses in a @sc{sed} script can be in any of the following forms: |
---|
235 | @table @samp |
---|
236 | @item @var{number} |
---|
237 | @cindex Address, numeric |
---|
238 | @cindex Line, selecting by number |
---|
239 | Specifying a line number will match only that line in the input. |
---|
240 | (Note that @sc{sed} counts lines continuously across all input files.) |
---|
241 | |
---|
242 | @item @var{first}~@var{step} |
---|
243 | @cindex @sc{GNU} extensions, @code{@var{n}~@var{m}} addresses |
---|
244 | This @sc{GNU} extension matches every @var{step}th line |
---|
245 | starting with line @var{first}. |
---|
246 | In particular, lines will be selected when there exists |
---|
247 | a non-negative @var{n} such that the current line-number equals |
---|
248 | @var{first} + (@var{n} * @var{step}). |
---|
249 | Thus, to select the odd-numbered lines, |
---|
250 | one would use @code{1~2}; |
---|
251 | to pick every third line starting with the second, @code{2~3} would be used; |
---|
252 | to pick every fifth line starting with the tenth, use @code{10~5}; |
---|
253 | and @code{50~0} is just an obscure way of saying @code{50}. |
---|
254 | |
---|
255 | @item $ |
---|
256 | @cindex Address, last line |
---|
257 | @cindex Last line, selecting |
---|
258 | @cindex Line, selecting last |
---|
259 | This address matches the last line of the last file of input. |
---|
260 | |
---|
261 | @item /@var{regexp}/ |
---|
262 | @cindex Address, as a regular expression |
---|
263 | @cindex Line, selecting by regular expression match |
---|
264 | This will select any line which matches the regular expression @var{regexp}. |
---|
265 | If @var{regexp} itself includes any @code{/} characters, |
---|
266 | each must be escaped by a backslash (@code{\}). |
---|
267 | |
---|
268 | @item \%@var{regexp}% |
---|
269 | (The @code{%} may be replaced by any other single character.) |
---|
270 | |
---|
271 | @cindex Slash character, in regular expressions |
---|
272 | This also matches the regular expression @var{regexp}, |
---|
273 | but allows one to use a different delimiter than @code{/}. |
---|
274 | This is particularly useful if the @var{regexp} itself contains |
---|
275 | a lot of @code{/}s, since it avoids the tedious escaping of every @code{/}. |
---|
276 | If @var{regexp} itself includes any delimiter characters, |
---|
277 | each must be escaped by a backslash (@code{\}). |
---|
278 | |
---|
279 | @item /@var{regexp}/I |
---|
280 | @itemx \%@var{regexp}%I |
---|
281 | @cindex @sc{GNU} extensions, @code{I} modifier |
---|
282 | The @code{I} modifier to regular-expression matching is a @sc{GNU} |
---|
283 | extension which causes the @var{regexp} to be matched in |
---|
284 | a case-insensitive manner. |
---|
285 | |
---|
286 | @end table |
---|
287 | |
---|
288 | If no addresses are given, then all lines are matched; |
---|
289 | if one address is given, then only lines matching that |
---|
290 | address are matched. |
---|
291 | |
---|
292 | @cindex Range of lines |
---|
293 | @cindex Several lines, selecting |
---|
294 | An address range can be specified by specifying two addresses |
---|
295 | separated by a comma (@code{,}). |
---|
296 | An address range matches lines starting from where the first |
---|
297 | address matches, and continues until the second address matches |
---|
298 | (inclusively). |
---|
299 | If the second address is a @var{regexp}, then checking for the |
---|
300 | ending match will start with the line @emph{following} the |
---|
301 | line which matched the first address. |
---|
302 | If the second address is a @var{number} less than (or equal to) |
---|
303 | the line matching the first address, |
---|
304 | then only the one line is matched. |
---|
305 | |
---|
306 | @cindex Excluding lines |
---|
307 | @cindex Selecting non-matching lines |
---|
308 | Appending the @code{!} character to the end of an address |
---|
309 | specification will negate the sense of the match. |
---|
310 | That is, if the @code{!} character follows an address range, |
---|
311 | then only lines which do @emph{not} match the address range |
---|
312 | will be selected. |
---|
313 | This also works for singleton addresses, |
---|
314 | and, perhaps perversely, for the null address. |
---|
315 | |
---|
316 | |
---|
317 | @node Regular Expressions, Data Spaces, Addresses, sed Programs |
---|
318 | @section Overview of regular expression syntax |
---|
319 | |
---|
320 | @c XXX FIXME |
---|
321 | [[I may add a brief overview of regular expressions at a later date; |
---|
322 | for now see any of the various other documentations for regular |
---|
323 | expressions, such as the @sc{awk} info page.]] |
---|
324 | |
---|
325 | |
---|
326 | @node Data Spaces, Common Commands, Regular Expressions, sed Programs |
---|
327 | @section Where @sc{sed} buffers data |
---|
328 | |
---|
329 | @cindex Buffer spaces, pattern and hold |
---|
330 | @cindex Spaces, pattern and hold |
---|
331 | @cindex Pattern space, definition |
---|
332 | @cindex Hold space, definition |
---|
333 | @sc{sed} maintains two data buffers: the active @emph{pattern} space, |
---|
334 | and the auxiliary @emph{hold} space. |
---|
335 | In ``normal'' operation, @sc{sed} reads in one line from the |
---|
336 | input stream and places it in the pattern space. |
---|
337 | This pattern space is where text manipulations occur. |
---|
338 | The hold space is initially empty, but there are commands |
---|
339 | for moving data between the pattern and hold spaces. |
---|
340 | @c XXX FIXME: explain why this is useful/interesting to know. |
---|
341 | |
---|
342 | |
---|
343 | @node Common Commands, Other Commands, Data Spaces, sed Programs |
---|
344 | @section Often used commands |
---|
345 | |
---|
346 | If you use @sc{sed} at all, you will quite likely want to know |
---|
347 | these commands. |
---|
348 | |
---|
349 | @table @samp |
---|
350 | @item # |
---|
351 | [No addresses allowed.] |
---|
352 | |
---|
353 | @findex # (comment) command |
---|
354 | @cindex Comments, in scripts |
---|
355 | The @code{#} ``command'' begins a comment; |
---|
356 | the comment continues until the next newline. |
---|
357 | |
---|
358 | @cindex Portability, comments |
---|
359 | If you are concerned about portability, be aware that |
---|
360 | some implementations of @sc{sed} (which are not POSIX.2 |
---|
361 | conformant) may only support a single one-line comment, |
---|
362 | and then only when the very first character of the script is a @code{#}. |
---|
363 | |
---|
364 | @findex -n, forcing from within a script |
---|
365 | @cindex Caveat --- #n on first line |
---|
366 | Warning: if the first two characters of the @sc{sed} script |
---|
367 | are @code{#n}, then the @code{-n} (no-autoprint) option is forced. |
---|
368 | If you want to put a comment in the first line of your script |
---|
369 | and that comment begins with the letter `n' |
---|
370 | and you do not want this behavior, |
---|
371 | then be sure to either use a capital `N', |
---|
372 | or place at least one space before the `n'. |
---|
373 | |
---|
374 | @item s/@var{regexp}/@var{replacement}/@var{flags} |
---|
375 | (The @code{/} characters may be uniformly replaced by |
---|
376 | any other single character within any given @code{s} command.) |
---|
377 | |
---|
378 | @findex s (substitute) command |
---|
379 | @cindex Substitution of text |
---|
380 | @cindex Replacing text matching regexp |
---|
381 | The @code{/} character (or whatever other character is used in its stead) |
---|
382 | can appear in the @var{regexp} or @var{replacement} |
---|
383 | only if it is preceded by a @code{\} character. |
---|
384 | Also newlines may appear in the @var{regexp} using the two |
---|
385 | character sequence @code{\n}. |
---|
386 | |
---|
387 | The @code{s} command attempts to match the pattern |
---|
388 | space against the supplied @var{regexp}. |
---|
389 | If the match is successful, then that portion of the pattern |
---|
390 | space which was matched is replaced with @var{replacement}. |
---|
391 | |
---|
392 | @cindex Backreferences, in regular expressions |
---|
393 | @cindex Parenthesized substrings |
---|
394 | The @var{replacement} can contain @code{\@var{n}} (@var{n} being |
---|
395 | a number from 1 to 9, inclusive) references, which refer to |
---|
396 | the portion of the match which is contained between the @var{n}th |
---|
397 | @code{\(} and its matching @code{\)}. |
---|
398 | Also, the @var{replacement} can contain unescaped @code{&} |
---|
399 | characters which will reference the whole matched portion |
---|
400 | of the pattern space. |
---|
401 | To include a literal @code{\}, @code{&}, or newline in the final |
---|
402 | replacement, be sure to precede the desired @code{\}, @code{&}, |
---|
403 | or newline in the @var{replacement} with a @code{\}. |
---|
404 | |
---|
405 | @findex s command, option flags |
---|
406 | @cindex Substitution of text, options |
---|
407 | @cindex Replacing text matching regexp, options |
---|
408 | The @code{s} command can be followed with zero or more of the |
---|
409 | following @var{flags}: |
---|
410 | |
---|
411 | @table @samp |
---|
412 | @item g |
---|
413 | @cindex Global substitution |
---|
414 | @cindex Replacing all text matching regexp in a line |
---|
415 | Apply the replacement to @emph{all} matches to the @var{regexp}, |
---|
416 | not just the first. |
---|
417 | @item p |
---|
418 | @cindex Printing text after substitution |
---|
419 | If the substitution was made, then print the new pattern space. |
---|
420 | @item @var{number} |
---|
421 | @cindex Replacing only @var{n}th match of regexp in a line |
---|
422 | Only replace the @var{number}th match of the @var{regexp}. |
---|
423 | @item w @var{file-name} |
---|
424 | @cindex Write result of a substitution to file |
---|
425 | If the substitution was made, then write out the result to the named file. |
---|
426 | @item I |
---|
427 | (This is a @sc{GNU} extension.) |
---|
428 | |
---|
429 | @cindex @sc{GNU} extensions, @code{I} modifier |
---|
430 | @cindex Case-insensitive matching |
---|
431 | Match @var{regexp} in a case-insensitive manner. |
---|
432 | @end table |
---|
433 | |
---|
434 | @item q |
---|
435 | [At most one address allowed.] |
---|
436 | |
---|
437 | @findex q (quit) command |
---|
438 | @cindex Quitting |
---|
439 | Exit @sc{sed} without processing any more commands or input. |
---|
440 | Note that the current pattern space is printed |
---|
441 | if auto-print is not disabled. |
---|
442 | |
---|
443 | @item d |
---|
444 | @findex d (delete) command |
---|
445 | @cindex Deleting lines |
---|
446 | Delete the pattern space; |
---|
447 | immediately start next cycle. |
---|
448 | |
---|
449 | @item p |
---|
450 | @findex p (print) command |
---|
451 | @cindex Print selected lines |
---|
452 | Print out the pattern space (to the standard output). |
---|
453 | This command is usually only used in conjunction with the @code{-n} |
---|
454 | command-line option. |
---|
455 | |
---|
456 | @cindex Caveat --- @code{p} command and -n flag |
---|
457 | Note: some implementations of @sc{sed}, such as this one, will |
---|
458 | double-print lines when auto-print is not disabled and the @code{p} |
---|
459 | command is given. |
---|
460 | Other implementations will only print the line once. |
---|
461 | Both ways conform with the POSIX.2 standard, and so neither |
---|
462 | way can be considered to be in error. |
---|
463 | @cindex Portability, @code{p} command and -n flag |
---|
464 | Portable @sc{sed} scripts should thus avoid relying on either behavior; |
---|
465 | either use the @code{-n} option and explicitly print what you want, |
---|
466 | or avoid use of the @code{p} command (and also the @code{p} flag to the |
---|
467 | @code{s} command). |
---|
468 | |
---|
469 | @item n |
---|
470 | @findex n (next-line) command |
---|
471 | @cindex Next input line, replace pattern space with |
---|
472 | @cindex Read next input line |
---|
473 | If auto-print is not disabled, print the pattern space, |
---|
474 | then, regardless, replace the pattern space with the next line of input. |
---|
475 | If there is no more input then @sc{sed} exits without processing |
---|
476 | any more commands. |
---|
477 | |
---|
478 | @item @{ @var{commands} @} |
---|
479 | @findex @{@} command grouping |
---|
480 | @cindex Grouping commands |
---|
481 | @cindex Command groups |
---|
482 | A group of commands may be enclosed between |
---|
483 | @code{@{} and @code{@}} characters. |
---|
484 | (The @code{@}} must appear in a zero-address command context.) |
---|
485 | This is particularly useful when you want a group of commands |
---|
486 | to be triggered by a single address (or address-range) match. |
---|
487 | |
---|
488 | @end table |
---|
489 | |
---|
490 | |
---|
491 | @node Other Commands, Programming Commands, Common Commands, sed Programs |
---|
492 | @section Less frequently used commands |
---|
493 | |
---|
494 | Though perhaps less frequently used than those in the previous |
---|
495 | section, some very small yet useful @sc{sed} scripts can be built with |
---|
496 | these commands. |
---|
497 | |
---|
498 | @table @samp |
---|
499 | @item y/@var{source-chars}/@var{dest-chars}/ |
---|
500 | (The @code{/} characters may be uniformly replaced by |
---|
501 | any other single character within any given @code{y} command.) |
---|
502 | |
---|
503 | @findex y (transliterate) command |
---|
504 | @cindex Transliteration |
---|
505 | Transliterate any characters in the pattern space which match |
---|
506 | any of the @var{source-chars} with the corresponding character |
---|
507 | in @var{dest-chars}. |
---|
508 | |
---|
509 | Instances of the @code{/} (or whatever other character is used in its stead), |
---|
510 | @code{\}, or newlines can appear in the @var{source-chars} or @var{dest-chars} |
---|
511 | lists, provide that each instance is escaped by a @code{\}. |
---|
512 | The @var{source-chars} and @var{dest-chars} lists @emph{must} |
---|
513 | contain the same number of characters (after de-escaping). |
---|
514 | |
---|
515 | @c XXX was getting a bad page break; remove this @need if formatting changes |
---|
516 | @need 1000 |
---|
517 | @item a\ |
---|
518 | @itemx @var{text} |
---|
519 | [At most one address allowed.] |
---|
520 | |
---|
521 | @findex a (append text lines) command |
---|
522 | @cindex Adding a block of text after a line |
---|
523 | @cindex Text, appending |
---|
524 | Queue the lines of text which follow this command |
---|
525 | (each but the last ending with a @code{\}, |
---|
526 | which will be removed from the output) |
---|
527 | to be output at the end of the current cycle, |
---|
528 | or when the next input line is read. |
---|
529 | |
---|
530 | @item i\ |
---|
531 | @itemx @var{text} |
---|
532 | [At most one address allowed.] |
---|
533 | |
---|
534 | @findex i (insert text lines) command |
---|
535 | @cindex Inserting a block of text before a line |
---|
536 | @cindex Text, insertion |
---|
537 | Immediately output the lines of text which follow this command |
---|
538 | (each but the last ending with a @code{\}, |
---|
539 | which will be removed from the output). |
---|
540 | |
---|
541 | @item c\ |
---|
542 | @itemx @var{text} |
---|
543 | @findex c (change to text lines) command |
---|
544 | @cindex Replace specific input lines |
---|
545 | @cindex Selected lines, replacing |
---|
546 | Delete the lines matching the address or address-range, |
---|
547 | and output the lines of text which follow this command |
---|
548 | (each but the last ending with a @code{\}, |
---|
549 | which will be removed from the output) |
---|
550 | in place of the last line |
---|
551 | (or in place of each line, if no addresses were specified). |
---|
552 | A new cycle is started after this command is done, |
---|
553 | since the pattern space will have been deleted. |
---|
554 | |
---|
555 | @item = |
---|
556 | [At most one address allowed.] |
---|
557 | |
---|
558 | @findex = (print line number) command |
---|
559 | @cindex Print line number |
---|
560 | @cindex Line number, print |
---|
561 | Print out the current input line number (with a trailing newline). |
---|
562 | |
---|
563 | @item l |
---|
564 | @findex l (list unambiguously) command |
---|
565 | @cindex List pattern space |
---|
566 | @cindex Print unambiguous representation of pattern space |
---|
567 | Print the pattern space in an unambiguous form: |
---|
568 | non-printable characters (and the @code{\} character) |
---|
569 | are printed in C-style escaped form; |
---|
570 | long lines are split, with a trailing @code{\} character |
---|
571 | to indicate the split; the end of each line is marked |
---|
572 | with a @code{$}. |
---|
573 | |
---|
574 | @item r @var{filename} |
---|
575 | [At most one address allowed.] |
---|
576 | |
---|
577 | @findex r (read file) command |
---|
578 | @cindex Read text from a file |
---|
579 | @cindex Insert text from a file |
---|
580 | Queue the contents of @var{filename} to be read and |
---|
581 | inserted into the output stream at the end of the current cycle, |
---|
582 | or when the next input line is read. |
---|
583 | Note that if @var{filename} cannot be read, it is treated as |
---|
584 | if it were an empty file, without any error indication. |
---|
585 | |
---|
586 | @item w @var{filename} |
---|
587 | @findex w (write file) command |
---|
588 | @cindex Write to a file |
---|
589 | Write the pattern space to @var{filename}. |
---|
590 | The @var{filename} will be created (or truncated) before the |
---|
591 | first input line is read; all @code{w} commands (including |
---|
592 | instances of @code{w} flag on successful @code{s} commands) |
---|
593 | which refer to the same @var{filename} are output through |
---|
594 | the same @sc{FILE} stream. |
---|
595 | |
---|
596 | @item D |
---|
597 | @findex D (delete first line) command |
---|
598 | @cindex Delete first line from pattern space |
---|
599 | Delete text in the pattern space up to the first newline. |
---|
600 | If any text is left, restart cycle with the resultant |
---|
601 | pattern space (without reading a new line of input), |
---|
602 | otherwise start a normal new cycle. |
---|
603 | |
---|
604 | @item N |
---|
605 | @findex N (append Next line) command |
---|
606 | @cindex Next input line, append to pattern space |
---|
607 | @cindex Append next input line to pattern space |
---|
608 | Add a newline to the pattern space, |
---|
609 | then append the next line of input to the pattern space. |
---|
610 | If there is no more input then @sc{sed} exits without processing |
---|
611 | any more commands. |
---|
612 | |
---|
613 | @item P |
---|
614 | @findex P (print first line) command |
---|
615 | @cindex Print first line from pattern space |
---|
616 | Print out the portion of the pattern space up to the first newline. |
---|
617 | |
---|
618 | @item h |
---|
619 | @findex h (hold) command |
---|
620 | @cindex Copy pattern space into hold space |
---|
621 | @cindex Replace hold space with copy of pattern space |
---|
622 | @cindex Hold space, copying pattern space into |
---|
623 | Replace the contents of the hold space with the contents of the pattern space. |
---|
624 | |
---|
625 | @item H |
---|
626 | @findex H (append Hold) command |
---|
627 | @cindex Append pattern space to hold space |
---|
628 | @cindex Hold space, appending from pattern space |
---|
629 | Append a newline to the contents of the hold space, |
---|
630 | and then append the contents of the pattern space to that of the hold space. |
---|
631 | |
---|
632 | @item g |
---|
633 | @findex g (get) command |
---|
634 | @cindex Copy hold space into pattern space |
---|
635 | @cindex Replace pattern space with copy of hold space |
---|
636 | @cindex Hold space, copy into pattern space |
---|
637 | Replace the contents of the pattern space with the contents of the hold space. |
---|
638 | |
---|
639 | @item G |
---|
640 | @findex G (appending Get) command |
---|
641 | @cindex Append hold space to pattern space |
---|
642 | @cindex Hold space, appending to pattern space |
---|
643 | Append a newline to the contents of the pattern space, |
---|
644 | and then append the contents of the hold space to that of the pattern space. |
---|
645 | |
---|
646 | @item x |
---|
647 | @findex x (eXchange) command |
---|
648 | @cindex Exchange hold space with pattern space |
---|
649 | @cindex Hold space, exchange with pattern space |
---|
650 | Exchange the contents of the hold and pattern spaces. |
---|
651 | |
---|
652 | @end table |
---|
653 | |
---|
654 | |
---|
655 | @node Programming Commands, , Other Commands, sed Programs |
---|
656 | @section Commands for die-hard @sc{sed} programmers |
---|
657 | |
---|
658 | In most cases, use of these commands indicates that you are |
---|
659 | probably better off programming in something like @sc{perl}. |
---|
660 | But occasionally one is committed to sticking with @sc{sed}, |
---|
661 | and these commands can enable one to write quite convoluted |
---|
662 | scripts. |
---|
663 | |
---|
664 | @cindex Flow of control in scripts |
---|
665 | @table @samp |
---|
666 | @item : @var{label} |
---|
667 | [No addresses allowed.] |
---|
668 | |
---|
669 | @findex : (label) command |
---|
670 | @cindex Labels, in scripts |
---|
671 | Specify the location of @var{label} for the @code{b} and @code{t} commands. |
---|
672 | In all other respects, a no-op. |
---|
673 | |
---|
674 | @item b @var{label} |
---|
675 | @findex b (branch) command |
---|
676 | @cindex Branch to a label, unconditionally |
---|
677 | @cindex Goto, in scripts |
---|
678 | Unconditionally branch to @var{label}. |
---|
679 | The @var{label} may be omitted, in which case the next cycle is started. |
---|
680 | |
---|
681 | @item t @var{label} |
---|
682 | @findex t (conditional branch) command |
---|
683 | @cindex Branch to a label, if @code{s///} succeeded |
---|
684 | @cindex Conditional branch |
---|
685 | Branch to @var{label} only if there has been a successful @code{s}ubstitution |
---|
686 | since the last input line was read or @code{t} branch was taken. |
---|
687 | The @var{label} may be omitted, in which case the next cycle is started. |
---|
688 | |
---|
689 | @end table |
---|
690 | |
---|
691 | |
---|
692 | @node Examples, Limitations, sed Programs, Top |
---|
693 | @chapter Some sample scripts |
---|
694 | |
---|
695 | @c XXX FIXME |
---|
696 | [[Not this release, sorry. |
---|
697 | But check out the scripts in the testsuite directory, |
---|
698 | and the amazing dc.sed script in the |
---|
699 | top-level directory of this distribution.]] |
---|
700 | |
---|
701 | |
---|
702 | @node Limitations, Other Resources, Examples, Top |
---|
703 | @chapter About the (non-)limitations on line length |
---|
704 | |
---|
705 | @cindex @sc{GNU} extensions, unlimited line length |
---|
706 | @cindex Portability, line length limitations |
---|
707 | For those who want to write portable @sc{sed} scripts, |
---|
708 | be aware that some implementations have been known to |
---|
709 | limit line lengths (for the pattern and hold spaces) |
---|
710 | to be no more than 4000 bytes. |
---|
711 | The POSIX.2 standard specifies that conforming @sc{sed} |
---|
712 | implementations shall support at least 8192 byte line lengths. |
---|
713 | @sc{GNU} @sc{sed} has no built-in limit on line length; |
---|
714 | as long as @sc{sed} can malloc() more (virtual) memory, |
---|
715 | it will allow lines as long as you care to feed it |
---|
716 | (or construct within it). |
---|
717 | |
---|
718 | @node Other Resources, Reporting Bugs, Limitations, Top |
---|
719 | @chapter Other resources for learning about @sc{sed} |
---|
720 | |
---|
721 | @cindex Addtional reading about @sc{sed} |
---|
722 | In addition to several books that have been written about @sc{sed} |
---|
723 | (either specifically or as chapters in books which discuss |
---|
724 | shell programming), one can find out more about @sc{sed} |
---|
725 | (including suggestions of a few books) from the FAQ |
---|
726 | for the seders mailing list, available from any of: |
---|
727 | @display |
---|
728 | @uref{http://www.dbnet.ece.ntua.gr/~george/sed/sedfaq.html} |
---|
729 | @uref{http://www.ptug.org/sed/sedfaq.htm} |
---|
730 | @uref{http://www.wollery.demon.co.uk/sedtut10.txt} |
---|
731 | @end display |
---|
732 | |
---|
733 | There is an informal ``seders'' mailing list manually maintained |
---|
734 | by Al Aab. To subscribe, send e-mail to @email{af137@@torfree.net} |
---|
735 | with a brief description of your interest. |
---|
736 | |
---|
737 | @node Reporting Bugs, Concept Index, Other Resources, Top |
---|
738 | @chapter Reporting bugs |
---|
739 | |
---|
740 | @cindex Bugs, reporting |
---|
741 | Email bug reports to @email{bug-gnu-utils@@gnu.org}. |
---|
742 | Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. |
---|
743 | |
---|
744 | @c XXX FIXME: the term "cycle" is never defined... |
---|
745 | |
---|
746 | @page |
---|
747 | @node Concept Index, Command and Option Index, Reporting Bugs, Top |
---|
748 | @unnumbered Concept Index |
---|
749 | |
---|
750 | This is a general index of all issues discussed in this manual, with the |
---|
751 | exception of the @sc{sed} commands and command-line options. |
---|
752 | |
---|
753 | @printindex cp |
---|
754 | |
---|
755 | @page |
---|
756 | @node Command and Option Index, , Concept Index, Top |
---|
757 | @unnumbered Command and Option Index |
---|
758 | |
---|
759 | This is an alphabetical list of all @sc{sed} commands and command-line |
---|
760 | opions. |
---|
761 | |
---|
762 | @printindex fn |
---|
763 | |
---|
764 | @contents |
---|
765 | @bye |
---|