1 .\" Copyright (c) 1992, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Copyright 2011 Nexenta Systems, Inc. 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 .\" 4. 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 .TH SED 1 "Feb 14, 2015"
45 [\fB\-e\fP \fIcommand\fP]
46 [\fB\-f\fP \fIcommand_file\fP]
47 [\fB\-I\fP[\fIextension\fP] | \fB\-i\fP[\fIextension\fP]]
52 utility reads the specified files, or the standard input if no files
53 are specified, modifying the input as specified by a list of commands.
54 The input is then written to the standard output.
56 A single command may be specified as the first argument to
58 Multiple commands may be specified by using the
63 All commands are applied to the input in the order they are specified
64 regardless of their origin.
66 The following options are available:
69 Interpret regular expressions as extended (modern) regular expressions
70 rather than basic regular expressions (BRE's).
73 manual page fully describes both formats.
76 The files listed as parameters for the
78 functions are created (or truncated) before any processing begins,
84 to delay opening each file until a command containing the related
86 function is applied to a line of input.
88 \fB\-e\fP \fIcommand\fP
89 Append the editing commands specified by the
92 to the list of commands.
94 \fB\-f\fP \fIcommand_file\fP
95 Append the editing commands found in the file
97 to the list of commands.
98 The editing commands should each be listed on a separate line.
100 \fB\-I\fP[\fIextension\fP]
101 Edit files in-place, saving backups if \fIextension\fP was specified.
102 It is not recommended to omit saving backups when in-place editing files,
103 as you risk corruption or partial content in situations where disk
104 space is exhausted, etc.
106 Note that in-place editing with
108 still takes place in a single continuous line address space covering
109 all files, although each file preserves its individuality instead of
110 forming one output stream.
111 The line counter is never reset between files, address ranges can span
112 file boundaries, and the
114 address matches only the last line of the last file.
116 .B "Sed Addresses" . )
117 That can lead to unexpected results in many cases of in-place editing,
122 \fB\-i\fP[\fIextension\fP]
123 Edit files in-place similarly to
125 but treat each file independently from other files.
126 In particular, line numbers in each file start at 1,
129 address matches the last line of the current file,
130 and address ranges are limited to the current file.
132 .B "Sed Addresses" . )
133 The net result is as though each file were edited by a separate
138 Make output line buffered.
141 By default, each line of input is echoed to the standard output after
142 all of the commands have been applied to it.
145 option suppresses this behavior.
150 for compatibility with GNU sed.
154 command is as follows:
156 [address[,address]]function[arguments]
158 Whitespace may be inserted before the first address and the function
159 portions of the command.
163 cyclically copies a line of input, not including its terminating newline
165 .IR "pattern space" ,
166 (unless there is something left after a
169 applies all of the commands with addresses that select that pattern space,
170 copies the pattern space to the standard output, appending a newline, and
171 deletes the pattern space.
173 Some of the functions use a
175 to save all or part of the pattern space for subsequent retrieval.
177 An address is not required, but if specified must have one of the
182 cumulatively across input files (or in each file independently
185 option is in effect);
189 character that addresses the last line of input (or the last line
190 of the current file if a
192 option was specified);
195 that consists of a regular expression preceded and followed by a
196 delimiter. The closing delimiter can also optionally be followed by the
198 character, to indicate that the regular expression is to be matched
199 in a case-insensitive way.
201 A command line with no addresses selects every pattern space.
203 A command line with one address selects all of the pattern spaces
204 that match the address.
206 A command line with two addresses selects an inclusive range.
208 range starts with the first pattern space that matches the first
210 The end of the range is the next following pattern space
211 that matches the second address.
212 If the second address is a number
213 less than or equal to the line number first selected, only that
215 The number in the second address may be prefixed with a
217 to specify the number of lines to match after the first pattern.
218 In the case when the second address is a context
221 does not re-match the second address against the
222 pattern space that matched the first address.
224 first line following the selected range,
226 starts looking again for the first address.
228 Editing commands can be applied to non-selected pattern spaces by use
229 of the exclamation character
232 .SH "Sed Regular Expressions"
233 The regular expressions used in
235 by default, are basic regular expressions (BREs, see
237 for more information), but extended (modern) regular expressions can be used
243 has the following two additions to regular expressions:
246 In a context address, any character other than a backslash
248 or newline character may be used to delimit the regular expression.
249 The opening delimiter needs to be preceded by a backslash
250 unless it is a slash.
251 For example, the context address
255 Also, putting a backslash character before the delimiting character
256 within the regular expression causes the character to be treated literally.
257 For example, in the context address
259 the RE delimiter is an
263 stands for itself, so that the regular expression is
267 The escape sequence \en matches a newline character embedded in the
269 You cannot, however, use a literal newline character in an address or
270 in the substitute command.
272 One special feature of
274 regular expressions is that they can default to the last regular
276 If a regular expression is empty, i.e., just the delimiter characters
277 are specified, the last regular expression encountered is used instead.
278 The last regular expression is defined as the last regular expression
279 used as part of an address or substitute command, and at run-time, not
281 For example, the command
288 In the following list of commands, the maximum number of permissible
289 addresses for each command is indicated by [0addr], [1addr], or [2addr],
290 representing zero, one, or two addresses.
294 consists of one or more lines.
295 To embed a newline in the text, precede it with a backslash.
296 Other backslashes in text are deleted and the following character
303 functions take an optional file parameter, which should be separated
304 from the function letter by white space.
305 Each file given as an argument to
307 is created (or its contents truncated) before any input processing begins.
319 functions all accept additional arguments.
320 The following synopses indicate which arguments have to be separated from
321 the function letters by white space characters.
323 Two of the functions take a function-list.
326 functions separated by newlines, as follows:
341 can be preceded by white space and can be followed by white space.
342 The function can be preceded by white space.
345 must be preceded by a newline or optional white space.
348 [2addr] function-list
349 Execute function-list only when the pattern space is selected.
357 to standard output immediately before each attempt to read a line of input,
358 whether by executing the
360 function or by beginning a new cycle.
366 function with the specified label.
367 If the label is not specified, branch to the end of the script.
373 Delete the pattern space.
374 With 0 or 1 address or at the end of a 2-address range,
376 is written to the standard output.
380 Delete the pattern space and start the next cycle.
384 Delete the initial segment of the pattern space through the first
385 newline character and start the next cycle.
389 Replace the contents of the pattern space with the contents of the
394 Append a newline character followed by the contents of the hold space
395 to the pattern space.
399 Replace the contents of the hold space with the contents of the
404 Append a newline character followed by the contents of the pattern space
413 to the standard output.
418 Write the pattern space to the standard output in a visually unambiguous
420 This form is as follows:
441 Nonprintable characters are written as three-digit octal numbers (with a
442 preceding backslash) for each byte in the character (most significant byte
444 Long lines are folded, with the point of folding indicated by displaying
445 a backslash followed by a newline.
446 The end of each line is marked with a
451 Write the pattern space to the standard output if the default output has
452 not been suppressed, and replace the pattern space with the next line of
457 Append the next line of input to the pattern space, using an embedded
458 newline character to separate the appended material from the original
460 Note that the current line number changes.
464 Write the pattern space to standard output.
468 Write the pattern space, up to the first newline character to the
473 Branch to the end of the script and quit without starting a new cycle.
479 to the standard output immediately before the next attempt to read a
483 cannot be read for any reason, it is silently ignored and no error
487 [2addr]s/regular expression/replacement/flags
488 Substitute the replacement string for the first instance of the regular
489 expression in the pattern space.
490 Any character other than backslash or newline can be used instead of
491 a slash to delimit the RE and the replacement.
492 Within the RE and the replacement, the RE delimiter itself can be used as
493 a literal character if it is preceded by a backslash.
497 appearing in the replacement is replaced by the string matching the RE.
498 The special meaning of
500 in this context can be suppressed by preceding it by a backslash.
505 is a digit, is replaced by the text matched
506 by the corresponding backreference expression (see
509 A line can be split by substituting a newline character into it.
510 To specify a newline character in the replacement string, precede it with
515 in the substitute function is zero or more of the following:
518 Make the substitution only for the
520 occurrence of the regular expression in the pattern space.
523 Make the substitution for all non-overlapping matches of the
524 regular expression, not just the first one.
527 Write the pattern space to standard output if a replacement was made.
528 If the replacement string is identical to that which it replaces, it
529 is still considered to have been a replacement.
532 Append the pattern space to
534 if a replacement was made.
535 If the replacement string is identical to that which it replaces, it
536 is still considered to have been a replacement.
539 Match the regular expression in a case-insensitive way.
545 function bearing the label if any substitutions have been made since the
546 most recent reading of an input line or execution of a
549 If no label is specified, branch to the end of the script.
553 Append the pattern space to the
558 Swap the contents of the pattern and hold spaces.
561 [2addr]y/string1/string2/
562 Replace all occurrences of characters in
564 in the pattern space with the corresponding characters from
566 Any character other than a backslash or newline can be used instead of
567 a slash to delimit the strings.
572 a backslash followed by any character other than a newline is that literal
573 character, and a backslash followed by an ``n'' is replaced by a newline
579 [2addr]!function-list
580 Apply the function or function-list only to the lines that are
582 selected by the address(es).
586 This function does nothing; it bears a label to which the
594 Write the line number to the standard output followed by a newline
599 Empty lines are ignored.
605 and the remainder of the line are ignored (treated as a comment), with
606 the single exception that if the first two characters in the file are
608 the default output is suppressed.
609 This is the same as specifying the
611 option on the command line.
614 .IR COLUMNS , LANG , LC_ALL , LC_CTYPE
617 environment variables affect the execution of
622 The \fBsed\fP utility exits 0 on success, and >0 if an error occurs.
631 utility is expected to be a superset of the IEEE Std 1003.2 (``POSIX.2'')
638 options, the prefixing
640 in the second member of an address range,
643 flag to the address regular expression and substitution command are
644 non-standard extensions and may not be available on other operating systems.
648 command, written by L. E. McMahon, appeared in Version 7 AT&T UNIX.
651 "Diomidis D. Spinellis" <dds@FreeBSD.org>
653 Multibyte characters containing a byte with value 0x5C (ASCII `\e')
654 may be incorrectly treated as line continuation characters in arguments to the
660 Multibyte characters cannot be used as delimiters with the