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