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