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