SysIII/usr/src/man/man1/sed.1
.TH SED 1
.SH NAME
sed \- stream editor
.SH SYNOPSIS
.B sed
[
.B \-n
] [
.B \-e
script ] [
.B \-f
sfile ] [ files ]
.SH DESCRIPTION
.I Sed\^
copies the named
.I files\^
(standard input default) to the standard output,
edited according to a script of commands.
The
.B \-f
option causes the script to be taken from file
.IR sfile ;
these options accumulate.
If there is just one
.B \-e
option and no
.B \-f
options,
the flag
.B \-e
may be omitted.
The
.B \-n
option suppresses the default output.
A script consists of editing commands, one per line,
of the following form:
.PP
.RS
[ \|address \|[ \|, \|address \|] \|] \|function \|[ \|arguments \|]
.RE
.PP
In normal operation,
.I sed\^
cyclically copies a line of input into a
.I pattern space\^
(unless there is something left after
a
.B D
command),
applies in sequence
all commands whose
.I addresses\^
select that pattern space,
and at the end of the script copies the pattern space
to the standard output (except under
.BR \-n )
and deletes the pattern space.
.PP
Some of the commands use a
.I hold space\^
to save all or part of the
.I pattern space\^
for subsequent retrieval.
.PP
An
.I address\^
is either a decimal number that counts
input lines cumulatively across files, a
.B $
that
addresses the last line of input, or a context address,
i.e.,
a
.BI / "regular expression" /
in the style of
.IR ed (1)
modified thus:
.PP
.PD 0
.RS
.HP
In a context address, the construction
\f3\e\fP\f2?regular expression?\^\fP,
where
.IR ?
is any character,
is identical to
.BI / "regular expression" /\fR.\fP
Note that in the context address
.BR \exabc\exdefx ,
the second
.B x
stands for itself, so that the
regular expression is
.BR abcxdef .
.HP
The escape sequence
.B \en
matches a
new-line
.I embedded\^
in the pattern space.
.HP
A period
.B .
matches any character except the
.I terminal\^
new-line of the pattern space.
.HP
A command line with no addresses selects every pattern space.
.HP
A command line with
one address selects each pattern space that matches the address.
.HP
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 that matches
the second.
(If the second address is a number less than or equal
to the line number first selected, only one
line is selected.)\
Thereafter the process is repeated, looking again for the
first address.
.RE
.PD
.PP
Editing commands can be applied only to non-selected pattern
spaces by use of the negation function
.B !
(below).
.PP
In the following list of functions the
maximum number of permissible addresses
for each function is indicated in parentheses.
.PP
The
.I text\^
argument
consists of one or more lines,
all but the last of which end with
.B \e
to hide the
new-line.
Backslashes in text are treated like backslashes
in the replacement string of an
.B s
command,
and may be used to protect initial blanks and tabs
against the stripping that is done on
every script line.
The
.I rfile\^
or
.I wfile\^
argument
must terminate the command
line and must be preceded by exactly one blank.
Each
.I wfile\^
is created before processing begins.
There can be at most 10 distinct
.I wfile\^
arguments.
.PP
.PD 0
.TP 10
(1)\|\f3a\e\fP
.br
.ns
.TP
.I text\^
Append.
Place
.I text\^
on the output before
reading the next input line.
.TP
.RI (2)\|\f3b\fP " label\^"
Branch to the
.B :
command bearing the
.IR label .
If
.I label\^
is empty, branch to the end of the script.
.br
.ne 2.1v
.TP
(2)\|\f3c\e\fP
.br
.ns
.TP
.I text\^
Change.
Delete the pattern space.
With 0 or 1 address or at the end of a 2-address range, place
.I text\^
on the output.
Start the next cycle.
.TP
(2)\|\f3d\fP
Delete the pattern space.
Start the next cycle.
.TP
(2)\|\f3D\fP
Delete the initial segment of the
pattern space through the first new-line.
Start the next cycle.
.TP
(2)\|\f3g\fP
Replace the contents of the pattern space
by the contents of the hold space.
.TP
(2)\|\f3G\fP
Append the contents of the hold space to the pattern space.
.TP
(2)\|\f3h\fP
Replace the contents of the hold space by the contents of the pattern space.
.TP
(2)\|\f3H\fP
Append the contents of the pattern space to the hold space.
.TP
(1)\|\f3i\e\fP
.br
.ns
.TP
.I text\^
Insert.
Place
.I text\^
on the standard output.
.TP
(2)\|\f3l\fP
List the pattern space on the standard output in an
unambiguous form.
Non-printing characters are spelled in two-digit
.SM ASCII
and long lines are folded.
.TP
(2)\|\f3n\fP
Copy the pattern space to the standard output.
Replace the pattern space with the next line of input.
.TP
(2)\|\f3N\fP
Append the next line of input to the pattern space
with an embedded new-line.
(The current line number changes.)
.TP
(2)\|\f3p\fP
Print.
Copy the pattern space to the standard output.
.TP
(2)\|\f3P\fP
Copy the initial segment of the pattern space through
the first new-line to the standard output.
.TP
(1)\|\f3q\fP
Quit.
Branch to the end of the script.
Do not start a new cycle.
.TP
.RI (2)\|\f3r\fP " rfile\^"
Read the contents of
.IR rfile .
Place them on the output before reading
the next input line.
.TP
.RI (2)\|\f3s\fP/ "regular expression" / replacement / flags\^
Substitute the
.I replacement\^
string for instances of the
.I regular expression\^
in the pattern space.
Any character may be used instead of
.BR / .
For a fuller description see
.IR ed (1).
.I Flags\^
is zero or more of:
.RS
.RS
.TP
.B g
Global.
Substitute for all nonoverlapping instances of the
.I regular expression\^
rather than just the
first one.
.TP
.B p
Print the pattern space if a replacement was made.
.TP
.BI w " wfile\^"
Write.
Append the pattern space to
.I wfile\^
if a replacement
was made.
.RE
.RE
.TP
.RI (2)\|\f3t\fP " label\^"
Test.
Branch to the
.B :
command bearing the
.I label\^
if any
substitutions have been made since the most recent
reading of an input line or execution of a
.BR t .
If
.I label\^
is empty, branch to the end of the script.
.TP
.RI (2)\|\f3w\fP " wfile\^"
Write.
Append the pattern space to
.IR wfile .
.TP
(2)\|\f3x\fP
Exchange the contents of the pattern and hold spaces.
.TP
.RI (2)\|\f3y\fP/ string1 / string2 /\^
Transform.
Replace all occurrences of characters in
.I string1\^
with the corresponding character in
.IR string2 .
The lengths of
.I
string1
and
.I string2\^
must be equal.
.TP
.RI (2)\f3!\fP " function\^"
Don't.
Apply the
.I function\^
(or group, if
.I function\^
is
.BR {\| )
only to lines
.I not\^
selected by the address(es).
.TP
.RI (0)\|\f3:\fP " label\^"
This command does nothing; it bears a
.I label\^
for
.B b
and
.B t
commands to branch to.
.TP
(1)\|\f3=\fP
Place the current line number on the standard output as a line.
.TP
(2)\|\f3{\fP
Execute the following commands through a matching
.B }
only when the pattern space is selected.
.TP
(0)\|
An empty command is ignored.
.PD
.SH SEE ALSO
awk(1), ed(1), grep(1).
.br
.I "\s-1SED\s+1\-A Non-interactive Text Editor\^"
by L. E. McMahon.