1 .\" Copyright (c) 1992, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the Institute of Electrical and Electronics Engineers, Inc.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" @(#)sed.1 8.2 (Berkeley) 12/30/93
52 .Op Fl f Ar command_file
57 utility reads the specified files, or the standard input if no files
58 are specified, modifying the input as specified by a list of commands.
59 The input is then written to the standard output.
61 A single command may be specified as the first argument to
63 Multiple commands may be specified by using the
68 All commands are applied to the input in the order they are specified
69 regardless of their origin.
71 The following options are available:
72 .Bl -tag -width indent
74 Interpret regular expressions as extended (modern) regular expressions
75 rather than basic regular expressions (BRE's).
78 manual page fully describes both formats.
80 The files listed as parameters for the
82 functions are created (or truncated) before any processing begins,
88 to delay opening each file until a command containing the related
90 function is applied to a line of input.
92 Append the editing commands specified by the
95 to the list of commands.
96 .It Fl f Ar command_file
97 Append the editing commands found in the file
99 to the list of commands.
100 The editing commands should each be listed on a separate line.
102 By default, each line of input is echoed to the standard output after
103 all of the commands have been applied to it.
106 option suppresses this behavior.
111 command is as follows:
113 .Dl [address[,address]]function[arguments]
115 Whitespace may be inserted before the first address and the function
116 portions of the command.
120 cyclically copies a line of input, not including its terminating newline
122 .Em "pattern space" ,
123 (unless there is something left after a
126 applies all of the commands with addresses that select that pattern space,
127 copies the pattern space to the standard output, appending a newline, and
128 deletes the pattern space.
130 Some of the functions use a
132 to save all or part of the pattern space for subsequent retrieval.
134 An address is not required, but if specified must be a number (that counts
136 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 an inclusive range. This
148 range starts with the first pattern space that matches the first
149 address. The end of the range is the next following pattern space
150 that matches the second address. If the second address is a number
151 less than or equal to the line number first selected, only that
152 line is selected. In the case when the second address is a context
153 address, sed does not re-match the second address against the
154 pattern space that matched the first address. Starting at the
155 first line following the selected range, sed starts looking again
156 for the first address.
158 starts looking again for the first address.
160 Editing commands can be applied to non-selected pattern spaces by use
161 of the exclamation character
164 .Sh "Sed Regular Expressions"
165 The regular expressions used in
167 by default, are basic regular expressions (BREs, see
169 for more information).
171 can use extended (modern) regular expressions instead if the
176 has the following two additions to regular expressions:
180 In a context address, any character other than a backslash
182 or newline character may be used to delimit the regular expression.
183 Also, putting a backslash character before the delimiting character
184 causes the character to be treated literally.
185 For example, in the context address \exabc\exdefx, the RE delimiter
190 stands for itself, so that the regular expression is
194 The escape sequence \en matches a newline character embedded in the
196 You can't, however, use a literal newline character in an address or
197 in the substitute command.
200 One special feature of
202 regular expressions is that they can default to the last regular
204 If a regular expression is empty, i.e. just the delimiter characters
205 are specified, the last regular expression encountered is used instead.
206 The last regular expression is defined as the last regular expression
207 used as part of an address or substitute command, and at run-time, not
209 For example, the command
216 In the following list of commands, the maximum number of permissible
217 addresses for each command is indicated by [0addr], [1addr], or [2addr],
218 representing zero, one, or two addresses.
222 consists of one or more lines.
223 To embed a newline in the text, precede it with a backslash.
224 Other backslashes in text are deleted and the following character
231 functions take an optional file parameter, which should be separated
232 from the function letter by white space.
233 Each file given as an argument to
235 is created (or its contents truncated) before any input processing begins.
247 functions all accept additional arguments.
248 The following synopses indicate which arguments have to be separated from
249 the function letters by white space characters.
251 Two of the functions take a function-list.
254 functions separated by newlines, as follows:
255 .Bd -literal -offset indent
265 can be preceded by white space and can be followed by white space.
266 The function can be preceded by white space.
269 must be preceded by a newline or optional white space.
271 .Bl -tag -width "XXXXXX" -compact
272 .It [2addr] function-list
273 Execute function-list only when the pattern space is selected.
279 to standard output immediately before each attempt to read a line of input,
280 whether by executing the
282 function or by beginning a new cycle.
287 function with the specified label.
288 If the label is not specified, branch to the end of the script.
292 Delete the pattern space.
293 With 0 or 1 address or at the end of a 2-address range,
295 is written to the standard output.
298 Delete the pattern space and start the next cycle.
301 Delete the initial segment of the pattern space through the first
302 newline character and start the next cycle.
305 Replace the contents of the pattern space with the contents of the
309 Append a newline character followed by the contents of the hold space
310 to the pattern space.
313 Replace the contents of the hold space with the contents of the
317 Append a newline character followed by the contents of the pattern space
324 to the standard output.
328 Write the pattern space to the standard output in a visually unambiguous
330 This form is as follows:
332 .Bl -tag -width "carriage-returnXX" -offset indent -compact
349 Nonprintable characters are written as three-digit octal numbers (with a
350 preceding backslash) for each byte in the character (most significant byte
352 Long lines are folded, with the point of folding indicated by displaying
353 a backslash followed by a newline.
354 The end of each line is marked with a
358 Write the pattern space to the standard output if the default output has
359 not been suppressed, and replace the pattern space with the next line of
363 Append the next line of input to the pattern space, using an embedded
364 newline character to separate the appended material from the original
366 Note that the current line number changes.
369 Write the pattern space to standard output.
372 Write the pattern space, up to the first newline character to the
376 Branch to the end of the script and quit without starting a new cycle.
381 to the standard output immediately before the next attempt to read a
385 cannot be read for any reason, it is silently ignored and no error
388 .It [2addr]s/regular expression/replacement/flags
389 Substitute the replacement string for the first instance of the regular
390 expression in the pattern space.
391 Any character other than backslash or newline can be used instead of
392 a slash to delimit the RE and the replacement.
393 Within the RE and the replacement, the RE delimiter itself can be used as
394 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.
514 function is expected to be a superset of the