1 .\" $NetBSD: sed.1,v 1.40 2014/06/25 02:05:58 uwe Exp $
2 .\" Copyright (c) 1992, 1993
3 .\" The Regents of the University of California. All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
6 .\" the Institute of Electrical and Electronics Engineers, Inc.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)sed.1 8.2 (Berkeley) 12/30/93
33 .\" $FreeBSD: head/usr.bin/sed/sed.1 259132 2013-12-09 18:57:20Z eadler $
49 .Op Fl f Ar command_file
50 .Op Fl I Ns Op Ar extension
51 .Op Fl i Ns Op Ar extension
56 utility reads the specified files, or the standard input if no files
57 are specified, modifying the input as specified by a list of commands.
58 The input is then written to the standard output.
60 A single command may be specified as the first argument to
62 Multiple commands may be specified by using the
67 All commands are applied to the input in the order they are specified
68 regardless of their origin.
70 The following options are available:
71 .Bl -tag -width indent
73 The files listed as parameters for the
75 functions are created (or truncated) before any processing begins,
81 to delay opening each file until a command containing the related
83 function is applied to a line of input.
85 Interpret regular expressions as extended (modern) regular expressions
86 rather than basic regular expressions (BRE's).
89 manual page fully describes both formats.
91 Append the editing commands specified by the
94 to the list of commands.
95 .It Fl f Ar command_file
96 Append the editing commands found in the file
98 to the list of commands.
99 The editing commands should each be listed on a separate line.
100 .It Fl I Ns Op Ar extension
101 Edit files in-place, saving backups with the specified
105 is given, no backup will be saved.
106 It is not recommended to give a zero-length
108 when in-place editing files, as you risk corruption or partial content
109 in situations where disk space is exhausted, etc.
111 Note that in-place editing with
113 still takes place in a single continuous line address space covering
114 all files, although each file preserves its individuality instead of
115 forming one output stream.
116 The line counter is never reset between files, address ranges can span
117 file boundaries, and the
119 address matches only the last line of the last file.
121 .Sx "Sed Addresses" . )
122 That can lead to unexpected results in many cases of in-place editing,
126 .It Fl i Ns Op Ar extension
127 Edit files in-place similarly to
129 but treat each file independently from other files.
130 In particular, line numbers in each file start at 1,
133 address matches the last line of the current file,
134 and address ranges are limited to the current file.
136 .Sx "Sed Addresses" . )
137 The net result is as though each file were edited by a separate
141 Make output line buffered.
143 By default, each line of input is echoed to the standard output after
144 all of the commands have been applied to it.
147 option suppresses this behavior.
151 for compatibility with GNU sed.
153 Make output unbuffered.
158 command is as follows:
160 .Dl [address[,address]]function[arguments]
162 Whitespace may be inserted before the first address and the function
163 portions of the command.
167 cyclically copies a line of input, not including its terminating newline
169 .Em "pattern space" ,
170 (unless there is something left after a
173 applies all of the commands with addresses that select that pattern space,
174 copies the pattern space to the standard output, appending a newline, and
175 deletes the pattern space.
177 Some of the functions use a
179 to save all or part of the pattern space for subsequent retrieval.
181 An address is not required, but if specified must have one of the
183 .Bl -bullet -offset indent
187 cumulatively across input files (or in each file independently
190 option is in effect);
194 character that addresses the last line of input (or the last line
195 of the current file if a
197 option was specified);
200 that consists of a regular expression preceded and followed by a
202 The closing delimiter can also optionally be followed by the
204 character, to indicate that the regular expression is to be matched
205 in a case-insensitive way.
208 A command line with no addresses selects every pattern space.
210 A command line with one address selects all of the pattern spaces
211 that match the address.
213 A command line with two addresses selects an inclusive range.
215 range starts with the first pattern space that matches the first
217 The end of the range is the next following pattern space
218 that matches the second address.
219 If the second address is a number
220 less than or equal to the line number first selected, only that
222 The number in the second address may be prefixed with a
224 to specify the number of lines to match after the first pattern.
225 In the case when the second address is a context
228 does not re-match the second address against the
229 pattern space that matched the first address.
231 first line following the selected range,
233 starts looking again for the first address.
235 Editing commands can be applied to non-selected pattern spaces by use
236 of the exclamation character
239 .Ss "Sed Regular Expressions"
240 The regular expressions used in
242 by default, are basic regular expressions (BREs, see
244 for more information), but extended (modern) regular expressions can be used
250 has the following two additions to regular expressions:
254 In a context address, any character other than a backslash
256 or newline character may be used to delimit the regular expression.
257 The opening delimiter needs to be preceded by a backslash
258 unless it is a slash.
259 For example, the context address
263 Also, putting a backslash character before the delimiting character
264 within the regular expression causes the character to be treated literally.
265 For example, in the context address
267 the RE delimiter is an
271 stands for itself, so that the regular expression is
275 The escape sequence \en matches a newline character embedded in the
277 You cannot, however, use a literal newline character in an address or
278 in the substitute command.
281 One special feature of
283 regular expressions is that they can default to the last regular
285 If a regular expression is empty, i.e., just the delimiter characters
286 are specified, the last regular expression encountered is used instead.
287 The last regular expression is defined as the last regular expression
288 used as part of an address or substitute command, and at run-time, not
290 For example, the command
297 In the following list of commands, the maximum number of permissible
298 addresses for each command is indicated by [0addr], [1addr], or [2addr],
299 representing zero, one, or two addresses.
303 consists of one or more lines.
304 To embed a newline in the text, precede it with a backslash.
305 Other backslashes in text are deleted and the following character
312 functions take an optional file parameter, which should be separated
313 from the function letter by white space.
314 Each file given as an argument to
316 is created (or its contents truncated) before any input processing begins.
328 functions all accept additional arguments.
329 The following synopses indicate which arguments have to be separated from
330 the function letters by white space characters.
332 Two of the functions take a function-list.
335 functions separated by newlines, as follows:
336 .Bd -literal -offset indent
346 can be preceded by white space and can be followed by white space.
347 The function can be preceded by white space.
350 must be preceded by a newline, and may also be preceded by white space.
352 .Bl -tag -width "XXXXXX" -compact
353 .It [2addr] function-list
354 Execute function-list only when the pattern space is selected.
360 to standard output immediately before each attempt to read a line of input,
361 whether by executing the
363 function or by beginning a new cycle.
368 function with the specified label.
369 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.
379 Delete the pattern space and start the next cycle.
382 Delete the initial segment of the pattern space through the first
383 newline character and start the next cycle.
386 Replace the contents of the pattern space with the contents of the
390 Append a newline character followed by the contents of the hold space
391 to the pattern space.
394 Replace the contents of the hold space with the contents of the
398 Append a newline character followed by the contents of the pattern space
405 to the standard output.
409 Write the pattern space to the standard output in a visually unambiguous
411 This form is as follows:
413 .Bl -tag -width "carriage-returnXX" -offset indent -compact
428 Nonprintable characters are written as three-digit octal numbers (with a
429 preceding backslash) for each byte in the character (most significant byte
431 Long lines are folded, with the point of folding indicated by displaying
432 a backslash followed by a newline.
433 The end of each line is marked with a
437 Write the pattern space to the standard output if the default output has
438 not been suppressed, and replace the pattern space with the next line of
442 Append the next line of input to the pattern space, using an embedded
443 newline character to separate the appended material from the original
445 Note that the current line number changes.
448 Write the pattern space to standard output.
451 Write the pattern space, up to the first newline character to the
455 Branch to the end of the script and quit without starting a new cycle.
460 to the standard output immediately before the next attempt to read a
464 cannot be read for any reason, it is silently ignored and no error
467 .It [2addr]s/regular expression/replacement/flags
468 Substitute the replacement string for the first instance of the regular
469 expression in the pattern space.
470 Any character other than backslash or newline can be used instead of
471 a slash to delimit the RE and the replacement.
472 Within the RE and the replacement, the RE delimiter itself can be used as
473 a literal character if it is preceded by a backslash.
477 appearing in the replacement is replaced by the string matching the RE.
478 The special meaning of
480 in this context can be suppressed by preceding it by a backslash.
485 is a digit, is replaced by the text matched
486 by the corresponding backreference expression (see
489 A line can be split by substituting a newline character into it.
490 To specify a newline character in the replacement string, precede it with
495 in the substitute function is zero or more of the following:
496 .Bl -tag -width "XXXXXX" -offset indent
498 Make the substitution only for the
500 occurrence of the regular expression in the pattern space.
502 Make the substitution for all non-overlapping matches of the
503 regular expression, not just the first one.
505 Write the pattern space to standard output if a replacement was made.
506 If the replacement string is identical to that which it replaces, it
507 is still considered to have been a replacement.
509 Append the pattern space to
511 if a replacement was made.
512 If the replacement string is identical to that which it replaces, it
513 is still considered to have been a replacement.
515 Match the regular expression in a case-insensitive way.
521 function bearing the label if any substitutions have been made since the
522 most recent reading of an input line or execution of a
525 If no label is specified, branch to the end of the script.
528 Append the pattern space to the
532 Swap the contents of the pattern and hold spaces.
534 .It [2addr]y/string1/string2/
535 Replace all occurrences of characters in
537 in the pattern space with the corresponding characters from
539 Any character other than a backslash or newline can be used instead of
540 a slash to delimit the strings.
545 a backslash followed by any character other than a newline is that literal
546 character, and a backslash followed by an ``n'' is replaced by a newline
550 .It [2addr]!function-list
551 Apply the function or function-list only to the lines that are
553 selected by the address(es).
556 This function does nothing; it bears a label to which the
563 Write the line number to the standard output followed by a newline
567 Empty lines are ignored.
572 and the remainder of the line are ignored (treated as a comment), with
573 the single exception that if the first two characters in the file are
575 the default output is suppressed.
576 This is the same as specifying the
578 option on the command line.
582 .Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
585 environment variables affect the execution of
600 utility is expected to be a superset of the
608 options, the prefixing
610 in the second member of an address range,
613 flag to the address regular expression and substitution command are
616 extensions and may not be available on other operating systems.
625 .An "Diomidis D. Spinellis" Aq dds@FreeBSD.org
627 Multibyte characters containing a byte with value 0x5C
630 may be incorrectly treated as line continuation characters in arguments to the
636 Multibyte characters cannot be used as delimiters with the