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