Annotation of src/usr.bin/sed/sed.1, Revision 1.14
1.14 ! aaron 1: .\" $OpenBSD: sed.1,v 1.13 2000/03/11 21:40:08 aaron 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.
17: .\" 3. All advertising materials mentioning features or use of this software
18: .\" must display the following acknowledgement:
19: .\" This product includes software developed by the University of
20: .\" California, Berkeley and its contributors.
21: .\" 4. Neither the name of the University nor the names of its contributors
22: .\" may be used to endorse or promote products derived from this software
23: .\" without specific prior written permission.
24: .\"
25: .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26: .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27: .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28: .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29: .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30: .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31: .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32: .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33: .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34: .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35: .\" SUCH DAMAGE.
36: .\"
37: .\" from: @(#)sed.1 8.2 (Berkeley) 12/30/93
38: .\"
1.7 aaron 39: .Dd December 30, 1993
1.1 deraadt 40: .Dt SED 1
41: .Os
42: .Sh NAME
43: .Nm sed
44: .Nd stream editor
45: .Sh SYNOPSIS
46: .Nm sed
47: .Op Fl an
48: .Ar command
49: .Op Ar file ...
50: .Nm sed
51: .Op Fl an
52: .Op Fl e Ar command
53: .Op Fl f Ar command_file
54: .Op Ar file ...
55: .Sh DESCRIPTION
56: The
1.8 aaron 57: .Nm
1.1 deraadt 58: utility reads the specified files, or the standard input if no files
59: are specified, modifying the input as specified by a list of commands.
60: The input is then written to the standard output.
61: .Pp
62: A single command may be specified as the first argument to
63: .Nm sed .
64: Multiple commands may be specified by using the
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.1 deraadt 73: .Bl -tag -width indent
74: .It Fl a
75: The files listed as parameters for the
1.8 aaron 76: .Ql w
1.1 deraadt 77: functions are created (or truncated) before any processing begins,
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.8 aaron 84: .Ql w
1.1 deraadt 85: function is applied to a line of input.
86: .It Fl e Ar command
87: Append the editing commands specified by the
88: .Ar command
89: argument
90: to the list of commands.
91: .It Fl f Ar command_file
92: Append the editing commands found in the file
93: .Ar command_file
94: to the list of commands.
95: The editing commands should each be listed on a separate line.
96: .It Fl n
97: By default, each line of input is echoed to the standard output after
98: all of the commands have been applied to it.
99: The
100: .Fl n
101: option suppresses this behavior.
102: .El
103: .Pp
104: The form of a
1.8 aaron 105: .Nm
1.1 deraadt 106: command is as follows:
107: .sp
108: .Dl [address[,address]]function[arguments]
109: .sp
110: Whitespace may be inserted before the first address and the function
111: portions of the command.
112: .Pp
113: Normally,
1.8 aaron 114: .Nm
1.1 deraadt 115: cyclically copies a line of input, not including its terminating newline
116: character, into a
117: .Em "pattern space" ,
118: (unless there is something left after a
1.8 aaron 119: .Sq D
1.1 deraadt 120: function),
121: applies all of the commands with addresses that select that pattern space,
122: copies the pattern space to the standard output, appending a newline, and
123: deletes the pattern space.
124: .Pp
125: Some of the functions use a
126: .Em "hold space"
127: to save all or part of the pattern space for subsequent retrieval.
128: .Sh "Sed Addresses"
129: An address is not required, but if specified must be a number (that counts
130: input lines
131: cumulatively across input files), a dollar
1.8 aaron 132: .Pq Ql $
1.1 deraadt 133: character that addresses the last line of input, or a context address
134: (which consists of a regular expression preceded and followed by a
135: delimiter).
136: .Pp
137: A command line with no addresses selects every pattern space.
138: .Pp
139: A command line with one address selects all of the pattern spaces
140: that match the address.
141: .Pp
142: A command line with two addresses selects the inclusive range from
143: the first pattern space that matches the first address through the next
144: pattern space that matches the second.
145: (If the second address is a number less than or equal to the line number
146: first selected, only that line is selected.)
147: Starting at the first line following the selected range,
1.8 aaron 148: .Nm
1.1 deraadt 149: starts looking again for the first address.
150: .Pp
151: Editing commands can be applied to non-selected pattern spaces by use
152: of the exclamation character
1.8 aaron 153: .Pq Ql !
1.1 deraadt 154: function.
155: .Sh "Sed Regular Expressions"
156: The
1.8 aaron 157: .Nm
1.1 deraadt 158: regular expressions are basic regular expressions (BRE's, see
159: .Xr regex 3
160: for more information).
161: In addition,
1.8 aaron 162: .Nm
1.1 deraadt 163: has the following two additions to BRE's:
164: .sp
165: .Bl -enum -compact
166: .It
167: In a context address, any character other than a backslash
1.8 aaron 168: .Pq Ql \e
1.1 deraadt 169: or newline character may be used to delimit the regular expression.
170: Also, putting a backslash character before the delimiting character
171: causes the character to be treated literally.
172: For example, in the context address \exabc\exdefx, the RE delimiter
173: is an
1.8 aaron 174: .Sq x
1.1 deraadt 175: and the second
1.8 aaron 176: .Sq x
1.1 deraadt 177: stands for itself, so that the regular expression is
178: .Dq abcxdef .
179: .sp
180: .It
181: The escape sequence \en matches a newline character embedded in the
182: pattern space.
183: You can't, however, use a literal newline character in an address or
184: in the substitute command.
185: .El
186: .Pp
187: One special feature of
1.8 aaron 188: .Nm
1.1 deraadt 189: regular expressions is that they can default to the last regular
190: expression used.
1.13 aaron 191: If a regular expression is empty, i.e., just the delimiter characters
1.1 deraadt 192: are specified, the last regular expression encountered is used instead.
193: The last regular expression is defined as the last regular expression
194: used as part of an address or substitute command, and at run-time, not
195: compile-time.
196: For example, the command
197: .Dq /abc/s//XXX/
198: will substitute
199: .Dq XXX
200: for the pattern
201: .Dq abc .
202: .Sh "Sed Functions"
203: In the following list of commands, the maximum number of permissible
204: addresses for each command is indicated by [0addr], [1addr], or [2addr],
205: representing zero, one, or two addresses.
206: .Pp
207: The argument
208: .Em text
209: consists of one or more lines.
210: To embed a newline in the text, precede it with a backslash.
211: Other backslashes in text are deleted and the following character
212: taken literally.
213: .Pp
214: The
1.8 aaron 215: .Sq r
1.1 deraadt 216: and
1.8 aaron 217: .Sq w
1.1 deraadt 218: functions take an optional file parameter, which should be separated
1.9 aaron 219: from the function letter by whitespace.
1.1 deraadt 220: Each file given as an argument to
1.8 aaron 221: .Nm
1.1 deraadt 222: is created (or its contents truncated) before any input processing begins.
223: .Pp
224: The
1.8 aaron 225: .Sq b ,
226: .Sq r ,
227: .Sq s ,
228: .Sq t ,
229: .Sq w ,
230: .Sq y ,
231: .Ql ! ,
1.1 deraadt 232: and
1.8 aaron 233: .Ql \&:
1.1 deraadt 234: functions all accept additional arguments.
235: The following synopses indicate which arguments have to be separated from
1.9 aaron 236: the function letters by whitespace characters.
1.1 deraadt 237: .Pp
238: Two of the functions take a function-list.
239: This is a list of
1.8 aaron 240: .Nm
1.1 deraadt 241: functions separated by newlines, as follows:
242: .Bd -literal -offset indent
243: { function
244: function
245: ...
246: function
247: }
248: .Ed
249: .Pp
250: The
1.8 aaron 251: .Ql {
1.9 aaron 252: can be preceded or followed by whitespace.
253: The function can be preceded by whitespace as well.
1.1 deraadt 254: The terminating
1.8 aaron 255: .Ql }
1.9 aaron 256: must be preceded by a newline or optional whitespace.
1.1 deraadt 257: .sp
258: .Bl -tag -width "XXXXXX" -compact
259: .It [2addr] function-list
260: Execute function-list only when the pattern space is selected.
261: .sp
262: .It [1addr]a\e
263: .It text
264: .br
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.
271: .sp
272: .It [2addr]b[label]
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.
277: .sp
278: .It [2addr]c\e
279: .It text
280: .br
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.
285: .sp
286: .It [2addr]d
287: Delete the pattern space and start the next cycle.
288: .sp
289: .It [2addr]D
290: Delete the initial segment of the pattern space through the first
291: newline character and start the next cycle.
292: .sp
293: .It [2addr]g
294: Replace the contents of the pattern space with the contents of the
295: hold space.
296: .sp
297: .It [2addr]G
298: Append a newline character followed by the contents of the hold space
299: to the pattern space.
300: .sp
301: .It [2addr]h
302: Replace the contents of the hold space with the contents of the
303: pattern space.
304: .sp
305: .It [2addr]H
306: Append a newline character followed by the contents of the pattern space
307: to the hold space.
308: .sp
309: .It [1addr]i\e
310: .It text
311: .br
312: Write
313: .Em text
314: to the standard output.
315: .sp
316: .It [2addr]l
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:
321: .sp
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
339: Nonprintable characters are written as three-digit octal numbers (with a
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.1 deraadt 346: .sp
347: .It [2addr]n
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.
351: .sp
352: .It [2addr]N
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.
357: .sp
358: .It [2addr]p
359: Write the pattern space to standard output.
360: .sp
361: .It [2addr]P
362: Write the pattern space, up to the first newline character to the
363: standard output.
364: .sp
365: .It [1addr]q
366: Branch to the end of the script and quit without starting a new cycle.
367: .sp
368: .It [1addr]r file
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.
377: .sp
378: .It [2addr]s/regular expression/replacement/flags
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
408: .It "0 ... 9"
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
425: .sp
426: .It [2addr]t [label]
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.
434: .sp
435: .It [2addr]w Em file
436: Append the pattern space to the
437: .Em file .
438: .sp
439: .It [2addr]x
440: Swap the contents of the pattern and hold spaces.
441: .sp
442: .It [2addr]y/string1/string2/
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.1 deraadt 457: .sp
458: .It [2addr]!function
459: .It [2addr]!function-list
460: Apply the function or function-list only to the lines that are
461: .Em not
462: selected by the address(es).
463: .sp
464: .It [0addr]:label
465: This function does nothing; it bears a label to which the
1.8 aaron 466: .Sq b
1.1 deraadt 467: and
1.8 aaron 468: .Sq t
1.1 deraadt 469: commands may branch.
470: .sp
471: .It [1addr]=
472: Write the line number to the standard output followed by a newline
473: character.
474: .sp
475: .It [0addr]
476: Empty lines are ignored.
477: .sp
478: .It [0addr]#
479: The
1.8 aaron 480: .Ql #
1.1 deraadt 481: and the remainder of the line are ignored (treated as a comment), with
482: the single exception that if the first two characters in the file are
1.8 aaron 483: .Ql #n ,
1.1 deraadt 484: the default output is suppressed.
485: This is the same as specifying the
486: .Fl n
487: option on the command line.
488: .El
489: .Pp
490: The
1.8 aaron 491: .Nm
1.5 aaron 492: utility exits 0 on success or >0 if an error occurred.
1.1 deraadt 493: .Sh SEE ALSO
494: .Xr awk 1 ,
495: .Xr ed 1 ,
496: .Xr grep 1 ,
497: .Xr regex 3 ,
498: .Xr re_format 7
499: .Sh STANDARDS
500: The
1.8 aaron 501: .Nm
1.1 deraadt 502: function is expected to be a superset of the
503: .St -p1003.2
504: specification.
1.11 aaron 505: .Sh HISTORY
506: A
507: .Nm
508: command appeared in
509: .At v7 .