Annotation of src/usr.bin/sed/sed.1, Revision 1.57
1.57 ! martijn 1: .\" $OpenBSD: sed.1,v 1.56 2018/07/11 06:47:38 martijn Exp $
1.12 aaron 2: .\"
1.1 deraadt 3: .\" Copyright (c) 1992, 1993
4: .\" The Regents of the University of California. All rights reserved.
5: .\"
6: .\" This code is derived from software contributed to Berkeley by
7: .\" the Institute of Electrical and Electronics Engineers, Inc.
8: .\"
9: .\" Redistribution and use in source and binary forms, with or without
10: .\" modification, are permitted provided that the following conditions
11: .\" are met:
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.
1.17 millert 17: .\" 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 18: .\" may be used to endorse or promote products derived from this software
19: .\" without specific prior written permission.
20: .\"
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
31: .\" SUCH DAMAGE.
32: .\"
33: .\" from: @(#)sed.1 8.2 (Berkeley) 12/30/93
34: .\"
1.57 ! martijn 35: .Dd $Mdocdate: July 11 2018 $
1.1 deraadt 36: .Dt SED 1
37: .Os
38: .Sh NAME
39: .Nm sed
40: .Nd stream editor
41: .Sh SYNOPSIS
42: .Nm sed
1.34 jmc 43: .Op Fl aEnru
1.46 jmc 44: .Op Fl i Ns Op Ar extension
1.1 deraadt 45: .Ar command
1.29 sobrado 46: .Op Ar
1.1 deraadt 47: .Nm sed
1.33 djm 48: .Op Fl aEnru
1.1 deraadt 49: .Op Fl e Ar command
50: .Op Fl f Ar command_file
1.45 jasper 51: .Op Fl i Ns Op Ar extension
1.29 sobrado 52: .Op Ar
1.1 deraadt 53: .Sh DESCRIPTION
54: The
1.8 aaron 55: .Nm
1.1 deraadt 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.
59: .Pp
60: A single command may be specified as the first argument to
61: .Nm sed .
1.25 jmc 62: Multiple commands may be specified
63: separated by newlines or semicolons,
64: or by using the
1.1 deraadt 65: .Fl e
66: or
67: .Fl f
68: options.
69: All commands are applied to the input in the order they are specified
70: regardless of their origin.
71: .Pp
1.10 aaron 72: The options are as follows:
1.16 aaron 73: .Bl -tag -width Ds
1.1 deraadt 74: .It Fl a
75: The files listed as parameters for the
1.41 jmc 76: .Ic w
77: function or flag are created (or truncated) before any processing begins,
1.1 deraadt 78: by default.
79: The
80: .Fl a
81: option causes
1.8 aaron 82: .Nm
1.1 deraadt 83: to delay opening each file until a command containing the related
1.41 jmc 84: .Ic w
85: function or flag is applied to a line of input.
1.34 jmc 86: .It Fl E
87: Interpret regular expressions using POSIX extended regular expression syntax.
88: The default behaviour is to use POSIX basic regular expression syntax.
1.1 deraadt 89: .It Fl e Ar command
90: Append the editing commands specified by the
91: .Ar command
92: argument
93: to the list of commands.
94: .It Fl f Ar command_file
95: Append the editing commands found in the file
96: .Ar command_file
97: to the list of commands.
98: The editing commands should each be listed on a separate line.
1.46 jmc 99: .It Fl i Ns Op Ar extension
100: Edit files in place, saving backups with the specified
1.45 jasper 101: .Ar extension .
1.46 jmc 102: If a zero length
1.45 jasper 103: .Ar extension
104: is given, no backup will be saved.
1.46 jmc 105: It is not recommended to give a zero length
1.45 jasper 106: .Ar extension
1.46 jmc 107: when in place editing files, as it risks corruption or partial content
1.45 jasper 108: in situations where disk space is exhausted, etc.
1.57 ! martijn 109: In
! 110: .Fl i
! 111: mode, the hold space, line numbers, and ranges are reset between files.
1.33 djm 112: .It Fl r
1.34 jmc 113: An alias for
114: .Fl E ,
115: for compatibility with GNU sed.
1.1 deraadt 116: .It Fl n
117: By default, each line of input is echoed to the standard output after
118: all of the commands have been applied to it.
119: The
120: .Fl n
121: option suppresses this behavior.
1.26 ray 122: .It Fl u
123: Force output to be line buffered,
124: printing each line as it becomes available.
125: By default, output is line buffered when standard output is a terminal
126: and block buffered otherwise.
127: See
1.47 tedu 128: .Xr setvbuf 3
1.26 ray 129: for a more detailed explanation.
1.1 deraadt 130: .El
131: .Pp
132: The form of a
1.8 aaron 133: .Nm
1.1 deraadt 134: command is as follows:
1.21 jmc 135: .Pp
1.1 deraadt 136: .Dl [address[,address]]function[arguments]
1.21 jmc 137: .Pp
1.1 deraadt 138: Whitespace may be inserted before the first address and the function
139: portions of the command.
140: .Pp
141: Normally,
1.8 aaron 142: .Nm
1.1 deraadt 143: cyclically copies a line of input, not including its terminating newline
144: character, into a
1.21 jmc 145: .Em pattern space ,
1.1 deraadt 146: (unless there is something left after a
1.37 jmc 147: .Ic D
1.1 deraadt 148: function),
149: applies all of the commands with addresses that select that pattern space,
150: copies the pattern space to the standard output, appending a newline, and
151: deletes the pattern space.
152: .Pp
153: Some of the functions use a
1.21 jmc 154: .Em hold space
1.1 deraadt 155: to save all or part of the pattern space for subsequent retrieval.
1.21 jmc 156: .Sh SED ADDRESSES
1.1 deraadt 157: An address is not required, but if specified must be a number (that counts
158: input lines
1.21 jmc 159: cumulatively across input files), a dollar character
1.8 aaron 160: .Pq Ql $
1.55 schwarze 161: that addresses the last line of input, or a context address,
162: which is a regular expression preceded and followed by a delimiter.
1.56 martijn 163: The delimiter can be any character except a newline or a backslash.
1.55 schwarze 164: Unless it is a slash, the opening delimiter needs to be escaped with
165: a backslash.
1.1 deraadt 166: .Pp
167: A command line with no addresses selects every pattern space.
168: .Pp
169: A command line with one address selects all of the pattern spaces
170: that match the address.
171: .Pp
172: A command line with two addresses selects the inclusive range from
173: the first pattern space that matches the first address through the next
174: pattern space that matches the second.
175: (If the second address is a number less than or equal to the line number
176: first selected, only that line is selected.)
177: Starting at the first line following the selected range,
1.8 aaron 178: .Nm
1.1 deraadt 179: starts looking again for the first address.
180: .Pp
181: Editing commands can be applied to non-selected pattern spaces by use
182: of the exclamation character
1.18 jmc 183: .Pq Ql \&!
1.1 deraadt 184: function.
1.21 jmc 185: .Sh SED REGULAR EXPRESSIONS
1.34 jmc 186: By default,
1.8 aaron 187: .Nm
1.23 jmc 188: regular expressions are basic regular expressions
189: .Pq BREs .
1.34 jmc 190: Extended regular expressions are supported using the
191: .Fl E
192: and
193: .Fl r
194: options.
1.23 jmc 195: See
1.19 jmc 196: .Xr re_format 7
1.23 jmc 197: for more information on regular expressions.
1.1 deraadt 198: In addition,
1.8 aaron 199: .Nm
1.23 jmc 200: has the following two additions to BREs:
1.21 jmc 201: .Pp
1.1 deraadt 202: .Bl -enum -compact
203: .It
1.55 schwarze 204: The character delimiting the regular expression
205: can be used inside the regular expression by prepending a backslash
206: or by including it in a character class.
207: For example, in the context address \ex\ex[xy]x, the RE delimiter
1.1 deraadt 208: is an
1.8 aaron 209: .Sq x
1.55 schwarze 210: and the other
1.8 aaron 211: .Sq x
1.55 schwarze 212: characters stand for themselves, so that the regular expression is
213: .Dq x[xy] .
1.21 jmc 214: .Pp
1.1 deraadt 215: .It
216: The escape sequence \en matches a newline character embedded in the
217: pattern space.
218: You can't, however, use a literal newline character in an address or
219: in the substitute command.
220: .El
221: .Pp
222: One special feature of
1.8 aaron 223: .Nm
1.1 deraadt 224: regular expressions is that they can default to the last regular
225: expression used.
1.13 aaron 226: If a regular expression is empty, i.e., just the delimiter characters
1.1 deraadt 227: are specified, the last regular expression encountered is used instead.
228: The last regular expression is defined as the last regular expression
229: used as part of an address or substitute command, and at run-time, not
230: compile-time.
231: For example, the command
232: .Dq /abc/s//XXX/
233: will substitute
234: .Dq XXX
235: for the pattern
236: .Dq abc .
1.21 jmc 237: .Sh SED FUNCTIONS
1.1 deraadt 238: In the following list of commands, the maximum number of permissible
239: addresses for each command is indicated by [0addr], [1addr], or [2addr],
240: representing zero, one, or two addresses.
241: .Pp
242: The argument
1.37 jmc 243: .Ar text
1.1 deraadt 244: consists of one or more lines.
245: To embed a newline in the text, precede it with a backslash.
246: Other backslashes in text are deleted and the following character
247: taken literally.
248: .Pp
249: The
1.40 jmc 250: .Ic r
1.1 deraadt 251: and
1.40 jmc 252: .Ic w
253: functions,
254: as well as the
255: .Cm w
256: flag to the
257: .Ic s
258: function,
1.52 martijn 259: take a
1.40 jmc 260: .Ar file
261: parameter,
262: which should be separated from the function or flag by whitespace.
263: Files are created
264: (or their contents truncated)
265: before any input processing begins.
1.1 deraadt 266: .Pp
267: The
1.40 jmc 268: .Ic b ,
269: .Ic r ,
270: .Ic s ,
271: .Ic t ,
272: .Ic w ,
273: .Ic y ,
1.1 deraadt 274: and
1.40 jmc 275: .Ic \&:
1.1 deraadt 276: functions all accept additional arguments.
1.40 jmc 277: The synopses below indicate which arguments have to be separated from
1.9 aaron 278: the function letters by whitespace characters.
1.1 deraadt 279: .Pp
1.41 jmc 280: Functions can be combined to form a
281: .Em function list ,
282: a list of
1.8 aaron 283: .Nm
1.44 schwarze 284: functions each followed by a newline, as follows:
1.1 deraadt 285: .Bd -literal -offset indent
286: { function
287: function
288: ...
289: function
290: }
291: .Ed
292: .Pp
1.44 schwarze 293: The braces can be preceded and followed by whitespace.
294: The functions can be preceded by whitespace as well.
1.38 jmc 295: .Pp
1.40 jmc 296: Functions and function lists may be preceded by an exclamation mark,
1.38 jmc 297: in which case they are applied only to lines that are
298: .Em not
299: selected by the addresses.
1.37 jmc 300: .Bl -tag -width Ds
1.51 martijn 301: .It [2addr] Ns Ar function-list
1.15 aaron 302: Execute
1.37 jmc 303: .Ar function-list
1.15 aaron 304: only when the pattern space is selected.
1.51 martijn 305: .It Xo [1addr] Ns Ic a Ns \e
1.37 jmc 306: .br
307: .Ar text
308: .Xc
1.1 deraadt 309: Write
1.37 jmc 310: .Ar text
1.1 deraadt 311: to standard output immediately before each attempt to read a line of input,
312: whether by executing the
1.37 jmc 313: .Ic N
1.1 deraadt 314: function or by beginning a new cycle.
1.37 jmc 315: .It [2addr] Ns Ic b Bq Ar label
1.1 deraadt 316: Branch to the
1.37 jmc 317: .Ic \&:
318: function with the specified
319: .Ar label .
1.1 deraadt 320: If the label is not specified, branch to the end of the script.
1.51 martijn 321: .It Xo [2addr] Ns Ic c Ns \e
1.37 jmc 322: .br
323: .Ar text
324: .Xc
1.1 deraadt 325: Delete the pattern space.
326: With 0 or 1 address or at the end of a 2-address range,
1.37 jmc 327: .Ar text
1.1 deraadt 328: is written to the standard output.
1.37 jmc 329: .It [2addr] Ns Ic d
1.1 deraadt 330: Delete the pattern space and start the next cycle.
1.37 jmc 331: .It [2addr] Ns Ic D
1.1 deraadt 332: Delete the initial segment of the pattern space through the first
333: newline character and start the next cycle.
1.37 jmc 334: .It [2addr] Ns Ic g
1.1 deraadt 335: Replace the contents of the pattern space with the contents of the
336: hold space.
1.37 jmc 337: .It [2addr] Ns Ic G
1.1 deraadt 338: Append a newline character followed by the contents of the hold space
339: to the pattern space.
1.37 jmc 340: .It [2addr] Ns Ic h
1.1 deraadt 341: Replace the contents of the hold space with the contents of the
342: pattern space.
1.37 jmc 343: .It [2addr] Ns Ic H
1.1 deraadt 344: Append a newline character followed by the contents of the pattern space
345: to the hold space.
1.51 martijn 346: .It Xo [1addr] Ns Ic i Ns \e
1.37 jmc 347: .br
348: .Ar text
349: .Xc
1.1 deraadt 350: Write
1.37 jmc 351: .Ar text
1.1 deraadt 352: to the standard output.
1.37 jmc 353: .It [2addr] Ns Ic l
1.1 deraadt 354: (The letter ell.)
355: Write the pattern space to the standard output in a visually unambiguous
356: form.
357: This form is as follows:
1.21 jmc 358: .Pp
1.1 deraadt 359: .Bl -tag -width "carriage-returnXX" -offset indent -compact
360: .It backslash
1.3 deraadt 361: \e\e
1.1 deraadt 362: .It alert
363: \ea
1.31 millert 364: .It backspace
365: \eb
1.1 deraadt 366: .It form-feed
367: \ef
368: .It carriage-return
369: \er
370: .It tab
371: \et
372: .It vertical tab
373: \ev
374: .El
375: .Pp
1.15 aaron 376: Non-printable characters are written as three-digit octal numbers (with a
1.1 deraadt 377: preceding backslash) for each byte in the character (most significant byte
378: first).
379: Long lines are folded, with the point of folding indicated by displaying
380: a backslash followed by a newline.
381: The end of each line is marked with a
1.8 aaron 382: .Ql $ .
1.37 jmc 383: .It [2addr] Ns Ic n
1.1 deraadt 384: Write the pattern space to the standard output if the default output has
385: not been suppressed, and replace the pattern space with the next line of
386: input.
1.37 jmc 387: .It [2addr] Ns Ic N
1.1 deraadt 388: Append the next line of input to the pattern space, using an embedded
389: newline character to separate the appended material from the original
390: contents.
391: Note that the current line number changes.
1.37 jmc 392: .It [2addr] Ns Ic p
1.1 deraadt 393: Write the pattern space to standard output.
1.37 jmc 394: .It [2addr] Ns Ic P
1.39 jmc 395: Write the pattern space, up to the first newline character,
396: to the standard output.
1.37 jmc 397: .It [1addr] Ns Ic q
1.57 ! martijn 398: Branch to the end of the script and quit without starting a new cycle or file.
1.37 jmc 399: .It [1addr] Ns Ic r Ar file
1.1 deraadt 400: Copy the contents of
1.37 jmc 401: .Ar file
1.1 deraadt 402: to the standard output immediately before the next attempt to read a
403: line of input.
404: If
1.37 jmc 405: .Ar file
1.1 deraadt 406: cannot be read for any reason, it is silently ignored and no error
407: condition is set.
1.37 jmc 408: .It [2addr] Ns Ic s Ns / Ns Ar RE Ns / Ns Ar replacement Ns / Ns Ar flags
409: Substitute the
410: .Ar replacement
411: string for the first instance of the regular expression
412: .Ar RE
413: in the pattern space.
1.1 deraadt 414: Any character other than backslash or newline can be used instead of
1.37 jmc 415: a slash to delimit the regular expression and the replacement.
1.55 schwarze 416: Also see the the section about
417: .Sx SED REGULAR EXPRESSIONS .
1.1 deraadt 418: .Pp
419: An ampersand
1.8 aaron 420: .Pq Ql &
1.37 jmc 421: appearing in the replacement is replaced by the string matching the
422: regular expression.
1.1 deraadt 423: The string
1.8 aaron 424: .Ql \e# ,
1.1 deraadt 425: where
1.8 aaron 426: .Ql #
1.1 deraadt 427: is a digit, is replaced by the text matched
428: by the corresponding backreference expression (see
1.14 aaron 429: .Xr re_format 7 ) .
1.1 deraadt 430: .Pp
1.56 martijn 431: All other instances of a backslash will print the literal character
432: following it.
433: Using a backslash before any other character other than
434: .Ql & ,
435: .Ql \e ,
436: digit, newline
437: .Pq ascii 0x0a ,
438: and the delimiter is unspecified and might not be portable to other
439: implementations of
440: .Nm .
1.1 deraadt 441: .Pp
442: The value of
1.37 jmc 443: .Ar flags
1.1 deraadt 444: in the substitute function is zero or more of the following:
445: .Bl -tag -width "XXXXXX" -offset indent
1.49 schwarze 446: .It Ar N
447: Make the substitution only for the
448: .Ar N Ap th
449: occurrence of the regular expression in the pattern space, where
450: .Ar N
451: is a positive integer starting with
452: .Cm 1 No ... Cm 9 .
1.37 jmc 453: .It Cm g
1.1 deraadt 454: Make the substitution for all non-overlapping matches of the
455: regular expression, not just the first one.
1.37 jmc 456: .It Cm p
1.1 deraadt 457: Write the pattern space to standard output if a replacement was made.
458: If the replacement string is identical to that which it replaces, it
459: is still considered to have been a replacement.
1.37 jmc 460: .It Cm w Ar file
1.1 deraadt 461: Append the pattern space to
1.37 jmc 462: .Ar file
1.1 deraadt 463: if a replacement was made.
464: If the replacement string is identical to that which it replaces, it
465: is still considered to have been a replacement.
466: .El
1.37 jmc 467: .It [2addr] Ns Ic t Bq Ar label
1.1 deraadt 468: Branch to the
1.37 jmc 469: .Ic \&:
470: function bearing the
471: .Ar label
472: if any substitutions have been made since the
1.1 deraadt 473: most recent reading of an input line or execution of a
1.37 jmc 474: .Ic t
1.1 deraadt 475: function.
476: If no label is specified, branch to the end of the script.
1.37 jmc 477: .It [2addr] Ns Ic w Ar file
1.1 deraadt 478: Append the pattern space to the
1.37 jmc 479: .Ar file .
480: .It [2addr] Ns Ic x
1.1 deraadt 481: Swap the contents of the pattern and hold spaces.
1.37 jmc 482: .It [2addr] Ns Ic y Ns / Ns Ar string1 Ns / Ns Ar string2 Ns /
1.1 deraadt 483: Replace all occurrences of characters in
1.37 jmc 484: .Ar string1
1.1 deraadt 485: in the pattern space with the corresponding characters from
1.37 jmc 486: .Ar string2 .
1.1 deraadt 487: Any character other than a backslash or newline can be used instead of
488: a slash to delimit the strings.
1.53 martijn 489: .Pp
1.1 deraadt 490: Within
1.37 jmc 491: .Ar string1
1.1 deraadt 492: and
1.37 jmc 493: .Ar string2 ,
1.53 martijn 494: a backslash followed by another backslash
495: is replaced by a single backslash,
496: a backslash followed by an
497: .Sq n
498: is replaced by a newline character,
499: and a backslash followed by the delimiting character
500: is replaced by that character,
501: causing it to be treated literally,
502: with the exception of the
1.8 aaron 503: .Sq n
1.53 martijn 504: character,
505: which will still be treated like a newline character.
506: It is an error for a backslash to not be followed by another backslash,
507: .Sq n ,
508: or the delimiting character,
509: or for
510: .Ar string1
511: to contain repeating characters.
1.37 jmc 512: .It [0addr] Ns Ic \&: Ns Ar label
513: This function does nothing; it bears a
514: .Ar label
515: to which the
516: .Ic b
1.1 deraadt 517: and
1.37 jmc 518: .Ic t
1.1 deraadt 519: commands may branch.
1.37 jmc 520: .It [1addr] Ns Ic =
1.15 aaron 521: Write the line number to the standard output followed by a newline character.
1.1 deraadt 522: .It [0addr]
523: Empty lines are ignored.
1.37 jmc 524: .It [0addr] Ns Ic #
1.1 deraadt 525: The
1.8 aaron 526: .Ql #
1.1 deraadt 527: and the remainder of the line are ignored (treated as a comment), with
528: the single exception that if the first two characters in the file are
1.8 aaron 529: .Ql #n ,
1.1 deraadt 530: the default output is suppressed.
531: This is the same as specifying the
532: .Fl n
533: option on the command line.
1.48 bentley 534: .El
535: .Sh ENVIRONMENT
536: .Bl -tag -width COLUMNS
537: .It Ev COLUMNS
538: If set to a positive integer,
539: output from the
540: .Ic l
541: function is formatted to the given width in columns.
542: Otherwise,
543: .Nm
1.51 martijn 544: defaults to the terminal width, or 80 columns if the output is not a terminal.
1.1 deraadt 545: .El
1.36 jmc 546: .Sh EXIT STATUS
1.24 jmc 547: .Ex -std sed
1.1 deraadt 548: .Sh SEE ALSO
549: .Xr awk 1 ,
550: .Xr ed 1 ,
551: .Xr grep 1 ,
552: .Xr re_format 7
553: .Sh STANDARDS
554: The
1.8 aaron 555: .Nm
1.25 jmc 556: utility is compliant with the
1.32 jmc 557: .St -p1003.1-2008
1.1 deraadt 558: specification.
1.25 jmc 559: .Pp
1.26 ray 560: The flags
1.45 jasper 561: .Op Fl aEiru
1.27 jmc 562: are extensions to that specification.
1.25 jmc 563: .Pp
564: The use of newlines to separate multiple commands on the command line
565: is non-portable;
566: the use of newlines to separate multiple commands within a command file
567: .Pq Fl f Ar command_file
568: is portable.
1.11 aaron 569: .Sh HISTORY
570: A
571: .Nm
572: command appeared in
573: .At v7 .
1.25 jmc 574: .Sh CAVEATS
575: The use of semicolons to separate multiple commands
576: is not permitted for the following commands:
1.37 jmc 577: .Ic a , b , c ,
578: .Ic i , r , t ,
579: .Ic w , \&: ,
1.25 jmc 580: and
1.37 jmc 581: .Ic # .