.\" Copyright (c) 1991 The Regents of the University of California.
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\" %sccs.include.redist.roff%
.\" @(#)sed.1 6.7 (Berkeley) %G%
utility is a stream editor which reads one or more
text files, applies given editing command scripts,
and writes the results to standard output.
The script of editing commands can be given in the command
line, or can be contained in the file
is used to edit the input.
options are given, the scripts are
applied in the order given to each line of the
option is given in addition
lines are acted upon by scripts first.
is expected to contain editing commands, one per line,
and these commands are applied to the input.
options are given, the commands in the
the order given to each line of the input
option is given in addition to
option suppresses the default output, which normally
passes each line, after it is examined for editing,
Therefore, only lines explicitly
selected for output are written.
The following operands are available:
A pathname of a file whose contents are read and
If multiple file operands are given,
the named files are read in the order given and
the concatenation is edited.
are given, the standard input is used.
The script consists of one or more editing
instructions that are entered on the command line.
.\" The following environment variable affects the execution of
.\" The locale for character classification.
.Bd -filled -offset indent -compact
cyclically copies a line of input
into a pattern space (unless there is something left after a
command), applies in sequence all commands whose addresses
select that pattern space, and at the end of the script
copies the pattern space to the standard output (except
and deletes the pattern space.
Some of the commands use a hold space to save all or part of
.\" The pattern and hold spaces are each
.\" .Pf { Dv SED_PATTERN_MAX Ns }
An address is either no address; a decimal number that
counts input lines cumulatively across files; a
addresses the last line of input; a context address; or
A command line with no addresses selects every pattern
A command line with one address selects each pattern space
that matches the address.
A command line with two addresses selects the inclusive
range from the first pattern space that matches the first
address through the next pattern space which matches the
(If the second address is a number less than or
equal to the line number first selected, only one line is
Starting at the first line following the
looks again for the first address.
Thereafter the process is repeated.
Editing commands can be applied only to non-selected pattern
spaces by use of the negation function
utility uses basic regular expressions, as are found in the
with the following additions:
In a context address, the construction
is any character, is identical to
itself, so that the regular expression is
matches a <newline> embedded
matches any character except the terminal <newline> of the pattern space.
In the following list of functions the maximum number of
permissible addresses for each function is indicated by
representing zero, one, or two
The argument text consists of one or more lines.
embedded <newline> in the text shall be preceded by a
Other backslashes in text are treated like
backslashes in the replacement string of an s command, and
can be used to protect initial <blank>s against the stripping
that is done on every script line.
commands take an optional
parameter, separated from the command letter by zero or more
is created before processing
There can be at most ten distinct
The following synopses indicate which arguments
are separated from the commands by <blank>s.
.Bl -tag -width addrcommandxx
.Oo Ad 2addr Oc \&{ command_list \&}
Executes command_list only when the pattern
The {} braces can be preceded
and followed by white space.
.Oo Ad 1addr Oc Ns Cm a Ar text
Writes text to the standard output after the
pattern space is written.
.Oo Ad 2addr Oc Ns Cm b Ar label
command bearing the label.
If label is empty, branch to the end of the
.Oo Ad 2addr Oc Ns Cm c Ar text
Deletes the pattern space.
address or at the end of a 2-address range,
places text on the output.
Deletes the pattern space and starts the next
Deletes the initial segment of the pattern
space through the first <newline> and starts
Replaces the contents of the pattern space by
the contents of the hold space.
Appends the contents of the hold space to the
Replaces the contents of the hold space by
the contents of the pattern space.
Appends the contents of the pattern space to
.Oo Ad 1addr Oc Ns Cm i Ar text
Writes text to the standard output before the
pattern space is written.
Lists the pattern space on the standard out-
put in an unambiguous form.
characters are listed as hexadecimal digit
pairs, with a preceding backslash, with the
.Bl -column <carriagexreturn> -offset indent
Long lines are folded; the length at which
folding occurs is ungiven, but should be
appropriate for the output device.
Copies the pattern space to the standard output
and replaces the pattern space with the
Appends the next line of input to the pattern
space, using an embedded <newline> to
separate the appended material from the
Note that the current line
the pattern space, up to the
first <newline>, to the standard output.
Branches to the end of the script and quits
without starting a new cycle.
.Oo Ad 1addr Oc Ns Cm r Ar rfile
Read the contents of rfile.
output before reading the next input line.
.Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags
Substitutes the replacement string for
instances of the regular expression in the
Any character can be used
The value of flags is zero or
n=1-512. Substitutes for the
occurrence only of the regular
expression found within the pattern space.
Globally substitutes for all
non-overlapping instances of the regular
expression rather than just
Prints the pattern space if a
Write. Appends the pattern space
.Oo Ad 2addr Oc Ns Cm t Ar label
label if any substitutions have been made
since the most recent reading of an input
branches to the end of the script.
.Oo Ad 2addr Oc Ns Cm w Ar wfile
Exchanges the contents of the pattern and
.Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/
Replaces all occurrences of collating
.Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction
Applies the function (or group, if function
is {) only to the lines that are not selected
.Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel
This command does nothing; it bears a label
for the b and t commands to branch to.
.Oo Ad 1addr Oc Ns Cm \&=
Places the current line number on the standard
output as a line with its own line
An empty command is ignored.
appears as the first character on any
line of a script file, that entire line is
ignored (treated as a comment), with the single
exception that if the first line of the
utility exits 0 on success, and >0 if an error occurs.
If one or more of the input (not script) files cannot be
continues to process the remaining
utility is expected to be