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