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