]> git.lizzy.rs Git - plan9front.git/blob - sys/man/1/sed
merge
[plan9front.git] / sys / man / 1 / sed
1 .TH SED 1
2 .SH NAME
3 sed \- stream editor
4 .SH SYNOPSIS
5 .B sed
6 [
7 .B -n
8 ]
9 [
10 .B -g
11 ]
12 [
13 .B -e
14 .I script
15 ]
16 [
17 .B -f
18 .I sfile
19 ]
20 [
21 .I file ...
22 ]
23 .SH DESCRIPTION
24 .I Sed
25 copies the named
26 .I files
27 (standard input default) to the standard output,
28 edited according to a script of commands.
29 The
30 .B -f
31 option causes the script to be taken from file
32 .IR sfile ;
33 these options accumulate.
34 If there is just one
35 .B -e
36 option and no
37 .BR -f 's,
38 the option
39 .B -e
40 may be omitted.
41 The
42 .B -n
43 option suppresses the default output;
44 .B -g
45 causes all substitutions to be global, as if suffixed
46 .BR g .
47 .PP
48 A script consists of editing commands, one per line,
49 of the following form:
50 .IP
51 [\fIaddress\fR [\fL,\fI address\fR] ] \fIfunction\fR [\fIargument\fR ...] [\fL;\fP]
52 .PP
53 In normal operation
54 .I sed
55 cyclically copies a line of input into a
56 .I pattern space
57 (unless there is something left after
58 a
59 .L D
60 command),
61 applies in sequence
62 all commands whose
63 .I addresses
64 select that pattern space,
65 and at the end of the script copies the pattern space
66 to the standard output (except under
67 .BR -n )
68 and deletes the pattern space.
69 .PP
70 An
71 .I address
72 is either a decimal number that counts
73 input lines cumulatively across files, a
74 .L $
75 that
76 addresses the last line of input, or a context address,
77 .BI / regular-expression / \f1,
78 in the style of
79 .IR regexp (6),
80 with the added convention that
81 .L \en
82 matches a
83 newline embedded in the pattern space.
84 .PP
85 A command line with no addresses selects every pattern space.
86 .PP
87 A command line with
88 one address selects each pattern space that matches the address.
89 .PP
90 A command line with
91 two addresses selects the inclusive range from the first
92 pattern space that matches the first address through
93 the next pattern space that matches
94 the second.
95 (If the second address is a number less than or equal
96 to the line number first selected, only one
97 line is selected.)
98 Thereafter the process is repeated, looking again for the
99 first address.
100 .PP
101 Editing commands can be applied to non-selected pattern
102 spaces by use of the negation function
103 .L !
104 (below).
105 .PP
106 An argument denoted
107 .I text
108 consists of one or more lines,
109 all but the last of which end with
110 .L \e
111 to hide the
112 newline.
113 Backslashes in text are treated like backslashes
114 in the replacement string of an
115 .L s
116 command,
117 and may be used to protect initial blanks and tabs
118 against the stripping that is done on
119 every script line.
120 .PP
121 An argument denoted
122 .I rfile
123 or
124 .I wfile
125 must terminate the command
126 line and must be preceded by exactly one blank.
127 Each
128 .I wfile
129 is created before processing begins.
130 There can be at most 120 distinct
131 .I wfile
132 arguments.
133 .TP \w'\fL!\ \fIfunction\fLXXX'u
134 .B a\e
135 .br
136 .ns
137 .TP
138 .I text
139 Append.
140 Place
141 .I text
142 on the output before
143 reading the next input line.
144 .TP
145 .BI b " label"
146 Branch to the
147 .B :
148 command bearing the
149 .IR label .
150 If
151 .I label
152 is empty, branch to the end of the script.
153 .TP
154 .B c\e
155 .br
156 .ns
157 .TP
158 .I text
159 Change.
160 Delete the pattern space.
161 With 0 or 1 address or at the end of a 2-address range, place
162 .I text
163 on the output.
164 Start the next cycle.
165 .TP
166 .B d
167 Delete the pattern space.
168 Start the next cycle.
169 .TP
170 .B D
171 Delete the initial segment of the
172 pattern space through the first newline.
173 Start the next cycle.
174 .TP
175 .B g
176 Replace the contents of the pattern space
177 by the contents of the hold space.
178 .TP
179 .B G
180 Append the contents of the hold space to the pattern space.
181 .TP
182 .B h
183 Replace the contents of the hold space by the contents of the pattern space.
184 .TP
185 .B H
186 Append the contents of the pattern space to the hold space.
187 .ne 3
188 .TP
189 .B i\e
190 .br
191 .ns
192 .TP
193 .I text
194 Insert.
195 Place
196 .I text
197 on the standard output.
198 .TP
199 .B n
200 Copy the pattern space to the standard output.
201 Replace the pattern space with the next line of input.
202 .TP
203 .B N
204 Append the next line of input to the pattern space
205 with an embedded newline.
206 (The current line number changes.)
207 .TP
208 .B p
209 Print.
210 Copy the pattern space to the standard output.
211 .TP
212 .B P
213 Copy the initial segment of the pattern space through
214 the first newline to the standard output.
215 .TP
216 .B q
217 Quit.
218 Branch to the end of the script.
219 Do not start a new cycle.
220 .TP
221 .BI r " rfile"
222 Read the contents of
223 .IR rfile .
224 Place them on the output before reading
225 the next input line.
226 .TP
227 .B s/\fIregular-expression\fP/\fIreplacement\fP/\fIflags
228 Substitute the
229 .I replacement
230 string for instances of the
231 .I regular-expression
232 in the pattern space.
233 Any character may be used instead of
234 .LR / .
235 For a fuller description see
236 .IR regexp (6).
237 .I Flags
238 is zero or more of
239 .RS
240 .TP
241 .B g
242 Global.
243 Substitute for all non-overlapping instances of the
244 .I regular expression
245 rather than just the
246 first one.
247 .TP
248 .B p
249 Print the pattern space if a replacement was made.
250 .TP
251 .BI w " wfile"
252 Write.
253 Append the pattern space to
254 .I wfile
255 if a replacement
256 was made.
257 .RE
258 .IP
259 An ampersand 
260 .L &
261 appearing in the replacement
262 is replaced by the string matching the regular expression.
263 The characters
264 .BI \e n\f1,
265 where
266 .I n
267 is a digit,
268 are replaced by the text matched by the
269 .IR n -th
270 regular subexpression
271 enclosed between
272 .L (
273 and
274 .LR ) .
275 When
276 nested parenthesized subexpressions
277 are present,
278 .I n
279 is determined by counting occurrences of
280 .L (
281 starting from the left.
282 .TP
283 .BI t " label"
284 Test.
285 Branch to the
286 .L :
287 command bearing the
288 .I label
289 if any
290 substitutions have been made since the most recent
291 reading of an input line or execution of a
292 .LR t .
293 If
294 .I label
295 is empty, branch to the end of the script.
296 .TP
297 .B w
298 .I wfile
299 .br
300 Write.
301 Append the pattern space to
302 .IR wfile .
303 .TP
304 .B x
305 Exchange the contents of the pattern and hold spaces.
306 .TP
307 .B y/\fIstring1\fP/\fIstring2\fP/
308 Transform.
309 Replace all occurrences of characters in
310 .I string1
311 with the corresponding character in
312 .IR string2 .
313 The lengths of
314 .I
315 string1
316 and
317 .I string2
318 must be equal.
319 .TP
320 .BI ! "function"
321 Don't.
322 Apply the
323 .I function
324 (or group, if
325 .I function
326 is
327 .LR { )
328 only to lines
329 .I not
330 selected by the address(es).
331 .TP
332 .B #
333 Comment.
334 Ignore the rest of the line.
335 .TP
336 .BI : " label"
337 This command does nothing; it bears a
338 .I label
339 for
340 .B b
341 and
342 .B t
343 commands to branch to.
344 .TP
345 .B =
346 Place the current line number on the standard output as a line.
347 .TP
348 .B {
349 Execute the following commands through a matching
350 .L }
351 only when the pattern space is selected.
352 .TP
353 .B " "
354 An empty command is ignored.
355 .ne 4
356 .SH EXAMPLES
357 .TP
358 .B sed 10q file
359 Print the first 10 lines of the file.
360 .TP
361 .B sed '/^$/d'
362 Delete empty lines from standard input.
363 .TP
364 .B sed 's/UNIX/& system/g'
365 Replace every instance of
366 .L UNIX
367 by
368 .LR "UNIX system" .
369 .PP
370 .EX
371 sed 's/ *$//    \fRdrop trailing blanks\fP
372 /^$/d           \fRdrop empty lines\fP
373 s/  */\e                \fRreplace blanks by newlines\fP
374 /g
375 /^$/d' chapter*
376 .EE
377 .ns
378 .IP
379 Print the files
380 .BR chapter1 ,
381 .BR chapter2 ,
382 etc. one word to a line.
383 .PP
384 .EX
385 nroff -ms manuscript | sed '
386 ${
387         /^$/p   \fRif last line of file is empty, print it\fP
388 }
389 //N                     \fRif current line is empty, append next line\fP
390 /^\en$/D'               \fRif two lines are empty, delete the first\fP
391 .EE
392 .ns
393 .IP
394 Delete all but one of each group of empty lines from a
395 formatted manuscript.
396 .SH SOURCE
397 .B /sys/src/cmd/sed.c
398 .SH SEE ALSO
399 .IR ed (1),
400 .IR grep (1),
401 .IR awk (1),
402 .IR lex (1),
403 .IR sam (1),
404 .IR regexp (6)
405 .br
406 L. E. McMahon,
407 `SED \(em A Non-interactive Text Editor',
408 Unix Research System Programmer's Manual, Volume 2.
409 .SH BUGS
410 If input is from a pipe, buffering may consume
411 characters beyond a line on which a
412 .L q
413 command is executed.