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
36 .\" $FreeBSD: src/usr.bin/sed/sed.1,v 1.9.2.10 2003/02/24 23:04:39 trhodes Exp $
37 .\" $DragonFly: src/usr.bin/sed/sed.1,v 1.3 2004/03/11 12:28:58 hmp Exp $
53 .Op Fl f Ar command_file
59 utility reads the specified files, or the standard input if no files
60 are specified, modifying the input as specified by a list of commands.
61 The input is then written to the standard output.
63 A single command may be specified as the first argument to
65 Multiple commands may be specified by using the
70 All commands are applied to the input in the order they are specified
71 regardless of their origin.
73 The following options are available:
74 .Bl -tag -width indent
76 Interpret regular expressions as extended (modern) regular expressions
77 rather than basic regular expressions (BRE's).
80 manual page fully describes both formats.
82 The files listed as parameters for the
84 functions are created (or truncated) before any processing begins,
90 to delay opening each file until a command containing the related
92 function is applied to a line of input.
94 Append the editing commands specified by the
97 to the list of commands.
98 .It Fl f Ar command_file
99 Append the editing commands found in the file
101 to the list of commands.
102 The editing commands should each be listed on a separate line.
103 .It Fl i Ar extension
104 Edit files in-place, saving backups with the specified
108 is given, no backup will be saved.
109 It is not recommended to give a zero-length
111 when in-place editing files, as you risk corruption or partial content
112 in situations where disk space is exhausted, etc.
114 By default, each line of input is echoed to the standard output after
115 all of the commands have been applied to it.
118 option suppresses this behavior.
123 command is as follows:
125 .Dl [address[,address]]function[arguments]
127 Whitespace may be inserted before the first address and the function
128 portions of the command.
132 cyclically copies a line of input, not including its terminating newline
134 .Em "pattern space" ,
135 (unless there is something left after a
138 applies all of the commands with addresses that select that pattern space,
139 copies the pattern space to the standard output, appending a newline, and
140 deletes the pattern space.
142 Some of the functions use a
144 to save all or part of the pattern space for subsequent retrieval.
146 An address is not required, but if specified must be a number (that counts
148 cumulatively across input files), a dollar
150 character that addresses the last line of input, or a context address
151 (which consists of a regular expression preceded and followed by a
154 A command line with no addresses selects every pattern space.
156 A command line with one address selects all of the pattern spaces
157 that match the address.
159 A command line with two addresses selects an inclusive range. This
160 range starts with the first pattern space that matches the first
161 address. The end of the range is the next following pattern space
162 that matches the second address. If the second address is a number
163 less than or equal to the line number first selected, only that
164 line is selected. In the case when the second address is a context
165 address, sed does not re-match the second address against the
166 pattern space that matched the first address. Starting at the
167 first line following the selected range,
169 starts looking again for the first address.
171 Editing commands can be applied to non-selected pattern spaces by use
172 of the exclamation character
175 .Sh "Sed Regular Expressions"
176 The regular expressions used in
178 by default, are basic regular expressions (BREs, see
180 for more information).
182 can use extended (modern) regular expressions instead if the
187 has the following two additions to regular expressions:
191 In a context address, any character other than a backslash
193 or newline character may be used to delimit the regular expression.
194 Also, putting a backslash character before the delimiting character
195 causes the character to be treated literally.
196 For example, in the context address \exabc\exdefx, the RE delimiter
201 stands for itself, so that the regular expression is
205 The escape sequence \en matches a newline character embedded in the
207 You can't, however, use a literal newline character in an address or
208 in the substitute command.
211 One special feature of
213 regular expressions is that they can default to the last regular
215 If a regular expression is empty, i.e. just the delimiter characters
216 are specified, the last regular expression encountered is used instead.
217 The last regular expression is defined as the last regular expression
218 used as part of an address or substitute command, and at run-time, not
220 For example, the command
227 In the following list of commands, the maximum number of permissible
228 addresses for each command is indicated by [0addr], [1addr], or [2addr],
229 representing zero, one, or two addresses.
233 consists of one or more lines.
234 To embed a newline in the text, precede it with a backslash.
235 Other backslashes in text are deleted and the following character
242 functions take an optional file parameter, which should be separated
243 from the function letter by white space.
244 Each file given as an argument to
246 is created (or its contents truncated) before any input processing begins.
258 functions all accept additional arguments.
259 The following synopses indicate which arguments have to be separated from
260 the function letters by white space characters.
262 Two of the functions take a function-list.
265 functions separated by newlines, as follows:
266 .Bd -literal -offset indent
276 can be preceded by white space and can be followed by white space.
277 The function can be preceded by white space.
280 must be preceded by a newline or optional white space.
282 .Bl -tag -width "XXXXXX" -compact
283 .It [2addr] function-list
284 Execute function-list only when the pattern space is selected.
290 to standard output immediately before each attempt to read a line of input,
291 whether by executing the
293 function or by beginning a new cycle.
298 function with the specified label.
299 If the label is not specified, branch to the end of the script.
303 Delete the pattern space.
304 With 0 or 1 address or at the end of a 2-address range,
306 is written to the standard output.
309 Delete the pattern space and start the next cycle.
312 Delete the initial segment of the pattern space through the first
313 newline character and start the next cycle.
316 Replace the contents of the pattern space with the contents of the
320 Append a newline character followed by the contents of the hold space
321 to the pattern space.
324 Replace the contents of the hold space with the contents of the
328 Append a newline character followed by the contents of the pattern space
335 to the standard output.
339 Write the pattern space to the standard output in a visually unambiguous
341 This form is as follows:
343 .Bl -tag -width "carriage-returnXX" -offset indent -compact
358 Nonprintable characters are written as three-digit octal numbers (with a
359 preceding backslash) for each byte in the character (most significant byte
361 Long lines are folded, with the point of folding indicated by displaying
362 a backslash followed by a newline.
363 The end of each line is marked with a
367 Write the pattern space to the standard output if the default output has
368 not been suppressed, and replace the pattern space with the next line of
372 Append the next line of input to the pattern space, using an embedded
373 newline character to separate the appended material from the original
375 Note that the current line number changes.
378 Write the pattern space to standard output.
381 Write the pattern space, up to the first newline character to the
385 Branch to the end of the script and quit without starting a new cycle.
390 to the standard output immediately before the next attempt to read a
394 cannot be read for any reason, it is silently ignored and no error
397 .It [2addr]s/regular expression/replacement/flags
398 Substitute the replacement string for the first instance of the regular
399 expression in the pattern space.
400 Any character other than backslash or newline can be used instead of
401 a slash to delimit the RE and the replacement.
402 Within the RE and the replacement, the RE delimiter itself can be used as
403 a literal character if it is preceded by a backslash.
407 appearing in the replacement is replaced by the string matching the RE.
408 The special meaning of
410 in this context can be suppressed by preceding it by a backslash.
415 is a digit, is replaced by the text matched
416 by the corresponding backreference expression (see
419 A line can be split by substituting a newline character into it.
420 To specify a newline character in the replacement string, precede it with
425 in the substitute function is zero or more of the following:
426 .Bl -tag -width "XXXXXX" -offset indent
428 Make the substitution only for the
430 occurrence of the regular expression in the pattern space.
432 Make the substitution for all non-overlapping matches of the
433 regular expression, not just the first one.
435 Write the pattern space to standard output if a replacement was made.
436 If the replacement string is identical to that which it replaces, it
437 is still considered to have been a replacement.
439 Append the pattern space to
441 if a replacement was made.
442 If the replacement string is identical to that which it replaces, it
443 is still considered to have been a replacement.
449 function bearing the label if any substitutions have been made since the
450 most recent reading of an input line or execution of a
453 If no label is specified, branch to the end of the script.
456 Append the pattern space to the
460 Swap the contents of the pattern and hold spaces.
462 .It [2addr]y/string1/string2/
463 Replace all occurrences of characters in
465 in the pattern space with the corresponding characters from
467 Any character other than a backslash or newline can be used instead of
468 a slash to delimit the strings.
473 a backslash followed by any character other than a newline is that literal
474 character, and a backslash followed by an ``n'' is replaced by a newline
478 .It [2addr]!function-list
479 Apply the function or function-list only to the lines that are
481 selected by the address(es).
484 This function does nothing; it bears a label to which the
491 Write the line number to the standard output followed by a newline
495 Empty lines are ignored.
500 and the remainder of the line are ignored (treated as a comment), with
501 the single exception that if the first two characters in the file are
503 the default output is suppressed.
504 This is the same as specifying the
506 option on the command line.
510 .Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
513 environment variables affect the execution of
528 utility is expected to be a superset of the
534 option is a non-standard
536 extension and may not be available on other operating systems.