1 .\" $NetBSD: sed.1,v 1.27 2008/09/21 16:46:01 wiz Exp $
3 .\" Copyright (c) 1992, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" the Institute of Electrical and Electronics Engineers, Inc.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
17 .\" 3. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 .\" @(#)sed.1 8.2 (Berkeley) 12/30/93
35 .Dd September 21, 2008
49 .Op Fl f Ar command_file
54 utility reads the specified files, or the standard input if no files
55 are specified, modifying the input as specified by a list of commands.
56 The input is then written to the standard output.
58 A single command may be specified as the first argument to
60 Multiple commands may be specified by using the
65 All commands are applied to the input in the order they are specified
66 regardless of their origin.
68 The following options are available:
69 .Bl -tag -width indent
71 The files listed as parameters for the
73 functions are created (or truncated) before any processing begins,
79 to delay opening each file until a command containing the related
81 function is applied to a line of input.
83 Enables the use of extended regular expressions instead of the
84 usual basic regular expression syntax.
86 Append the editing commands specified by the
89 to the list of commands.
90 .It Fl f Ar command_file
91 Append the editing commands found in the file
93 to the list of commands.
94 The editing commands should each be listed on a separate line.
96 By default, each line of input is echoed to the standard output after
97 all of the commands have been applied to it.
100 option suppresses this behavior.
104 present for compatibility with GNU sed.
109 command is as follows:
111 .Dl [address[,address]]function[arguments]
113 Whitespace may be inserted before the first address and the function
114 portions of the command.
118 cyclically copies a line of input, not including its terminating newline
120 .Em "pattern space" ,
121 (unless there is something left after a
124 applies all of the commands with addresses that select that pattern space,
125 copies the pattern space to the standard output, appending a newline, and
126 deletes the pattern space.
128 Some of the functions use a
130 to save all or part of the pattern space for subsequent retrieval.
132 An address is not required, but if specified must be a number (that counts
134 cumulatively across input files), a dollar
138 character that addresses the last line of input, or a context address
139 (which consists of a regular expression preceded and followed by a
142 A command line with no addresses selects every pattern space.
144 A command line with one address selects all of the pattern spaces
145 that match the address.
147 A command line with two addresses selects the inclusive range from
148 the first pattern space that matches the first address through the next
149 pattern space that matches the second.
150 (If the second address is a number less than or equal to the line number
151 first selected, only that line is selected.)
152 Starting at the first line following the selected range,
154 starts looking again for the first address.
156 Editing commands can be applied to non-selected pattern spaces by use
157 of the exclamation character
160 .Sh SED REGULAR EXPRESSIONS
163 regular expressions are basic regular expressions (BRE's, see
165 for more information).
168 has the following two additions to BRE's:
172 In a context address, any character other than a backslash
176 or newline character may be used to delimit the regular expression
177 by prefixing the first use of that delimiter with a backslash.
178 Also, putting a backslash character before the delimiting character
179 causes the character to be treated literally.
180 For example, in the context address \exabc\exdefx, the RE delimiter
185 stands for itself, so that the regular expression is
189 The escape sequence \en matches a newline character embedded in the
191 You can't, however, use a literal newline character in an address or
192 in the substitute command.
195 One special feature of
197 regular expressions is that they can default to the last regular
199 If a regular expression is empty, i.e. just the delimiter characters
200 are specified, the last regular expression encountered is used instead.
201 The last regular expression is defined as the last regular expression
202 used as part of an address or substitute command, and at run-time, not
204 For example, the command
211 In the following list of commands, the maximum number of permissible
212 addresses for each command is indicated by [0addr], [1addr], or [2addr],
213 representing zero, one, or two addresses.
217 consists of one or more lines.
218 To embed a newline in the text, precede it with a backslash.
219 Other backslashes in text are deleted and the following character
226 functions take an optional file parameter, which should be separated
227 from the function letter by white space.
228 Each file given as an argument to
230 is created (or its contents truncated) before any input processing begins.
242 functions all accept additional arguments.
243 The following synopses indicate which arguments have to be separated from
244 the function letters by white space characters.
246 Two of the functions take a function-list.
249 functions separated by newlines, as follows:
250 .Bd -literal -offset indent
260 can be preceded by white space and can be followed by white space.
261 The function can be preceded by white space.
264 must be preceded by a newline (and optionally white space).
266 .Bl -tag -width "XXXXXX" -compact
267 .It [2addr] function-list
268 Execute function-list only when the pattern space is selected.
275 to standard output immediately before each attempt to read a line of input,
276 whether by executing the
278 function or by beginning a new cycle.
283 function with the specified label.
284 If the label is not specified, branch to the end of the script.
289 Delete the pattern space.
290 With 0 or 1 address or at the end of a 2-address range,
292 is written to the standard output.
295 Delete the pattern space and start the next cycle.
298 Delete the initial segment of the pattern space through the first
299 newline character and start the next cycle.
302 Replace the contents of the pattern space with the contents of the
306 Append a newline character followed by the contents of the hold space
307 to the pattern space.
310 Replace the contents of the hold space with the contents of the
314 Append a newline character followed by the contents of the pattern space
322 to the standard output.
326 Write the pattern space to the standard output in a visually unambiguous
328 This form is as follows:
330 .Bl -tag -width "carriage-returnXX" -offset indent -compact
347 Nonprintable characters are written as three-digit octal numbers (with a
348 preceding backslash) for each byte in the character (most significant byte
350 Long lines are folded, with the point of folding indicated by displaying
351 a backslash followed by a newline.
352 The end of each line is marked with a
356 Write the pattern space to the standard output if the default output has
357 not been suppressed, and replace the pattern space with the next line of
358 input. (Does not begin a new cycle.)
361 Append the next line of input to the pattern space, using an embedded
362 newline character to separate the appended material from the original
364 Note that the current line number changes.
367 Write the pattern space to standard output.
370 Write the pattern space, up to the first newline character to the
374 Branch to the end of the script and quit without starting a new cycle.
379 to the standard output immediately before the next attempt to read a
383 cannot be read for any reason, it is silently ignored and no error
386 .It [2addr]s/regular expression/replacement/flags
387 Substitute the replacement string for the first instance of the regular
388 expression in the pattern space.
389 Any character other than backslash or newline can be used instead of
390 a slash to delimit the RE and the replacement.
391 Within the RE and the replacement, the RE delimiter itself can be used as
392 a literal character if it is preceded by a backslash.
398 appearing in the replacement is replaced by the string matching the RE.
399 The special meaning of
401 in this context can be suppressed by preceding it by a backslash.
406 is a digit, is replaced by the text matched
407 by the corresponding backreference expression (see
410 A line can be split by substituting a newline character into it.
411 To specify a newline character in the replacement string, precede it with
416 in the substitute function is zero or more of the following:
417 .Bl -tag -width "XXXXXX" -offset indent
419 Make the substitution only for the N'th occurrence of the regular
420 expression in the pattern space.
422 Make the substitution for all non-overlapping matches of the
423 regular expression, not just the first one.
425 Write the pattern space to standard output if a replacement was made.
426 If the replacement string is identical to that which it replaces, it
427 is still considered to have been a replacement.
429 Append the pattern space to
431 if a replacement was made.
432 If the replacement string is identical to that which it replaces, it
433 is still considered to have been a replacement.
439 function bearing the label if any substitutions have been made since the
440 most recent reading of an input line or execution of a
443 If no label is specified, branch to the end of the script.
446 Append the pattern space to the
450 Swap the contents of the pattern and hold spaces.
452 .It [2addr]y/string1/string2/
453 Replace all occurrences of characters in
455 in the pattern space with the corresponding characters from
457 Any character other than a backslash or newline can be used instead of
458 a slash to delimit the strings.
463 a backslash followed by any character other than a newline is that literal
464 character, and a backslash followed by an ``n'' is replaced by a newline
468 .It [2addr]!function-list
469 Apply the function or function-list only to the lines that are
471 selected by the address(es).
474 This function does nothing; it bears a label to which the
481 Write the line number to the standard output followed by a newline
485 Empty lines are ignored.
490 and the remainder of the line are ignored (treated as a comment), with
491 the single exception that if the first two characters in the file are
493 the default output is suppressed.
494 This is the same as specifying the
496 option on the command line.
501 utility exits 0 on success and \*[Gt]0 if an error occurs.
511 function is expected to be a superset of the