manual page distributed with 4.1BSD
[unix-history] / usr / src / old / sed / sed.1
CommitLineData
2d33ba58
KM
1.\" @(#)sed.1 4.1 (Berkeley) %G%
2.\"
3.TH SED 1
4.AT 3
5.SH NAME
6sed \- stream editor
7.SH SYNOPSIS
8.B sed
9[
10.B \-n
11]
12[
13.B \-e
14script ] [
15.B \-f
16sfile ] [ file ] ...
17.SH DESCRIPTION
18.I Sed
19copies the named
20.I files
21(standard input default) to the standard output,
22edited according to a script of commands.
23The
24.B \-f
25option causes the script to be taken from file
26.IR sfile ;
27these options accumulate.
28If there is just one
29.B \-e
30option and no
31.BR \-f 's,
32the flag
33.B \-e
34may be omitted.
35The
36.B \-n
37option suppresses the default output.
38.PP
39A script consists of editing commands, one per line,
40of the following form:
41.IP
42[address [, address] ] function [arguments]
43.PP
44In normal operation
45.I sed
46cyclically copies a line of input into a
47.I pattern space
48(unless there is something left after
49a `D' command),
50applies in sequence
51all commands whose
52.I addresses
53select that pattern space,
54and at the end of the script copies the pattern space
55to the standard output (except under
56.BR \-n )
57and deletes the pattern space.
58.PP
59An
60.I address
61is either a decimal number that counts
62input lines cumulatively across files, a `$' that
63addresses the last line of input, or a context address,
64`/regular expression/', in the style of
65.IR ed (1)
66modified thus:
67.IP
68The escape sequence `\en' matches a
69newline embedded in the pattern space.
70.PP
71A command line with no addresses selects every pattern space.
72.PP
73A command line with
74one address selects each pattern space that matches the address.
75.PP
76A command line with
77two addresses selects the inclusive range from the first
78pattern space that matches the first address through
79the next pattern space that matches
80the second.
81(If the second address is a number less than or equal
82to the line number first selected, only one
83line is selected.)
84Thereafter the process is repeated, looking again for the
85first address.
86.PP
87Editing commands can be applied only to non-selected pattern
88spaces by use of the negation function `!' (below).
89.PP
90In the following list of functions the
91maximum number of permissible addresses
92for each function is indicated in parentheses.
93.PP
94An argument denoted
95.I text
96consists of one or more lines,
97all but the last of which end with `\e' to hide the
98newline.
99Backslashes in text are treated like backslashes
100in the replacement string of an `s' command,
101and may be used to protect initial blanks and tabs
102against the stripping that is done on
103every script line.
104.PP
105An argument denoted
106.I rfile
107or
108.I wfile
109must terminate the command
110line and must be preceded by exactly one blank.
111Each
112.I wfile
113is created before processing begins.
114There can be at most 10 distinct
115.I wfile
116arguments.
117.TP
118(1)\|a\e
119.br
120.ns
121.TP
122.I text
123.br
124Append.
125Place
126.I text
127on the output before
128reading the next input line.
129.TP
130.RI (2)\|b " label"
131Branch to the `:' command bearing the
132.IR label .
133If
134.I label
135is empty, branch to the end of the script.
136.TP
137(2)\|c\e
138.br
139.ns
140.TP
141.I text
142.br
143Change.
144Delete the pattern space.
145With 0 or 1 address or at the end of a 2-address range, place
146.I text
147on the output.
148Start the next cycle.
149.TP
150(2)\|d
151Delete the pattern space.
152Start the next cycle.
153.TP
154(2)\|D
155Delete the initial segment of the
156pattern space through the first newline.
157Start the next cycle.
158.TP
159(2)\|g
160Replace the contents of the pattern space
161by the contents of the hold space.
162.TP
163(2)\|G
164Append the contents of the hold space to the pattern space.
165.TP
166(2)\|h
167Replace the contents of the hold space by the contents of the pattern space.
168.TP
169(2)\|H
170Append the contents of the pattern space to the hold space.
171.TP
172(1)\|i\e
173.br
174.ns
175.TP
176.I text
177.br
178Insert.
179Place
180.I text
181on the standard output.
182.TP
183(2)\|n
184Copy the pattern space to the standard output.
185Replace the pattern space with the next line of input.
186.TP
187(2)\|N
188Append the next line of input to the pattern space
189with an embedded newline.
190(The current line number changes.)
191.TP
192(2)\|p
193Print.
194Copy the pattern space to the standard output.
195.TP
196(2)\|P
197Copy the initial segment of the pattern space through
198the first newline to the standard output.
199.TP
200(1)\|q
201Quit.
202Branch to the end of the script.
203Do not start a new cycle.
204.TP
205.RI (2)\|r " rfile"
206Read the contents of
207.IR rfile .
208Place them on the output before reading
209the next input line.
210.TP
211.RI (2)\|s /regular\ expression/replacement/flags
212Substitute the
213.I replacement
214string for instances of the
215.I regular expression
216in the pattern space.
217Any character may be used instead of `/'.
218For a fuller description see
219.IR ed (1).
220.I Flags
221is zero or more of
222.RS
223.TP
224g
225Global.
226Substitute for all nonoverlapping instances of the
227.I regular expression
228rather than just the
229first one.
230.TP
231p
232Print the pattern space if a replacement was made.
233.TP
234.RI w " wfile"
235Write.
236Append the pattern space to
237.I wfile
238if a replacement
239was made.
240.RE
241.TP
242.RI (2)\|t " label"
243Test.
244Branch to the `:' command bearing the
245.I label
246if any
247substitutions have been made since the most recent
248reading of an input line or execution of a `t'.
249If
250.I label
251is empty, branch to the end of the script.
252.TP
253.RI (2)\|w " wfile"
254Write.
255Append the pattern space to
256.IR wfile .
257.TP
258.RI (2)\|x
259Exchange the contents of the pattern and hold spaces.
260.TP
261.RI (2)\|y /string1/string2/
262Transform.
263Replace all occurrences of characters in
264.I string1
265with the corresponding character in
266.I string2.
267The lengths of
268.I
269string1
270and
271.I string2
272must be equal.
273.TP
274.RI (2)! " function"
275Don't.
276Apply the
277.I function
278(or group, if
279.I function
280is `{') only to lines
281.I not
282selected by the address(es).
283.TP
284.RI (0)\|: " label"
285This command does nothing; it bears a
286.I label
287for `b' and `t' commands to branch to.
288.TP
289(1)\|=
290Place the current line number on the standard output as a line.
291.TP
292(2)\|{
293Execute the following commands through a matching `}'
294only when the pattern space is selected.
295.TP
296(0)\|
297An empty command is ignored.
298.SH SEE ALSO
299ed(1), grep(1), awk(1), lex(1)