3 b5fdffee 2004-04-19 devnull 9sed \- stream editor
4 cfa37a7b 2004-04-10 devnull .SH SYNOPSIS
14 cfa37a7b 2004-04-10 devnull .I script
21 cfa37a7b 2004-04-10 devnull .I file ...
23 cfa37a7b 2004-04-10 devnull .SH DESCRIPTION
25 cfa37a7b 2004-04-10 devnull copies the named
27 cfa37a7b 2004-04-10 devnull (standard input default) to the standard output,
28 cfa37a7b 2004-04-10 devnull edited according to a script of commands.
31 cfa37a7b 2004-04-10 devnull option causes the script to be taken from file
32 cfa37a7b 2004-04-10 devnull .IR sfile ;
33 cfa37a7b 2004-04-10 devnull these options accumulate.
34 cfa37a7b 2004-04-10 devnull If there is just one
36 cfa37a7b 2004-04-10 devnull option and no
37 cfa37a7b 2004-04-10 devnull .BR -f 's,
40 cfa37a7b 2004-04-10 devnull may be omitted.
43 cfa37a7b 2004-04-10 devnull option suppresses the default output;
45 cfa37a7b 2004-04-10 devnull causes all substitutions to be global, as if suffixed
48 cfa37a7b 2004-04-10 devnull A script consists of editing commands, one per line,
49 cfa37a7b 2004-04-10 devnull of the following form:
51 cfa37a7b 2004-04-10 devnull [\fIaddress\fR [\fL,\fI address\fR] ] \fIfunction\fR [\fIargument\fR ...]
53 cfa37a7b 2004-04-10 devnull In normal operation
55 cfa37a7b 2004-04-10 devnull cyclically copies a line of input into a
56 cfa37a7b 2004-04-10 devnull .I pattern space
57 cfa37a7b 2004-04-10 devnull (unless there is something left after
60 cfa37a7b 2004-04-10 devnull command),
61 cfa37a7b 2004-04-10 devnull applies in sequence
62 cfa37a7b 2004-04-10 devnull all commands whose
63 cfa37a7b 2004-04-10 devnull .I addresses
64 cfa37a7b 2004-04-10 devnull select that pattern space,
65 cfa37a7b 2004-04-10 devnull and at the end of the script copies the pattern space
66 cfa37a7b 2004-04-10 devnull to the standard output (except under
68 cfa37a7b 2004-04-10 devnull and deletes the pattern space.
71 cfa37a7b 2004-04-10 devnull .I address
72 cfa37a7b 2004-04-10 devnull is either a decimal number that counts
73 cfa37a7b 2004-04-10 devnull input lines cumulatively across files, a
76 cfa37a7b 2004-04-10 devnull addresses the last line of input, or a context address,
77 cfa37a7b 2004-04-10 devnull .BI / regular-expression / \f1,
78 cfa37a7b 2004-04-10 devnull in the style of
79 cfa37a7b 2004-04-10 devnull .IR regexp (6),
80 cfa37a7b 2004-04-10 devnull with the added convention that
82 cfa37a7b 2004-04-10 devnull matches a
83 cfa37a7b 2004-04-10 devnull newline embedded in the pattern space.
85 cfa37a7b 2004-04-10 devnull A command line with no addresses selects every pattern space.
87 cfa37a7b 2004-04-10 devnull A command line with
88 cfa37a7b 2004-04-10 devnull one address selects each pattern space that matches the address.
90 cfa37a7b 2004-04-10 devnull A command line with
91 cfa37a7b 2004-04-10 devnull two addresses selects the inclusive range from the first
92 cfa37a7b 2004-04-10 devnull pattern space that matches the first address through
93 cfa37a7b 2004-04-10 devnull the next pattern space that matches
94 cfa37a7b 2004-04-10 devnull the second.
95 cfa37a7b 2004-04-10 devnull (If the second address is a number less than or equal
96 cfa37a7b 2004-04-10 devnull to the line number first selected, only one
97 cfa37a7b 2004-04-10 devnull line is selected.)
98 cfa37a7b 2004-04-10 devnull Thereafter the process is repeated, looking again for the
99 cfa37a7b 2004-04-10 devnull first address.
101 cfa37a7b 2004-04-10 devnull Editing commands can be applied to non-selected pattern
102 cfa37a7b 2004-04-10 devnull spaces by use of the negation function
104 cfa37a7b 2004-04-10 devnull (below).
106 cfa37a7b 2004-04-10 devnull An argument denoted
108 cfa37a7b 2004-04-10 devnull consists of one or more lines,
109 cfa37a7b 2004-04-10 devnull all but the last of which end with
111 cfa37a7b 2004-04-10 devnull to hide the
112 cfa37a7b 2004-04-10 devnull newline.
113 cfa37a7b 2004-04-10 devnull Backslashes in text are treated like backslashes
114 cfa37a7b 2004-04-10 devnull in the replacement string of an
116 cfa37a7b 2004-04-10 devnull command,
117 cfa37a7b 2004-04-10 devnull and may be used to protect initial blanks and tabs
118 cfa37a7b 2004-04-10 devnull against the stripping that is done on
119 cfa37a7b 2004-04-10 devnull every script line.
121 cfa37a7b 2004-04-10 devnull An argument denoted
122 cfa37a7b 2004-04-10 devnull .I rfile
124 cfa37a7b 2004-04-10 devnull .I wfile
125 cfa37a7b 2004-04-10 devnull must terminate the command
126 cfa37a7b 2004-04-10 devnull line and must be preceded by exactly one blank.
128 cfa37a7b 2004-04-10 devnull .I wfile
129 cfa37a7b 2004-04-10 devnull is created before processing begins.
130 cfa37a7b 2004-04-10 devnull There can be at most 120 distinct
131 cfa37a7b 2004-04-10 devnull .I wfile
132 cfa37a7b 2004-04-10 devnull arguments.
133 cfa37a7b 2004-04-10 devnull .TP \w'\fL!\ \fIfunction\fLXXX'u
142 cfa37a7b 2004-04-10 devnull on the output before
143 cfa37a7b 2004-04-10 devnull reading the next input line.
145 cfa37a7b 2004-04-10 devnull .BI b " label"
146 cfa37a7b 2004-04-10 devnull Branch to the
148 cfa37a7b 2004-04-10 devnull command bearing the
149 cfa37a7b 2004-04-10 devnull .IR label .
151 cfa37a7b 2004-04-10 devnull .I label
152 cfa37a7b 2004-04-10 devnull is empty, branch to the end of the script.
160 cfa37a7b 2004-04-10 devnull Delete the pattern space.
161 cfa37a7b 2004-04-10 devnull With 0 or 1 address or at the end of a 2-address range, place
163 cfa37a7b 2004-04-10 devnull on the output.
164 cfa37a7b 2004-04-10 devnull Start the next cycle.
167 cfa37a7b 2004-04-10 devnull Delete the pattern space.
168 cfa37a7b 2004-04-10 devnull Start the next cycle.
171 cfa37a7b 2004-04-10 devnull Delete the initial segment of the
172 cfa37a7b 2004-04-10 devnull pattern space through the first newline.
173 cfa37a7b 2004-04-10 devnull Start the next cycle.
176 cfa37a7b 2004-04-10 devnull Replace the contents of the pattern space
177 cfa37a7b 2004-04-10 devnull by the contents of the hold space.
180 cfa37a7b 2004-04-10 devnull Append the contents of the hold space to the pattern space.
183 cfa37a7b 2004-04-10 devnull Replace the contents of the hold space by the contents of the pattern space.
186 cfa37a7b 2004-04-10 devnull Append the contents of the pattern space to the hold space.
197 cfa37a7b 2004-04-10 devnull on the standard output.
200 cfa37a7b 2004-04-10 devnull Copy the pattern space to the standard output.
201 cfa37a7b 2004-04-10 devnull Replace the pattern space with the next line of input.
204 cfa37a7b 2004-04-10 devnull Append the next line of input to the pattern space
205 cfa37a7b 2004-04-10 devnull with an embedded newline.
206 cfa37a7b 2004-04-10 devnull (The current line number changes.)
210 cfa37a7b 2004-04-10 devnull Copy the pattern space to the standard output.
213 cfa37a7b 2004-04-10 devnull Copy the initial segment of the pattern space through
214 cfa37a7b 2004-04-10 devnull the first newline to the standard output.
218 cfa37a7b 2004-04-10 devnull Branch to the end of the script.
219 cfa37a7b 2004-04-10 devnull Do not start a new cycle.
221 cfa37a7b 2004-04-10 devnull .BI r " rfile"
222 cfa37a7b 2004-04-10 devnull Read the contents of
223 cfa37a7b 2004-04-10 devnull .IR rfile .
224 cfa37a7b 2004-04-10 devnull Place them on the output before reading
225 cfa37a7b 2004-04-10 devnull the next input line.
227 cfa37a7b 2004-04-10 devnull .B s/\fIregular-expression\fP/\fIreplacement\fP/\fIflags
228 cfa37a7b 2004-04-10 devnull Substitute the
229 cfa37a7b 2004-04-10 devnull .I replacement
230 cfa37a7b 2004-04-10 devnull string for instances of the
231 cfa37a7b 2004-04-10 devnull .I regular-expression
232 cfa37a7b 2004-04-10 devnull in the pattern space.
233 cfa37a7b 2004-04-10 devnull Any character may be used instead of
235 cfa37a7b 2004-04-10 devnull For a fuller description see
236 cfa37a7b 2004-04-10 devnull .IR regexp (6).
237 cfa37a7b 2004-04-10 devnull .I Flags
238 cfa37a7b 2004-04-10 devnull is zero or more of
243 cfa37a7b 2004-04-10 devnull Substitute for all non-overlapping instances of the
244 cfa37a7b 2004-04-10 devnull .I regular expression
245 cfa37a7b 2004-04-10 devnull rather than just the
246 cfa37a7b 2004-04-10 devnull first one.
249 cfa37a7b 2004-04-10 devnull Print the pattern space if a replacement was made.
251 cfa37a7b 2004-04-10 devnull .BI w " wfile"
253 cfa37a7b 2004-04-10 devnull Append the pattern space to
254 cfa37a7b 2004-04-10 devnull .I wfile
255 cfa37a7b 2004-04-10 devnull if a replacement
256 cfa37a7b 2004-04-10 devnull was made.
259 cfa37a7b 2004-04-10 devnull .BI t " label"
261 cfa37a7b 2004-04-10 devnull Branch to the
263 cfa37a7b 2004-04-10 devnull command bearing the
264 cfa37a7b 2004-04-10 devnull .I label
266 cfa37a7b 2004-04-10 devnull substitutions have been made since the most recent
267 cfa37a7b 2004-04-10 devnull reading of an input line or execution of a
270 cfa37a7b 2004-04-10 devnull .I label
271 cfa37a7b 2004-04-10 devnull is empty, branch to the end of the script.
274 cfa37a7b 2004-04-10 devnull .I wfile
277 cfa37a7b 2004-04-10 devnull Append the pattern space to
278 cfa37a7b 2004-04-10 devnull .IR wfile .
281 cfa37a7b 2004-04-10 devnull Exchange the contents of the pattern and hold spaces.
283 cfa37a7b 2004-04-10 devnull .B y/\fIstring1\fP/\fIstring2\fP/
284 cfa37a7b 2004-04-10 devnull Transform.
285 cfa37a7b 2004-04-10 devnull Replace all occurrences of characters in
286 cfa37a7b 2004-04-10 devnull .I string1
287 cfa37a7b 2004-04-10 devnull with the corresponding character in
288 cfa37a7b 2004-04-10 devnull .IR string2 .
289 cfa37a7b 2004-04-10 devnull The lengths of
293 cfa37a7b 2004-04-10 devnull .I string2
294 cfa37a7b 2004-04-10 devnull must be equal.
296 cfa37a7b 2004-04-10 devnull .BI ! "function"
298 cfa37a7b 2004-04-10 devnull Apply the
299 cfa37a7b 2004-04-10 devnull .I function
300 cfa37a7b 2004-04-10 devnull (or group, if
301 cfa37a7b 2004-04-10 devnull .I function
304 cfa37a7b 2004-04-10 devnull only to lines
306 cfa37a7b 2004-04-10 devnull selected by the address(es).
308 cfa37a7b 2004-04-10 devnull .BI : " label"
309 cfa37a7b 2004-04-10 devnull This command does nothing; it bears a
310 cfa37a7b 2004-04-10 devnull .I label
315 cfa37a7b 2004-04-10 devnull commands to branch to.
318 cfa37a7b 2004-04-10 devnull Place the current line number on the standard output as a line.
321 cfa37a7b 2004-04-10 devnull Execute the following commands through a matching
323 cfa37a7b 2004-04-10 devnull only when the pattern space is selected.
326 cfa37a7b 2004-04-10 devnull An empty command is ignored.
328 cfa37a7b 2004-04-10 devnull .SH EXAMPLES
330 cfa37a7b 2004-04-10 devnull .B sed 10q file
331 cfa37a7b 2004-04-10 devnull Print the first 10 lines of the file.
333 cfa37a7b 2004-04-10 devnull .B sed '/^$/d'
334 cfa37a7b 2004-04-10 devnull Delete empty lines from standard input.
336 cfa37a7b 2004-04-10 devnull .B sed 's/UNIX/& system/g'
337 cfa37a7b 2004-04-10 devnull Replace every instance of
340 cfa37a7b 2004-04-10 devnull .LR "UNIX system" .
343 cfa37a7b 2004-04-10 devnull sed 's/ *$// \fRdrop trailing blanks\fP
344 cfa37a7b 2004-04-10 devnull /^$/d \fRdrop empty lines\fP
345 cfa37a7b 2004-04-10 devnull s/ */\e \fRreplace blanks by newlines\fP
347 cfa37a7b 2004-04-10 devnull /^$/d' chapter*
351 cfa37a7b 2004-04-10 devnull Print the files
352 cfa37a7b 2004-04-10 devnull .BR chapter1 ,
353 cfa37a7b 2004-04-10 devnull .BR chapter2 ,
354 cfa37a7b 2004-04-10 devnull etc. one word to a line.
357 cfa37a7b 2004-04-10 devnull nroff -ms manuscript | sed '
359 cfa37a7b 2004-04-10 devnull /^$/p \fRif last line of file is empty, print it\fP
361 cfa37a7b 2004-04-10 devnull //N \fRif current line is empty, append next line\fP
362 cfa37a7b 2004-04-10 devnull /^\en$/D' \fRif two lines are empty, delete the first\fP
366 cfa37a7b 2004-04-10 devnull Delete all but one of each group of empty lines from a
367 cfa37a7b 2004-04-10 devnull formatted manuscript.
368 cfa37a7b 2004-04-10 devnull .SH SOURCE
369 b5fdffee 2004-04-19 devnull .B /usr/local/plan9/src/cmd/sed.c
370 cfa37a7b 2004-04-10 devnull .SH SEE ALSO
371 cfa37a7b 2004-04-10 devnull .IR ed (1),
372 cfa37a7b 2004-04-10 devnull .IR grep (1),
373 cfa37a7b 2004-04-10 devnull .IR awk (1),
374 cfa37a7b 2004-04-10 devnull .IR lex (1),
375 cfa37a7b 2004-04-10 devnull .IR sam (1),
376 cfa37a7b 2004-04-10 devnull .IR regexp (6)
378 cfa37a7b 2004-04-10 devnull L. E. McMahon,
379 cfa37a7b 2004-04-10 devnull `SED \(em A Non-interactive Text Editor',
380 cfa37a7b 2004-04-10 devnull Unix Research System Programmer's Manual, Volume 2.
381 cfa37a7b 2004-04-10 devnull .SH BUGS
382 cfa37a7b 2004-04-10 devnull If input is from a pipe, buffering may consume
383 cfa37a7b 2004-04-10 devnull characters beyond a line on which a
385 cfa37a7b 2004-04-10 devnull command is executed.