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. Neither the name of the University nor the names of its contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" @(#)sed.1 8.2 (Berkeley) 12/30/93
32 .\" $FreeBSD: head/usr.bin/sed/sed.1 362017 2020-06-10 19:23:58Z 0mp $
50 .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.
101 The commands are read from the standard input if
105 .It Fl I Ar extension
106 Edit files in-place, saving backups with the specified
110 is given, no backup will be saved.
111 It is not recommended to give a zero-length
113 when in-place editing files, as you risk corruption or partial content
114 in situations where disk space is exhausted, etc.
116 Note that in-place editing with
118 still takes place in a single continuous line address space covering
119 all files, although each file preserves its individuality instead of
120 forming one output stream.
121 The line counter is never reset between files, address ranges can span
122 file boundaries, and the
124 address matches only the last line of the last file.
126 .Sx "Sed Addresses" . )
127 That can lead to unexpected results in many cases of in-place editing,
131 .It Fl i Ar extension
132 Edit files in-place similarly to
134 but treat each file independently from other files.
135 In particular, line numbers in each file start at 1,
138 address matches the last line of the current file,
139 and address ranges are limited to the current file.
141 .Sx "Sed Addresses" . )
142 The net result is as though each file were edited by a separate
146 Make output line buffered.
148 By default, each line of input is echoed to the standard output after
149 all of the commands have been applied to it.
152 option suppresses this behavior.
156 for compatibility with GNU sed.
158 Make output unbuffered.
163 command is as follows:
165 .Dl [address[,address]]function[arguments]
167 Whitespace may be inserted before the first address and the function
168 portions of the command.
172 cyclically copies a line of input, not including its terminating newline
174 .Em "pattern space" ,
175 (unless there is something left after a
178 applies all of the commands with addresses that select that pattern space,
179 copies the pattern space to the standard output, appending a newline, and
180 deletes the pattern space.
182 Some of the functions use a
184 to save all or part of the pattern space for subsequent retrieval.
186 An address is not required, but if specified must have one of the
188 .Bl -bullet -offset indent
192 cumulatively across input files (or in each file independently
195 option is in effect);
199 character that addresses the last line of input (or the last line
200 of the current file if a
202 option was specified);
205 that consists of a regular expression preceded and followed by a
207 The closing delimiter can also optionally be followed by the
209 character, to indicate that the regular expression is to be matched
210 in a case-insensitive way.
213 A command line with no addresses selects every pattern space.
215 A command line with one address selects all of the pattern spaces
216 that match the address.
218 A command line with two addresses selects an inclusive range.
220 range starts with the first pattern space that matches the first
222 The end of the range is the next following pattern space
223 that matches the second address.
224 If the second address is a number
225 less than or equal to the line number first selected, only that
227 The number in the second address may be prefixed with a
229 to specify the number of lines to match after the first pattern.
230 In the case when the second address is a context
233 does not re-match the second address against the
234 pattern space that matched the first address.
236 first line following the selected range,
238 starts looking again for the first address.
240 Editing commands can be applied to non-selected pattern spaces by use
241 of the exclamation character
244 .Sh "Sed Regular Expressions"
245 The regular expressions used in
247 by default, are basic regular expressions (BREs, see
249 for more information), but extended (modern) regular expressions can be used
255 has the following two additions to regular expressions:
259 In a context address, any character other than a backslash
261 or newline character may be used to delimit the regular expression.
262 The opening delimiter needs to be preceded by a backslash
263 unless it is a slash.
264 For example, the context address
268 Also, putting a backslash character before the delimiting character
269 within the regular expression causes the character to be treated literally.
270 For example, in the context address
272 the RE delimiter is an
276 stands for itself, so that the regular expression is
280 The escape sequence \en matches a newline character embedded in the
282 You cannot, however, use a literal newline character in an address or
283 in the substitute command.
286 One special feature of
288 regular expressions is that they can default to the last regular
290 If a regular expression is empty, i.e., just the delimiter characters
291 are specified, the last regular expression encountered is used instead.
292 The last regular expression is defined as the last regular expression
293 used as part of an address or substitute command, and at run-time, not
295 For example, the command
302 In the following list of commands, the maximum number of permissible
303 addresses for each command is indicated by [0addr], [1addr], or [2addr],
304 representing zero, one, or two addresses.
308 consists of one or more lines.
309 To embed a newline in the text, precede it with a backslash.
310 Other backslashes in text are deleted and the following character
317 functions take an optional file parameter, which should be separated
318 from the function letter by white space.
319 Each file given as an argument to
321 is created (or its contents truncated) before any input processing begins.
333 functions all accept additional arguments.
334 The following synopses indicate which arguments have to be separated from
335 the function letters by white space characters.
337 Two of the functions take a function-list.
340 functions separated by newlines, as follows:
341 .Bd -literal -offset indent
351 can be preceded by white space and can be followed by white space.
352 The function can be preceded by white space.
355 must be preceded by a newline, and may also be preceded by white space.
357 .Bl -tag -width "XXXXXX" -compact
358 .It [2addr] function-list
359 Execute function-list only when the pattern space is selected.
365 to standard output immediately before each attempt to read a line of input,
366 whether by executing the
368 function or by beginning a new cycle.
373 function with the specified label.
374 If the label is not specified, branch to the end of the script.
378 Delete the pattern space.
379 With 0 or 1 address or at the end of a 2-address range,
381 is written to the standard output.
384 Delete the pattern space and start the next cycle.
387 Delete the initial segment of the pattern space through the first
388 newline character and start the next cycle.
391 Replace the contents of the pattern space with the contents of the
395 Append a newline character followed by the contents of the hold space
396 to the pattern space.
399 Replace the contents of the hold space with the contents of the
403 Append a newline character followed by the contents of the pattern space
410 to the standard output.
414 Write the pattern space to the standard output in a visually unambiguous
416 This form is as follows:
418 .Bl -tag -width "carriage-returnXX" -offset indent -compact
433 Nonprintable characters are written as three-digit octal numbers (with a
434 preceding backslash) for each byte in the character (most significant byte
436 Long lines are folded, with the point of folding indicated by displaying
437 a backslash followed by a newline.
438 The end of each line is marked with a
442 Write the pattern space to the standard output if the default output has
443 not been suppressed, and replace the pattern space with the next line of
447 Append the next line of input to the pattern space, using an embedded
448 newline character to separate the appended material from the original
450 Note that the current line number changes.
453 Write the pattern space to standard output.
456 Write the pattern space, up to the first newline character to the
460 Branch to the end of the script and quit without starting a new cycle.
465 to the standard output immediately before the next attempt to read a
469 cannot be read for any reason, it is silently ignored and no error
472 .It [2addr]s/regular expression/replacement/flags
473 Substitute the replacement string for the first instance of the regular
474 expression in the pattern space.
475 Any character other than backslash or newline can be used instead of
476 a slash to delimit the RE and the replacement.
477 Within the RE and the replacement, the RE delimiter itself can be used as
478 a literal character if it is preceded by a backslash.
482 appearing in the replacement is replaced by the string matching the RE.
483 The special meaning of
485 in this context can be suppressed by preceding it by a backslash.
490 is a digit, is replaced by the text matched
491 by the corresponding backreference expression (see
494 A line can be split by substituting a newline character into it.
495 To specify a newline character in the replacement string, precede it with
500 in the substitute function is zero or more of the following:
501 .Bl -tag -width "XXXXXX" -offset indent
503 Make the substitution only for the
505 occurrence of the regular expression in the pattern space.
507 Make the substitution for all non-overlapping matches of the
508 regular expression, not just the first one.
510 Write the pattern space to standard output if a replacement was made.
511 If the replacement string is identical to that which it replaces, it
512 is still considered to have been a replacement.
514 Append the pattern space to
516 if a replacement was made.
517 If the replacement string is identical to that which it replaces, it
518 is still considered to have been a replacement.
520 Match the regular expression in a case-insensitive way.
526 function bearing the label if any substitutions have been made since the
527 most recent reading of an input line or execution of a
530 If no label is specified, branch to the end of the script.
533 Append the pattern space to the
537 Swap the contents of the pattern and hold spaces.
539 .It [2addr]y/string1/string2/
540 Replace all occurrences of characters in
542 in the pattern space with the corresponding characters from
544 Any character other than a backslash or newline can be used instead of
545 a slash to delimit the strings.
550 a backslash followed by any character other than a newline is that literal
551 character, and a backslash followed by an ``n'' is replaced by a newline
555 .It [2addr]!function-list
556 Apply the function or function-list only to the lines that are
558 selected by the address(es).
561 This function does nothing; it bears a label to which the
568 Write the line number to the standard output followed by a newline
572 Empty lines are ignored.
577 and the remainder of the line are ignored (treated as a comment), with
578 the single exception that if the first two characters in the file are
580 the default output is suppressed.
581 This is the same as specifying the
583 option on the command line.
587 .Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
590 environment variables affect the execution of
601 when piped from another command:
602 .Bd -literal -offset indent
603 echo "An alternate word, like bar, is sometimes used in examples." | sed 's/bar/baz/'
606 Using backlashes can sometimes be hard to read and follow:
607 .Bd -literal -offset indent
608 echo "/home/example" | sed 's/\\/home\\/example/\\/usr\\/local\\/example/'
611 Using a different separator can be handy when working with paths:
612 .Bd -literal -offset indent
613 echo "/home/example" | sed 's#/home/example#/usr/local/example#'
616 Replace all occurrences of
622 without creating a backup of the file:
623 .Bd -literal -offset indent
624 sed -i '' -e 's/foo/bar/g' test.txt
635 utility is expected to be a superset of the
643 options, the special meaning of
647 in the second member of an address range,
650 flag to the address regular expression and substitution command are
653 extensions and may not be available on other operating systems.
662 .An Diomidis D. Spinellis Aq Mt dds@FreeBSD.org
664 Multibyte characters containing a byte with value 0x5C
667 may be incorrectly treated as line continuation characters in arguments to the
673 Multibyte characters cannot be used as delimiters with the