Annotation of src/usr.bin/sed/sed.1, Revision 1.28
1.28 ! jmc 1: .\" $OpenBSD: sed.1,v 1.27 2007/05/30 04:41:34 jmc 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.28 ! jmc 35: .Dd $Mdocdate$
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.26 ray 43: .Op Fl anu
1.1 deraadt 44: .Ar command
45: .Op Ar file ...
46: .Nm sed
1.26 ray 47: .Op Fl anu
1.1 deraadt 48: .Op Fl e Ar command
49: .Op Fl f Ar command_file
50: .Op Ar file ...
51: .Sh DESCRIPTION
52: The
1.8 aaron 53: .Nm
1.1 deraadt 54: utility reads the specified files, or the standard input if no files
55: are specified, modifying the input as specified by a list of commands.
56: The input is then written to the standard output.
57: .Pp
58: A single command may be specified as the first argument to
59: .Nm sed .
1.25 jmc 60: Multiple commands may be specified
61: separated by newlines or semicolons,
62: or by using the
1.1 deraadt 63: .Fl e
64: or
65: .Fl f
66: options.
67: All commands are applied to the input in the order they are specified
68: regardless of their origin.
69: .Pp
1.10 aaron 70: The options are as follows:
1.16 aaron 71: .Bl -tag -width Ds
1.1 deraadt 72: .It Fl a
73: The files listed as parameters for the
1.8 aaron 74: .Ql w
1.1 deraadt 75: functions are created (or truncated) before any processing begins,
76: by default.
77: The
78: .Fl a
79: option causes
1.8 aaron 80: .Nm
1.1 deraadt 81: to delay opening each file until a command containing the related
1.8 aaron 82: .Ql w
1.1 deraadt 83: function is applied to a line of input.
84: .It Fl e Ar command
85: Append the editing commands specified by the
86: .Ar command
87: argument
88: to the list of commands.
89: .It Fl f Ar command_file
90: Append the editing commands found in the file
91: .Ar command_file
92: to the list of commands.
93: The editing commands should each be listed on a separate line.
94: .It Fl n
95: By default, each line of input is echoed to the standard output after
96: all of the commands have been applied to it.
97: The
98: .Fl n
99: option suppresses this behavior.
1.26 ray 100: .It Fl u
101: Force output to be line buffered,
102: printing each line as it becomes available.
103: By default, output is line buffered when standard output is a terminal
104: and block buffered otherwise.
105: See
106: .Xr setbuf 3
107: for a more detailed explanation.
1.1 deraadt 108: .El
109: .Pp
110: The form of a
1.8 aaron 111: .Nm
1.1 deraadt 112: command is as follows:
1.21 jmc 113: .Pp
1.1 deraadt 114: .Dl [address[,address]]function[arguments]
1.21 jmc 115: .Pp
1.1 deraadt 116: Whitespace may be inserted before the first address and the function
117: portions of the command.
118: .Pp
119: Normally,
1.8 aaron 120: .Nm
1.1 deraadt 121: cyclically copies a line of input, not including its terminating newline
122: character, into a
1.21 jmc 123: .Em pattern space ,
1.1 deraadt 124: (unless there is something left after a
1.8 aaron 125: .Sq D
1.1 deraadt 126: function),
127: applies all of the commands with addresses that select that pattern space,
128: copies the pattern space to the standard output, appending a newline, and
129: deletes the pattern space.
130: .Pp
131: Some of the functions use a
1.21 jmc 132: .Em hold space
1.1 deraadt 133: to save all or part of the pattern space for subsequent retrieval.
1.21 jmc 134: .Sh SED ADDRESSES
1.1 deraadt 135: An address is not required, but if specified must be a number (that counts
136: input lines
1.21 jmc 137: cumulatively across input files), a dollar character
1.8 aaron 138: .Pq Ql $
1.21 jmc 139: that addresses the last line of input, or a context address
1.1 deraadt 140: (which consists of a regular expression preceded and followed by a
141: delimiter).
142: .Pp
143: A command line with no addresses selects every pattern space.
144: .Pp
145: A command line with one address selects all of the pattern spaces
146: that match the address.
147: .Pp
148: A command line with two addresses selects the inclusive range from
149: the first pattern space that matches the first address through the next
150: pattern space that matches the second.
151: (If the second address is a number less than or equal to the line number
152: first selected, only that line is selected.)
153: Starting at the first line following the selected range,
1.8 aaron 154: .Nm
1.1 deraadt 155: starts looking again for the first address.
156: .Pp
157: Editing commands can be applied to non-selected pattern spaces by use
158: of the exclamation character
1.18 jmc 159: .Pq Ql \&!
1.1 deraadt 160: function.
1.21 jmc 161: .Sh SED REGULAR EXPRESSIONS
1.1 deraadt 162: The
1.8 aaron 163: .Nm
1.23 jmc 164: regular expressions are basic regular expressions
165: .Pq BREs .
166: See
1.19 jmc 167: .Xr re_format 7
1.23 jmc 168: for more information on regular expressions.
1.1 deraadt 169: In addition,
1.8 aaron 170: .Nm
1.23 jmc 171: has the following two additions to BREs:
1.21 jmc 172: .Pp
1.1 deraadt 173: .Bl -enum -compact
174: .It
175: In a context address, any character other than a backslash
1.8 aaron 176: .Pq Ql \e
1.1 deraadt 177: or newline character may be used to delimit the regular expression.
178: Also, putting a backslash character before the delimiting character
179: causes the character to be treated literally.
180: For example, in the context address \exabc\exdefx, the RE delimiter
181: is an
1.8 aaron 182: .Sq x
1.1 deraadt 183: and the second
1.8 aaron 184: .Sq x
1.1 deraadt 185: stands for itself, so that the regular expression is
186: .Dq abcxdef .
1.21 jmc 187: .Pp
1.1 deraadt 188: .It
189: The escape sequence \en matches a newline character embedded in the
190: pattern space.
191: You can't, however, use a literal newline character in an address or
192: in the substitute command.
193: .El
194: .Pp
195: One special feature of
1.8 aaron 196: .Nm
1.1 deraadt 197: regular expressions is that they can default to the last regular
198: expression used.
1.13 aaron 199: If a regular expression is empty, i.e., just the delimiter characters
1.1 deraadt 200: are specified, the last regular expression encountered is used instead.
201: The last regular expression is defined as the last regular expression
202: used as part of an address or substitute command, and at run-time, not
203: compile-time.
204: For example, the command
205: .Dq /abc/s//XXX/
206: will substitute
207: .Dq XXX
208: for the pattern
209: .Dq abc .
1.21 jmc 210: .Sh SED FUNCTIONS
1.1 deraadt 211: In the following list of commands, the maximum number of permissible
212: addresses for each command is indicated by [0addr], [1addr], or [2addr],
213: representing zero, one, or two addresses.
214: .Pp
215: The argument
216: .Em text
217: consists of one or more lines.
218: To embed a newline in the text, precede it with a backslash.
219: Other backslashes in text are deleted and the following character
220: taken literally.
221: .Pp
222: The
1.8 aaron 223: .Sq r
1.1 deraadt 224: and
1.8 aaron 225: .Sq w
1.1 deraadt 226: functions take an optional file parameter, which should be separated
1.9 aaron 227: from the function letter by whitespace.
1.1 deraadt 228: Each file given as an argument to
1.8 aaron 229: .Nm
1.1 deraadt 230: is created (or its contents truncated) before any input processing begins.
231: .Pp
232: The
1.8 aaron 233: .Sq b ,
234: .Sq r ,
235: .Sq s ,
236: .Sq t ,
237: .Sq w ,
238: .Sq y ,
1.18 jmc 239: .Ql \&! ,
1.1 deraadt 240: and
1.8 aaron 241: .Ql \&:
1.1 deraadt 242: functions all accept additional arguments.
243: The following synopses indicate which arguments have to be separated from
1.9 aaron 244: the function letters by whitespace characters.
1.1 deraadt 245: .Pp
246: Two of the functions take a function-list.
247: This is a list of
1.8 aaron 248: .Nm
1.1 deraadt 249: functions separated by newlines, as follows:
250: .Bd -literal -offset indent
251: { function
252: function
253: ...
254: function
255: }
256: .Ed
257: .Pp
258: The
1.8 aaron 259: .Ql {
1.9 aaron 260: can be preceded or followed by whitespace.
261: The function can be preceded by whitespace as well.
1.1 deraadt 262: The terminating
1.8 aaron 263: .Ql }
1.9 aaron 264: must be preceded by a newline or optional whitespace.
1.21 jmc 265: .Pp
266: .Bl -tag -width "XXXXXXXX" -compact
1.15 aaron 267: .It [2addr] Em function-list
268: Execute
269: .Em function-list
270: only when the pattern space is selected.
1.21 jmc 271: .Pp
1.20 jmc 272: .It [1addr] Ns Em a Ns \e
1.15 aaron 273: .It Em text
1.21 jmc 274: .Pp
1.1 deraadt 275: Write
276: .Em text
277: to standard output immediately before each attempt to read a line of input,
278: whether by executing the
1.8 aaron 279: .Sq N
1.1 deraadt 280: function or by beginning a new cycle.
1.21 jmc 281: .Pp
1.20 jmc 282: .It [2addr] Ns Em b Ns [label]
1.1 deraadt 283: Branch to the
1.8 aaron 284: .Sq \&:
1.1 deraadt 285: function with the specified label.
286: If the label is not specified, branch to the end of the script.
1.21 jmc 287: .Pp
1.20 jmc 288: .It [2addr] Ns Em c Ns \e
1.15 aaron 289: .It Em text
1.21 jmc 290: .Pp
1.1 deraadt 291: Delete the pattern space.
292: With 0 or 1 address or at the end of a 2-address range,
293: .Em text
294: is written to the standard output.
1.21 jmc 295: .Pp
1.15 aaron 296: .It [2addr] Ns Em d
1.1 deraadt 297: Delete the pattern space and start the next cycle.
1.21 jmc 298: .Pp
1.15 aaron 299: .It [2addr] Ns Em D
1.1 deraadt 300: Delete the initial segment of the pattern space through the first
301: newline character and start the next cycle.
1.21 jmc 302: .Pp
1.15 aaron 303: .It [2addr] Ns Em g
1.1 deraadt 304: Replace the contents of the pattern space with the contents of the
305: hold space.
1.21 jmc 306: .Pp
1.15 aaron 307: .It [2addr] Ns Em G
1.1 deraadt 308: Append a newline character followed by the contents of the hold space
309: to the pattern space.
1.21 jmc 310: .Pp
1.15 aaron 311: .It [2addr] Ns Em h
1.1 deraadt 312: Replace the contents of the hold space with the contents of the
313: pattern space.
1.21 jmc 314: .Pp
1.15 aaron 315: .It [2addr] Ns Em H
1.1 deraadt 316: Append a newline character followed by the contents of the pattern space
317: to the hold space.
1.21 jmc 318: .Pp
1.20 jmc 319: .It [1addr] Ns Em i Ns \e
1.15 aaron 320: .It Em text
1.21 jmc 321: .Pp
1.1 deraadt 322: Write
323: .Em text
324: to the standard output.
1.21 jmc 325: .Pp
1.15 aaron 326: .It [2addr] Ns Em l
1.1 deraadt 327: (The letter ell.)
328: Write the pattern space to the standard output in a visually unambiguous
329: form.
330: This form is as follows:
1.21 jmc 331: .Pp
1.1 deraadt 332: .Bl -tag -width "carriage-returnXX" -offset indent -compact
333: .It backslash
1.3 deraadt 334: \e\e
1.1 deraadt 335: .It alert
336: \ea
337: .It form-feed
338: \ef
339: .It newline
340: \en
341: .It carriage-return
342: \er
343: .It tab
344: \et
345: .It vertical tab
346: \ev
347: .El
348: .Pp
1.15 aaron 349: Non-printable characters are written as three-digit octal numbers (with a
1.1 deraadt 350: preceding backslash) for each byte in the character (most significant byte
351: first).
352: Long lines are folded, with the point of folding indicated by displaying
353: a backslash followed by a newline.
354: The end of each line is marked with a
1.8 aaron 355: .Ql $ .
1.21 jmc 356: .Pp
1.15 aaron 357: .It [2addr] Ns Em n
1.1 deraadt 358: Write the pattern space to the standard output if the default output has
359: not been suppressed, and replace the pattern space with the next line of
360: input.
1.21 jmc 361: .Pp
1.15 aaron 362: .It [2addr] Ns Em N
1.1 deraadt 363: Append the next line of input to the pattern space, using an embedded
364: newline character to separate the appended material from the original
365: contents.
366: Note that the current line number changes.
1.21 jmc 367: .Pp
1.15 aaron 368: .It [2addr] Ns Em p
1.1 deraadt 369: Write the pattern space to standard output.
1.21 jmc 370: .Pp
1.15 aaron 371: .It [2addr] Ns Em P
1.1 deraadt 372: Write the pattern space, up to the first newline character to the
373: standard output.
1.21 jmc 374: .Pp
1.15 aaron 375: .It [1addr] Ns Em q
1.1 deraadt 376: Branch to the end of the script and quit without starting a new cycle.
1.21 jmc 377: .Pp
1.15 aaron 378: .It [1addr] Ns Em r file
1.1 deraadt 379: Copy the contents of
380: .Em file
381: to the standard output immediately before the next attempt to read a
382: line of input.
383: If
384: .Em file
385: cannot be read for any reason, it is silently ignored and no error
386: condition is set.
1.21 jmc 387: .Pp
1.20 jmc 388: .It [2addr] Ns Em s Ns /re/replacement/flags
1.1 deraadt 389: Substitute the replacement string for the first instance of the regular
390: expression in the pattern space.
391: Any character other than backslash or newline can be used instead of
392: a slash to delimit the RE and the replacement.
393: Within the RE and the replacement, the RE delimiter itself can be used as
394: a literal character if it is preceded by a backslash.
395: .Pp
396: An ampersand
1.8 aaron 397: .Pq Ql &
1.1 deraadt 398: appearing in the replacement is replaced by the string matching the RE.
399: The special meaning of
1.8 aaron 400: .Ql &
1.1 deraadt 401: in this context can be suppressed by preceding it by a backslash.
402: The string
1.8 aaron 403: .Ql \e# ,
1.1 deraadt 404: where
1.8 aaron 405: .Ql #
1.1 deraadt 406: is a digit, is replaced by the text matched
407: by the corresponding backreference expression (see
1.14 aaron 408: .Xr re_format 7 ) .
1.1 deraadt 409: .Pp
410: A line can be split by substituting a newline character into it.
411: To specify a newline character in the replacement string, precede it with
412: a backslash.
413: .Pp
414: The value of
415: .Em flags
416: in the substitute function is zero or more of the following:
417: .Bl -tag -width "XXXXXX" -offset indent
1.21 jmc 418: .It 0 ... 9
1.1 deraadt 419: Make the substitution only for the N'th occurrence of the regular
420: expression in the pattern space.
421: .It g
422: Make the substitution for all non-overlapping matches of the
423: regular expression, not just the first one.
424: .It p
425: Write the pattern space to standard output if a replacement was made.
426: If the replacement string is identical to that which it replaces, it
427: is still considered to have been a replacement.
428: .It w Em file
429: Append the pattern space to
430: .Em file
431: if a replacement was made.
432: If the replacement string is identical to that which it replaces, it
433: is still considered to have been a replacement.
434: .El
1.21 jmc 435: .Pp
1.20 jmc 436: .It [2addr] Ns Em t Ns [label]
1.1 deraadt 437: Branch to the
1.8 aaron 438: .Ql \&:
1.1 deraadt 439: function bearing the label if any substitutions have been made since the
440: most recent reading of an input line or execution of a
1.8 aaron 441: .Sq t
1.1 deraadt 442: function.
443: If no label is specified, branch to the end of the script.
1.21 jmc 444: .Pp
1.15 aaron 445: .It [2addr] Ns Em w file
1.1 deraadt 446: Append the pattern space to the
447: .Em file .
1.21 jmc 448: .Pp
1.15 aaron 449: .It [2addr] Ns Em x
1.1 deraadt 450: Swap the contents of the pattern and hold spaces.
1.21 jmc 451: .Pp
1.20 jmc 452: .It [2addr] Ns Em y Ns /string1/string2/
1.1 deraadt 453: Replace all occurrences of characters in
454: .Em string1
455: in the pattern space with the corresponding characters from
456: .Em string2 .
457: Any character other than a backslash or newline can be used instead of
458: a slash to delimit the strings.
459: Within
460: .Em string1
461: and
462: .Em string2 ,
463: a backslash followed by any character other than a newline is that literal
1.8 aaron 464: character, and a backslash followed by an
465: .Sq n
466: is replaced by a newline character.
1.21 jmc 467: .Pp
1.15 aaron 468: .Sm off
1.21 jmc 469: .It Xo [2addr] Em !function No ,\ \&[2addr]
1.15 aaron 470: .Em !function-list
471: .Xc
472: .Sm on
1.1 deraadt 473: Apply the function or function-list only to the lines that are
474: .Em not
475: selected by the address(es).
1.21 jmc 476: .Pp
477: .It [0addr] Ns Em \&: Ns label
1.1 deraadt 478: This function does nothing; it bears a label to which the
1.8 aaron 479: .Sq b
1.1 deraadt 480: and
1.8 aaron 481: .Sq t
1.1 deraadt 482: commands may branch.
1.21 jmc 483: .Pp
1.15 aaron 484: .It [1addr] Ns Em =
485: Write the line number to the standard output followed by a newline character.
1.21 jmc 486: .Pp
1.1 deraadt 487: .It [0addr]
488: Empty lines are ignored.
1.21 jmc 489: .Pp
1.15 aaron 490: .It [0addr] Ns Em #
1.1 deraadt 491: The
1.8 aaron 492: .Ql #
1.1 deraadt 493: and the remainder of the line are ignored (treated as a comment), with
494: the single exception that if the first two characters in the file are
1.8 aaron 495: .Ql #n ,
1.1 deraadt 496: the default output is suppressed.
497: This is the same as specifying the
498: .Fl n
499: option on the command line.
500: .El
501: .Pp
1.24 jmc 502: .Ex -std sed
1.1 deraadt 503: .Sh SEE ALSO
504: .Xr awk 1 ,
505: .Xr ed 1 ,
506: .Xr grep 1 ,
507: .Xr regex 3 ,
1.26 ray 508: .Xr setbuf 3 ,
1.1 deraadt 509: .Xr re_format 7
1.21 jmc 510: .Pp
1.22 jmc 511: "SED \(em A Non-interactive Text Editor",
512: .Pa /usr/share/doc/usd/15.sed/ .
1.1 deraadt 513: .Sh STANDARDS
514: The
1.8 aaron 515: .Nm
1.25 jmc 516: utility is compliant with the
517: .St -p1003.1-2004
1.1 deraadt 518: specification.
1.25 jmc 519: .Pp
1.26 ray 520: The flags
521: .Op Fl au
1.27 jmc 522: are extensions to that specification.
1.25 jmc 523: .Pp
524: The use of newlines to separate multiple commands on the command line
525: is non-portable;
526: the use of newlines to separate multiple commands within a command file
527: .Pq Fl f Ar command_file
528: is portable.
1.11 aaron 529: .Sh HISTORY
530: A
531: .Nm
532: command appeared in
533: .At v7 .
1.25 jmc 534: .Sh CAVEATS
535: The use of semicolons to separate multiple commands
536: is not permitted for the following commands:
537: .Cm a , b , c ,
538: .Cm i , r , t ,
539: .Cm w , \&: ,
540: and
541: .Cm # .