3 c8b6342d 2005-01-13 devnull sed \- stream editor
4 058b0118 2005-01-03 devnull .SH SYNOPSIS
11 058b0118 2005-01-03 devnull .I script
18 058b0118 2005-01-03 devnull .I file ...
20 058b0118 2005-01-03 devnull .SH DESCRIPTION
22 058b0118 2005-01-03 devnull copies the named
24 058b0118 2005-01-03 devnull (standard input default) to the standard output,
25 058b0118 2005-01-03 devnull edited according to a script of commands.
28 058b0118 2005-01-03 devnull option causes the script to be taken from file
29 058b0118 2005-01-03 devnull .IR sfile ;
30 058b0118 2005-01-03 devnull these options accumulate.
31 058b0118 2005-01-03 devnull If there is just one
33 058b0118 2005-01-03 devnull option and no
34 058b0118 2005-01-03 devnull .BR -f 's,
37 058b0118 2005-01-03 devnull may be omitted.
40 058b0118 2005-01-03 devnull option suppresses the default output;
42 058b0118 2005-01-03 devnull causes all substitutions to be global, as if suffixed
46 751ff8ab 2006-06-12 devnull option causes
48 751ff8ab 2006-06-12 devnull to flush its output buffer after every newline.
50 058b0118 2005-01-03 devnull A script consists of editing commands, one per line,
51 058b0118 2005-01-03 devnull of the following form:
53 058b0118 2005-01-03 devnull [\fIaddress\fR [\fL,\fI address\fR] ] \fIfunction\fR [\fIargument\fR ...]
55 058b0118 2005-01-03 devnull In normal operation
57 058b0118 2005-01-03 devnull cyclically copies a line of input into a
58 058b0118 2005-01-03 devnull .I pattern space
59 058b0118 2005-01-03 devnull (unless there is something left after
62 058b0118 2005-01-03 devnull command),
63 058b0118 2005-01-03 devnull applies in sequence
64 058b0118 2005-01-03 devnull all commands whose
65 058b0118 2005-01-03 devnull .I addresses
66 058b0118 2005-01-03 devnull select that pattern space,
67 058b0118 2005-01-03 devnull and at the end of the script copies the pattern space
68 058b0118 2005-01-03 devnull to the standard output (except under
70 058b0118 2005-01-03 devnull and deletes the pattern space.
73 058b0118 2005-01-03 devnull .I address
74 058b0118 2005-01-03 devnull is either a decimal number that counts
75 058b0118 2005-01-03 devnull input lines cumulatively across files, a
78 058b0118 2005-01-03 devnull addresses the last line of input, or a context address,
79 058b0118 2005-01-03 devnull .BI / regular-expression / \f1,
80 058b0118 2005-01-03 devnull in the style of
81 058b0118 2005-01-03 devnull .IR regexp (7),
82 058b0118 2005-01-03 devnull with the added convention that
84 058b0118 2005-01-03 devnull matches a
85 058b0118 2005-01-03 devnull newline embedded in the pattern space.
87 058b0118 2005-01-03 devnull A command line with no addresses selects every pattern space.
89 058b0118 2005-01-03 devnull A command line with
90 058b0118 2005-01-03 devnull one address selects each pattern space that matches the address.
92 058b0118 2005-01-03 devnull A command line with
93 058b0118 2005-01-03 devnull two addresses selects the inclusive range from the first
94 058b0118 2005-01-03 devnull pattern space that matches the first address through
95 058b0118 2005-01-03 devnull the next pattern space that matches
96 058b0118 2005-01-03 devnull the second.
97 058b0118 2005-01-03 devnull (If the second address is a number less than or equal
98 058b0118 2005-01-03 devnull to the line number first selected, only one
99 058b0118 2005-01-03 devnull line is selected.)
100 058b0118 2005-01-03 devnull Thereafter the process is repeated, looking again for the
101 058b0118 2005-01-03 devnull first address.
103 058b0118 2005-01-03 devnull Editing commands can be applied to non-selected pattern
104 058b0118 2005-01-03 devnull spaces by use of the negation function
106 058b0118 2005-01-03 devnull (below).
108 058b0118 2005-01-03 devnull An argument denoted
110 058b0118 2005-01-03 devnull consists of one or more lines,
111 058b0118 2005-01-03 devnull all but the last of which end with
113 058b0118 2005-01-03 devnull to hide the
114 058b0118 2005-01-03 devnull newline.
115 058b0118 2005-01-03 devnull Backslashes in text are treated like backslashes
116 058b0118 2005-01-03 devnull in the replacement string of an
118 058b0118 2005-01-03 devnull command,
119 058b0118 2005-01-03 devnull and may be used to protect initial blanks and tabs
120 058b0118 2005-01-03 devnull against the stripping that is done on
121 058b0118 2005-01-03 devnull every script line.
123 058b0118 2005-01-03 devnull An argument denoted
124 058b0118 2005-01-03 devnull .I rfile
126 058b0118 2005-01-03 devnull .I wfile
127 058b0118 2005-01-03 devnull must terminate the command
128 058b0118 2005-01-03 devnull line and must be preceded by exactly one blank.
130 058b0118 2005-01-03 devnull .I wfile
131 058b0118 2005-01-03 devnull is created before processing begins.
132 058b0118 2005-01-03 devnull There can be at most 120 distinct
133 058b0118 2005-01-03 devnull .I wfile
134 058b0118 2005-01-03 devnull arguments.
135 058b0118 2005-01-03 devnull .TP \w'\fL!\ \fIfunction\fLXXX'u
144 058b0118 2005-01-03 devnull on the output before
145 058b0118 2005-01-03 devnull reading the next input line.
147 058b0118 2005-01-03 devnull .BI b " label"
148 058b0118 2005-01-03 devnull Branch to the
150 058b0118 2005-01-03 devnull command bearing the
151 058b0118 2005-01-03 devnull .IR label .
153 058b0118 2005-01-03 devnull .I label
154 058b0118 2005-01-03 devnull is empty, branch to the end of the script.
162 058b0118 2005-01-03 devnull Delete the pattern space.
163 058b0118 2005-01-03 devnull With 0 or 1 address or at the end of a 2-address range, place
165 058b0118 2005-01-03 devnull on the output.
166 058b0118 2005-01-03 devnull Start the next cycle.
169 058b0118 2005-01-03 devnull Delete the pattern space.
170 058b0118 2005-01-03 devnull Start the next cycle.
173 058b0118 2005-01-03 devnull Delete the initial segment of the
174 058b0118 2005-01-03 devnull pattern space through the first newline.
175 058b0118 2005-01-03 devnull Start the next cycle.
178 058b0118 2005-01-03 devnull Replace the contents of the pattern space
179 058b0118 2005-01-03 devnull by the contents of the hold space.
182 058b0118 2005-01-03 devnull Append the contents of the hold space to the pattern space.
185 058b0118 2005-01-03 devnull Replace the contents of the hold space by the contents of the pattern space.
188 058b0118 2005-01-03 devnull Append the contents of the pattern space to the hold space.
199 058b0118 2005-01-03 devnull on the standard output.
202 058b0118 2005-01-03 devnull Copy the pattern space to the standard output.
203 058b0118 2005-01-03 devnull Replace the pattern space with the next line of input.
206 058b0118 2005-01-03 devnull Append the next line of input to the pattern space
207 058b0118 2005-01-03 devnull with an embedded newline.
208 058b0118 2005-01-03 devnull (The current line number changes.)
212 058b0118 2005-01-03 devnull Copy the pattern space to the standard output.
215 058b0118 2005-01-03 devnull Copy the initial segment of the pattern space through
216 058b0118 2005-01-03 devnull the first newline to the standard output.
220 058b0118 2005-01-03 devnull Branch to the end of the script.
221 058b0118 2005-01-03 devnull Do not start a new cycle.
223 058b0118 2005-01-03 devnull .BI r " rfile"
224 058b0118 2005-01-03 devnull Read the contents of
225 058b0118 2005-01-03 devnull .IR rfile .
226 058b0118 2005-01-03 devnull Place them on the output before reading
227 058b0118 2005-01-03 devnull the next input line.
229 058b0118 2005-01-03 devnull .B s/\fIregular-expression\fP/\fIreplacement\fP/\fIflags
230 058b0118 2005-01-03 devnull Substitute the
231 058b0118 2005-01-03 devnull .I replacement
232 058b0118 2005-01-03 devnull string for instances of the
233 058b0118 2005-01-03 devnull .I regular-expression
234 058b0118 2005-01-03 devnull in the pattern space.
235 058b0118 2005-01-03 devnull Any character may be used instead of
237 058b0118 2005-01-03 devnull For a fuller description see
238 058b0118 2005-01-03 devnull .IR regexp (7).
239 058b0118 2005-01-03 devnull .I Flags
240 058b0118 2005-01-03 devnull is zero or more of
245 058b0118 2005-01-03 devnull Substitute for all non-overlapping instances of the
246 058b0118 2005-01-03 devnull .I regular expression
247 058b0118 2005-01-03 devnull rather than just the
248 058b0118 2005-01-03 devnull first one.
251 058b0118 2005-01-03 devnull Print the pattern space if a replacement was made.
253 058b0118 2005-01-03 devnull .BI w " wfile"
255 058b0118 2005-01-03 devnull Append the pattern space to
256 058b0118 2005-01-03 devnull .I wfile
257 058b0118 2005-01-03 devnull if a replacement
258 058b0118 2005-01-03 devnull was made.
261 058b0118 2005-01-03 devnull .BI t " label"
263 058b0118 2005-01-03 devnull Branch to the
265 058b0118 2005-01-03 devnull command bearing the
266 058b0118 2005-01-03 devnull .I label
268 058b0118 2005-01-03 devnull substitutions have been made since the most recent
269 058b0118 2005-01-03 devnull reading of an input line or execution of a
272 058b0118 2005-01-03 devnull .I label
273 058b0118 2005-01-03 devnull is empty, branch to the end of the script.
276 058b0118 2005-01-03 devnull .I wfile
279 058b0118 2005-01-03 devnull Append the pattern space to
280 058b0118 2005-01-03 devnull .IR wfile .
283 058b0118 2005-01-03 devnull Exchange the contents of the pattern and hold spaces.
285 058b0118 2005-01-03 devnull .B y/\fIstring1\fP/\fIstring2\fP/
286 058b0118 2005-01-03 devnull Transform.
287 058b0118 2005-01-03 devnull Replace all occurrences of characters in
288 058b0118 2005-01-03 devnull .I string1
289 058b0118 2005-01-03 devnull with the corresponding character in
290 058b0118 2005-01-03 devnull .IR string2 .
291 058b0118 2005-01-03 devnull The lengths of
295 058b0118 2005-01-03 devnull .I string2
296 058b0118 2005-01-03 devnull must be equal.
298 058b0118 2005-01-03 devnull .BI ! "function"
300 058b0118 2005-01-03 devnull Apply the
301 058b0118 2005-01-03 devnull .I function
302 058b0118 2005-01-03 devnull (or group, if
303 058b0118 2005-01-03 devnull .I function
306 058b0118 2005-01-03 devnull only to lines
308 058b0118 2005-01-03 devnull selected by the address(es).
310 058b0118 2005-01-03 devnull .BI : " label"
311 058b0118 2005-01-03 devnull This command does nothing; it bears a
312 058b0118 2005-01-03 devnull .I label
317 058b0118 2005-01-03 devnull commands to branch to.
320 058b0118 2005-01-03 devnull Place the current line number on the standard output as a line.
323 058b0118 2005-01-03 devnull Execute the following commands through a matching
325 058b0118 2005-01-03 devnull only when the pattern space is selected.
328 058b0118 2005-01-03 devnull An empty command is ignored.
330 058b0118 2005-01-03 devnull .SH EXAMPLES
332 058b0118 2005-01-03 devnull .B sed 10q file
333 058b0118 2005-01-03 devnull Print the first 10 lines of the file.
335 058b0118 2005-01-03 devnull .B sed '/^$/d'
336 058b0118 2005-01-03 devnull Delete empty lines from standard input.
338 058b0118 2005-01-03 devnull .B sed 's/UNIX/& system/g'
339 058b0118 2005-01-03 devnull Replace every instance of
342 058b0118 2005-01-03 devnull .LR "UNIX system" .
345 058b0118 2005-01-03 devnull sed 's/ *$// \fRdrop trailing blanks\fP
346 058b0118 2005-01-03 devnull /^$/d \fRdrop empty lines\fP
347 058b0118 2005-01-03 devnull s/ */\e \fRreplace blanks by newlines\fP
349 058b0118 2005-01-03 devnull /^$/d' chapter*
353 058b0118 2005-01-03 devnull Print the files
354 058b0118 2005-01-03 devnull .BR chapter1 ,
355 058b0118 2005-01-03 devnull .BR chapter2 ,
356 058b0118 2005-01-03 devnull etc. one word to a line.
359 058b0118 2005-01-03 devnull nroff -ms manuscript | sed '
361 058b0118 2005-01-03 devnull /^$/p \fRif last line of file is empty, print it\fP
363 058b0118 2005-01-03 devnull //N \fRif current line is empty, append next line\fP
364 058b0118 2005-01-03 devnull /^\en$/D' \fRif two lines are empty, delete the first\fP
368 058b0118 2005-01-03 devnull Delete all but one of each group of empty lines from a
369 058b0118 2005-01-03 devnull formatted manuscript.
370 058b0118 2005-01-03 devnull .SH SOURCE
371 c8b6342d 2005-01-13 devnull .B \*9/src/cmd/sed.c
372 058b0118 2005-01-03 devnull .SH SEE ALSO
373 058b0118 2005-01-03 devnull .IR ed (1),
374 058b0118 2005-01-03 devnull .IR grep (1),
375 058b0118 2005-01-03 devnull .IR awk (1),
376 058b0118 2005-01-03 devnull .IR lex (1),
377 058b0118 2005-01-03 devnull .IR sam (1),
378 058b0118 2005-01-03 devnull .IR regexp (7)
380 058b0118 2005-01-03 devnull L. E. McMahon,
381 058b0118 2005-01-03 devnull `SED \(em A Non-interactive Text Editor',
382 058b0118 2005-01-03 devnull Unix Research System Programmer's Manual, Volume 2.
383 058b0118 2005-01-03 devnull .SH BUGS
384 058b0118 2005-01-03 devnull If input is from a pipe, buffering may consume
385 058b0118 2005-01-03 devnull characters beyond a line on which a
387 058b0118 2005-01-03 devnull command is executed.