4.4BSD/usr/src/old/sed/sed.1

Compare this file to the similar file:
Show the results in this format:

.\" Copyright (c) 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed by the University of
.\"	California, Berkeley and its contributors.
.\" 4. Neither the name of the University nor the names of its contributors
.\"    may be used to endorse or promote products derived from this software
.\"    without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\"     @(#)sed.1	6.7 (Berkeley) 7/25/92
.\"
.Dd July 25, 1992
.Dt SED 1
.Os
.Sh NAME
.Nm sed
.Nd stream editor.
.Sh SYNOPSIS
.Nm sed
.Op Fl n
.Ar script
.Ar
.Nm sed
.Op Fl n
.Op Fl e Ar script
.Op Fl f Ar script_file
.Ar
.Sh DESCRIPTION
The
.Nm sed
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
.Ar script_file .
.Pp
Options:
.Bl -tag -width indent
.It Fl e Ar script
The command line
.Ar script
is used to edit the input.
If multiple
.Fl e
options are given, the scripts are
applied in the order given to each line of the
input files.
If a
.Fl f
option is given in addition
to
.Fl e ,
lines are acted upon by scripts first.
.It Fl f Ar script_file
The file
.Ar script_file
is expected to contain editing commands, one per line,
and these commands are applied to the input.
If multiple
.Fl f
options are given, the commands in the
.Ar script_file Ns s
are applied in
the order given to each line of the input
files.
If a
.Fl e
option is given in addition to
.Fl f ,
lines are acted upon by
the commands in the
.At script_file Ns s
first.
.It Fl n
The
.Fl n
option suppresses the default output, which normally
passes each line, after it is examined for editing,
to standard output.
Therefore, only lines explicitly
selected for output are written.
.El
.Pp
The following operands are available:
.Bl -tag -width file
.It Ar file
A pathname of a file whose contents are read and
edited.
If multiple file operands are given,
the named files are read in the order given and
the concatenation is edited.
If no file operands
are given, the standard input is used.
.It Ar script
The script consists of one or more editing
instructions that are entered on the command line.
.El
.\" .Pp
.\" The following environment variable affects the execution of
.\" sed:
.\" .It Ev LC_CTYPE
.\" The locale for character classification.
.Pp
.Bd -filled -offset indent -compact
.Op address Op ,address
function
.Op arguments
.Ed
.Pp
In default operation,
.Nm sed
cyclically copies a line of input
into a pattern space (unless there is something left after a
.Cm D
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
under
.Fl n )
and deletes the pattern space.
.Pp
Some of the commands use a hold space to save all or part of
the pattern space for
subsequent retrieval.
.\" The pattern and hold spaces are each
.\" limited to
.\" .Pf { Dv SED_PATTERN_MAX Ns }
.\" bytes.
.Pp
An address is either no address; a decimal number that
counts input lines cumulatively across files; a
.Ql $
that
addresses the last line of input; a context address; or
regular expression.
.Pp
A command line with no addresses selects every pattern
space.
.Pp
A command line with one address selects each pattern space
that matches the address.
.Pp
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
second.
(If the second address is a number less than or
equal to the line number first selected, only one line is
selected.)
Starting at the first line following the
selected range,
.Nm sed
looks again for the first address.
Thereafter the process is repeated.
.Pp
Editing commands can be applied only to non-selected pattern
spaces by use of the negation function
.Cm \&!
(See below.)
.Pp
The
.Nm sed
utility uses basic regular expressions, as are found in the
editor
.Xr ed 1 ,
with the following additions:
.Pp
.Bl -enum -offset indent
.It
In a context address, the construction
.Li \e?RE? ,
where ?
is any character, is identical to
.Li /RE/ .
Note that in the
context address
.Li \exabc\exdefx ,
the second
.Ql x
stands for
itself, so that the regular expression is
.Li abcxdef .
.It
The escape sequence
.Ql \en
matches a <newline> embedded
in the pattern space.
.It
A period
.Ql \&.
matches any character except the terminal <newline> of the pattern space.
.El
.Pp
In the following list of functions the maximum number of
permissible addresses for each function is indicated by
.Op 0addr ,
.Op 1addr ,
or
.Op 2addr ;
representing zero, one, or two
addresses.
.Pp
The argument text consists of one or more lines.
Each
embedded <newline> in the text shall be preceded by a
backslash.
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.
.Pp
The
.Cm r
and
.Cm w
commands take an optional
.Ar rfile
(or
.Ar wfile )
parameter, separated from the command letter by zero or more
<blank>s.
.Pp
The argument
.Ar rfile
or the argument
.Ar wfile
shall terminate the
command line.
Each
.Ar wfile
is created before processing
begins.
There can be at most ten distinct
.Ar wfile
arguments
in the script.
.Pp
The
.Cm b , r , s , t ,
.Cm w , y , \&! ,
and
.Cm \&:
commands take additional
arguments.
The following synopses indicate which arguments
are separated from the commands by <blank>s.
.Bl -tag -width addrcommandxx
.It Xo
.Oo Ad 2addr Oc \&{ command_list \&}
.Xc
Executes command_list only when the pattern
space is selected.
The {} braces can be preceded
and followed by white space.
.It Xo
.Oo Ad 1addr Oc Ns Cm a Ar text
.Xc
Writes text to the standard output after the
pattern space is written.
.It Xo
.Oo Ad 2addr Oc Ns Cm b Ar label
.Xc
Branches to the
.Cm \&:
command bearing the label.
If label is empty, branch to the end of the
script.
.It Xo
.Oo Ad 2addr Oc Ns Cm c Ar text
.Xc
Deletes the pattern space.
With 0 or 1
address or at the end of a 2-address range,
places text on the output.
.It Xo
.Oo Ad 2addr Oc Ns Cm d
.Xc
Deletes the pattern space and starts the next
cycle.
.It Xo
.Oo Ad 2addr Oc Ns Cm D
.Xc
Deletes the initial segment of the pattern
space through the first <newline> and starts
the next cycle.
.It Xo
.Oo Ad 2addr Oc Ns Cm g
.Xc
Replaces the contents of the pattern space by
the contents of the hold space.
.It Xo
.Oo Ad 2addr Oc Ns Cm G
.Xc
Appends the contents of the hold space to the
pattern space.
.It Xo
.Oo Ad 2addr Oc Ns Cm h
.Xc
Replaces the contents of the hold space by
the contents of the pattern space.
.It Xo
.Oo Ad 2addr Oc Ns Cm H
.Xc
Appends the contents of the pattern space to
the hold space.
.It Xo
.Oo Ad 1addr Oc Ns Cm i Ar text
.Xc
Writes text to the standard output before the
pattern space is written.
.It Xo
.Oo Ad 2addr Oc Ns Cm l
.Xc
Lists the pattern space on the standard out-
put in an unambiguous form.
Nonprinting
characters are listed as hexadecimal digit
pairs, with a preceding backslash, with the
following exceptions:
.Bl -column <carriagexreturn> -offset indent
<alert>	\ea
<backslash>	\e\e
<backspace>	\eb
<carriage return>	\er
<form-feed>	\ef
<newline>	\en
<tab>	\et
<vertical tab>	\ev
.El
.Pp
Long lines are folded; the length at which
folding occurs is ungiven, but should be
appropriate for the output device.
.It Xo
.Oo Ad 2addr Oc Ns Cm n
.Xc
Copies the pattern space to the standard output
and replaces the pattern space with the
next line of input.
.It Xo
.Oo Ad 2addr Oc Ns Cm N
.Xc
Appends the next line of input to the pattern
space, using an embedded <newline> to
separate the appended material from the
original material.
Note that the current line
number changes.
.It Xo
.Oo Ad 2addr Oc Ns Cm p
.Xc
Copies
.Op prints
the pattern space to the
standard output.
.It Xo
.Oo Ad 2addr Oc Ns Cm P
.Xc
Copies
.Op prints
the pattern space, up to the
first <newline>, to the standard output.
.It Xo
.Oo Ad 1addr Oc Ns Cm q
.Xc
Branches to the end of the script and quits
without starting a new cycle.
.It Xo
.Oo Ad 1addr Oc Ns Cm r Ar rfile
.Xc
Read the contents of rfile.
Place them on the
output before reading the next input line.
.It Xo
.Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags
.Xc
Substitutes the replacement string for
instances of the regular expression in the
pattern space.
Any character can be used
instead of
.Ql / .
The value of flags is zero or
more of:
.Bl -tag -width Ds
.It Ar n
n=1-512.  Substitutes for the
.Ar n Ns th
occurrence only of the regular
expression found within the pattern space.
.It Cm g
Globally substitutes for all
non-overlapping instances of the regular
expression rather than just
the first one.  If both
.Cm g
and
.Cm n
are given,
.Cm g
takes precedence.
.It Cm p
Prints the pattern space if a
replacement was made.
.It Cm w Ar wfile
Write.  Appends the pattern space
to
.Ar wfile
if a replacement was
made.
.El
.It Xo
.Oo Ad 2addr Oc Ns Cm t Ar label
.Xc
Test.
Branches to the
.Cm \&:
command bearing the
label if any substitutions have been made
since the most recent reading of an input
line or execution of a
.Cm t .
If label is empty,
branches to the end of the script.
.It Xo
.Oo Ad 2addr Oc Ns Cm w Ar wfile
.Xc
Appends
.Op writes
the pattern space to
.Ar wfile .
.It Xo
.Oo Ad 2addr Oc Ns Cm x
.Xc
Exchanges the contents of the pattern and
hold spaces.
.It Xo
.Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/
.Xc
Replaces all occurrences of collating
elements in
.Ar string1
with the corresponding
collating element in
.Ar string2 .
The lengths of
.Ar string1
and
.Ar string2
shall be equal.
.It Xo
.Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction
.Xc
Applies the function (or group, if function
is {) only to the lines that are not selected
by the address(es).
.It Xo
.Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel
.Xc
This command does nothing; it bears a label
for the b and t commands to branch to.
.It Xo
.Oo Ad 1addr Oc Ns Cm \&=
.Xc
Places the current line number on the standard
output as a line with its own line
number.
.It Xo
.Oo Ad 0addr Oc
.Xc
An empty command is ignored.
.It Xo
.Oo Ad 0addr Oc Cm #
.Xc
If a
.Cm #
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
script file begins with
.Cm Ns #n ,
the default
output is suppressed.
.El
.Pp
The
.Nm sed
utility exits 0 on success, and >0 if an error occurs.
.Pp
If one or more of the input (not script) files cannot be
opened for reading,
.Nm sed
continues to process the remaining
files.
.Sh STANDARDS
The
.Nm sed
utility is expected to be
.St -p1003.2
compatible.