mirror of https://github.com/mkerrisk/man-pages
734 lines
28 KiB
Groff
734 lines
28 KiB
Groff
|
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
|
||
|
.TH "SED" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
|
||
|
.\" sed
|
||
|
.SH NAME
|
||
|
sed \- stream editor
|
||
|
.SH SYNOPSIS
|
||
|
.LP
|
||
|
\fBsed\fP \fB[\fP\fB-n\fP\fB]\fP \fIscript\fP\fB[\fP\fIfile\fP\fB...\fP\fB]\fP\fB
|
||
|
.br
|
||
|
.sp
|
||
|
sed\fP \fB[\fP\fB-n\fP\fB][\fP\fB-e\fP \fIscript\fP\fB]\fP\fB...\fP\fB[\fP\fB-f\fP
|
||
|
\fIscript_file\fP\fB]\fP\fB...\fP\fB[\fP\fIfile\fP\fB...\fP\fB]\fP\fB
|
||
|
.br
|
||
|
\fP
|
||
|
.SH DESCRIPTION
|
||
|
.LP
|
||
|
The \fIsed\fP utility is a stream editor that shall read one or more
|
||
|
text files, make editing changes according to a script of
|
||
|
editing commands, and write the results to standard output. The script
|
||
|
shall be obtained from either the \fIscript\fP operand
|
||
|
string or a combination of the option-arguments from the \fB-e\fP
|
||
|
\fIscript\fP and \fB-f\fP \fIscript_file\fP options.
|
||
|
.SH OPTIONS
|
||
|
.LP
|
||
|
The \fIsed\fP utility shall conform to the Base Definitions volume
|
||
|
of IEEE\ Std\ 1003.1-2001, Section 12.2, Utility Syntax Guidelines,
|
||
|
except that the order of presentation of the
|
||
|
\fB-e\fP and \fB-f\fP options is significant.
|
||
|
.LP
|
||
|
The following options shall be supported:
|
||
|
.TP 7
|
||
|
\fB-e\ \fP \fIscript\fP
|
||
|
Add the editing commands specified by the \fIscript\fP option-argument
|
||
|
to the end of the script of editing commands. The
|
||
|
\fIscript\fP option-argument shall have the same properties as the
|
||
|
\fIscript\fP operand, described in the OPERANDS section.
|
||
|
.TP 7
|
||
|
\fB-f\ \fP \fIscript_file\fP
|
||
|
Add the editing commands in the file \fIscript_file\fP to the end
|
||
|
of the script.
|
||
|
.TP 7
|
||
|
\fB-n\fP
|
||
|
Suppress the default output (in which each line, after it is examined
|
||
|
for editing, is written to standard output). Only lines
|
||
|
explicitly selected for output are written.
|
||
|
.sp
|
||
|
.LP
|
||
|
Multiple \fB-e\fP and \fB-f\fP options may be specified. All commands
|
||
|
shall be added to the script in the order specified,
|
||
|
regardless of their origin.
|
||
|
.SH OPERANDS
|
||
|
.LP
|
||
|
The following operands shall be supported:
|
||
|
.TP 7
|
||
|
\fIfile\fP
|
||
|
A pathname of a file whose contents are read and edited. If multiple
|
||
|
\fIfile\fP operands are specified, the named files shall
|
||
|
be read in the order specified and the concatenation shall be edited.
|
||
|
If no \fIfile\fP operands are specified, the standard input
|
||
|
shall be used.
|
||
|
.TP 7
|
||
|
\fIscript\fP
|
||
|
A string to be used as the script of editing commands. The application
|
||
|
shall not present a \fIscript\fP that violates the
|
||
|
restrictions of a text file except that the final character need not
|
||
|
be a <newline>.
|
||
|
.sp
|
||
|
.SH STDIN
|
||
|
.LP
|
||
|
The standard input shall be used only if no \fIfile\fP operands are
|
||
|
specified. See the INPUT FILES section.
|
||
|
.SH INPUT FILES
|
||
|
.LP
|
||
|
The input files shall be text files. The \fIscript_file\fPs named
|
||
|
by the \fB-f\fP option shall consist of editing
|
||
|
commands.
|
||
|
.SH ENVIRONMENT VARIABLES
|
||
|
.LP
|
||
|
The following environment variables shall affect the execution of
|
||
|
\fIsed\fP:
|
||
|
.TP 7
|
||
|
\fILANG\fP
|
||
|
Provide a default value for the internationalization variables that
|
||
|
are unset or null. (See the Base Definitions volume of
|
||
|
IEEE\ Std\ 1003.1-2001, Section 8.2, Internationalization Variables
|
||
|
for
|
||
|
the precedence of internationalization variables used to determine
|
||
|
the values of locale categories.)
|
||
|
.TP 7
|
||
|
\fILC_ALL\fP
|
||
|
If set to a non-empty string value, override the values of all the
|
||
|
other internationalization variables.
|
||
|
.TP 7
|
||
|
\fILC_COLLATE\fP
|
||
|
.sp
|
||
|
Determine the locale for the behavior of ranges, equivalence classes,
|
||
|
and multi-character collating elements within regular
|
||
|
expressions.
|
||
|
.TP 7
|
||
|
\fILC_CTYPE\fP
|
||
|
Determine the locale for the interpretation of sequences of bytes
|
||
|
of text data as characters (for example, single-byte as
|
||
|
opposed to multi-byte characters in arguments and input files), and
|
||
|
the behavior of character classes within regular
|
||
|
expressions.
|
||
|
.TP 7
|
||
|
\fILC_MESSAGES\fP
|
||
|
Determine the locale that should be used to affect the format and
|
||
|
contents of diagnostic messages written to standard
|
||
|
error.
|
||
|
.TP 7
|
||
|
\fINLSPATH\fP
|
||
|
Determine the location of message catalogs for the processing of \fILC_MESSAGES
|
||
|
\&.\fP
|
||
|
.sp
|
||
|
.SH ASYNCHRONOUS EVENTS
|
||
|
.LP
|
||
|
Default.
|
||
|
.SH STDOUT
|
||
|
.LP
|
||
|
The input files shall be written to standard output, with the editing
|
||
|
commands specified in the script applied. If the \fB-n\fP
|
||
|
option is specified, only those input lines selected by the script
|
||
|
shall be written to standard output.
|
||
|
.SH STDERR
|
||
|
.LP
|
||
|
The standard error shall be used only for diagnostic messages.
|
||
|
.SH OUTPUT FILES
|
||
|
.LP
|
||
|
The output files shall be text files whose formats are dependent on
|
||
|
the editing commands given.
|
||
|
.SH EXTENDED DESCRIPTION
|
||
|
.LP
|
||
|
The \fIscript\fP shall consist of editing commands of the following
|
||
|
form:
|
||
|
.sp
|
||
|
.RS
|
||
|
.nf
|
||
|
|
||
|
\fB[\fP\fIaddress\fP\fB[\fP\fB,\fP\fIaddress\fP\fB]]\fP\fIfunction\fP
|
||
|
.fi
|
||
|
.RE
|
||
|
.LP
|
||
|
where \fIfunction\fP represents a single-character command verb from
|
||
|
the list in Editing Commands
|
||
|
in sed , followed by any applicable arguments.
|
||
|
.LP
|
||
|
The command can be preceded by <blank>s and/or semicolons. The function
|
||
|
can be preceded by <blank>s. These optional
|
||
|
characters shall have no effect.
|
||
|
.LP
|
||
|
In default operation, \fIsed\fP cyclically shall append a line of
|
||
|
input, less its terminating <newline>, into the pattern
|
||
|
space. Normally the pattern space will be empty, unless a \fBD\fP
|
||
|
command terminated the last cycle. The \fIsed\fP utility shall
|
||
|
then apply in sequence all commands whose addresses select that pattern
|
||
|
space, and at the end of the script copy the pattern space
|
||
|
to standard output (except when \fB-n\fP is specified) and delete
|
||
|
the pattern space. Whenever the pattern space is written to
|
||
|
standard output or a named file, \fIsed\fP shall immediately follow
|
||
|
it with a <newline>.
|
||
|
.LP
|
||
|
Some of the editing commands use a hold space to save all or part
|
||
|
of the pattern space for subsequent retrieval. The pattern and
|
||
|
hold spaces shall each be able to hold at least 8192 bytes.
|
||
|
.SS Addresses in sed
|
||
|
.LP
|
||
|
An address is either a decimal number that counts input lines cumulatively
|
||
|
across files, a \fB'$'\fP character that addresses
|
||
|
the last line of input, or a context address (which consists of a
|
||
|
BRE, as described in Regular
|
||
|
Expressions in sed , preceded and followed by a delimiter, usually
|
||
|
a slash).
|
||
|
.LP
|
||
|
An editing command with no addresses shall select every pattern space.
|
||
|
.LP
|
||
|
An editing command with one address shall select each pattern space
|
||
|
that matches the address.
|
||
|
.LP
|
||
|
An editing command with two addresses shall select 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 shall be selected.) Starting
|
||
|
at the first line following the selected range, \fIsed\fP shall
|
||
|
look again for the first address. Thereafter, the process shall be
|
||
|
repeated. Omitting either or both of the address components in
|
||
|
the following form produces undefined results:
|
||
|
.sp
|
||
|
.RS
|
||
|
.nf
|
||
|
|
||
|
\fB[\fP\fIaddress\fP\fB[\fP\fB,\fP\fIaddress\fP\fB]]\fP
|
||
|
.fi
|
||
|
.RE
|
||
|
.SS Regular Expressions in sed
|
||
|
.LP
|
||
|
The \fIsed\fP utility shall support the BREs described in the Base
|
||
|
Definitions volume of IEEE\ Std\ 1003.1-2001, Section 9.3, Basic Regular
|
||
|
Expressions, with the following additions:
|
||
|
.IP " *" 3
|
||
|
In a context address, the construction \fB"\\cBREc"\fP , where \fIc\fP
|
||
|
is any character other than backslash or
|
||
|
<newline>, shall be identical to \fB"/BRE/"\fP . If the character
|
||
|
designated by \fIc\fP appears following a backslash,
|
||
|
then it shall be considered to be that literal character, which shall
|
||
|
not terminate the BRE. For example, in the context address
|
||
|
\fB"\\xabc\\xdefx"\fP , the second \fIx\fP stands for itself, so that
|
||
|
the BRE is \fB"abcxdef"\fP .
|
||
|
.LP
|
||
|
.IP " *" 3
|
||
|
The escape sequence \fB'\\n'\fP shall match a <newline> embedded in
|
||
|
the pattern space. A literal <newline> shall
|
||
|
not be used in the BRE of a context address or in the substitute function.
|
||
|
.LP
|
||
|
.IP " *" 3
|
||
|
If an RE is empty (that is, no pattern is specified) \fIsed\fP shall
|
||
|
behave as if the last RE used in the last command applied
|
||
|
(either as an address or as part of a substitute command) was specified.
|
||
|
.LP
|
||
|
.SS Editing Commands in sed
|
||
|
.LP
|
||
|
In the following list of editing commands, the maximum number of permissible
|
||
|
addresses for each function is indicated by [
|
||
|
\fI0addr\fP], [ \fI1addr\fP], or [ \fI2addr\fP], representing zero,
|
||
|
one, or two addresses.
|
||
|
.LP
|
||
|
The argument \fItext\fP shall consist of one or more lines. Each embedded
|
||
|
<newline> in the text shall be preceded by a
|
||
|
backslash. Other backslashes in text shall be removed, and the following
|
||
|
character shall be treated literally.
|
||
|
.LP
|
||
|
The \fBr\fP and \fBw\fP command verbs, and the \fIw\fP flag to the
|
||
|
\fBs\fP command, take an optional \fIrfile\fP (or
|
||
|
\fIwfile\fP) parameter, separated from the command verb letter or
|
||
|
flag by one or more <blank>s; implementations may allow
|
||
|
zero separation as an extension.
|
||
|
.LP
|
||
|
The argument \fIrfile\fP or the argument \fIwfile\fP shall terminate
|
||
|
the editing command. Each \fIwfile\fP shall be created
|
||
|
before processing begins. Implementations shall support at least ten
|
||
|
\fIwfile\fP arguments in the script; the actual number
|
||
|
(greater than or equal to 10) that is supported by the implementation
|
||
|
is unspecified. The use of the \fIwfile\fP parameter shall
|
||
|
cause that file to be initially created, if it does not exist, or
|
||
|
shall replace the contents of an existing file.
|
||
|
.LP
|
||
|
The \fBb\fP, \fBr\fP, \fBs\fP, \fBt\fP, \fBw\fP, \fBy\fP, and \fB:\fP
|
||
|
command verbs shall accept additional arguments.
|
||
|
The following synopses indicate which arguments shall be separated
|
||
|
from the command verbs by a single <space>.
|
||
|
.LP
|
||
|
The \fBa\fP and \fBr\fP commands schedule text for later output. The
|
||
|
text specified for the \fBa\fP command, and the contents
|
||
|
of the file specified for the \fBr\fP command, shall be written to
|
||
|
standard output just before the next attempt to fetch a line of
|
||
|
input when executing the \fBN\fP or \fBn\fP commands, or when reaching
|
||
|
the end of the script. If written when reaching the end of
|
||
|
the script, and the \fB-n\fP option was not specified, the text shall
|
||
|
be written after copying the pattern space to standard
|
||
|
output. The contents of the file specified for the \fBr\fP command
|
||
|
shall be as of the time the output is written, not the time the
|
||
|
\fBr\fP command is applied. The text shall be output in the order
|
||
|
in which the \fBa\fP and \fBr\fP commands were applied to the
|
||
|
input.
|
||
|
.LP
|
||
|
Command verbs other than \fB{\fP, \fBa\fP, \fBb\fP, \fBc\fP, \fBi\fP,
|
||
|
\fBr\fP, \fBt\fP, \fBw\fP, \fB:\fP, and \fB#\fP
|
||
|
can be followed by a semicolon, optional <blank>s, and another command
|
||
|
verb. However, when the \fBs\fP command verb is used
|
||
|
with the \fIw\fP flag, following it with another command in this manner
|
||
|
produces undefined results.
|
||
|
.LP
|
||
|
A function can be preceded by one or more \fB'!'\fP characters, in
|
||
|
which case the function shall be applied if the addresses
|
||
|
do not select the pattern space. Zero or more <blank>s shall be accepted
|
||
|
before the first \fB'!'\fP character. It is
|
||
|
unspecified whether <blank>s can follow a \fB'!'\fP character, and
|
||
|
conforming applications shall not follow a \fB'!'\fP
|
||
|
character with <blank>s.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]\ {\fP\fIfunction\fP
|
||
|
.TP 7
|
||
|
\fIfunction\fP
|
||
|
.TP 7
|
||
|
\&...
|
||
|
.TP 7
|
||
|
\fB}\fP
|
||
|
Execute a list of \fIsed\fP functions only when the pattern space
|
||
|
is selected. The list of \fIsed\fP functions shall be
|
||
|
surrounded by braces and separated by <newline>s, and conform to the
|
||
|
following rules. The braces can be preceded or followed
|
||
|
by <blank>s. The functions can be preceded by <blank>s, but shall
|
||
|
not be followed by <blank>s. The
|
||
|
<right-brace> shall be preceded by a <newline> and can be preceded
|
||
|
or followed by <blank>s.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI1addr\fP\fB]a\\\fP
|
||
|
.TP 7
|
||
|
\fItext\fP
|
||
|
Write text to standard output as described previously.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]b\ [\fP\fIlabel\fP\fB]\fP
|
||
|
.sp
|
||
|
Branch to the \fB:\fP function bearing the \fIlabel\fP. If \fIlabel\fP
|
||
|
is not specified, branch to the end of the script. The
|
||
|
implementation shall support \fIlabel\fPs recognized as unique up
|
||
|
to at least 8 characters; the actual length (greater than or
|
||
|
equal to 8) that shall be supported by the implementation is unspecified.
|
||
|
It is unspecified whether exceeding a label length causes
|
||
|
an error or a silent truncation.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]c\\\fP
|
||
|
.TP 7
|
||
|
\fItext\fP
|
||
|
Delete the pattern space. With a 0 or 1 address or at the end of a
|
||
|
2-address range, place \fItext\fP on the output and start
|
||
|
the next cycle.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]d\fP
|
||
|
Delete the pattern space and start the next cycle.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]D\fP
|
||
|
Delete the initial segment of the pattern space through the first
|
||
|
<newline> and start the next cycle.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]g\fP
|
||
|
Replace the contents of the pattern space by the contents of the hold
|
||
|
space.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]G\fP
|
||
|
Append to the pattern space a <newline> followed by the contents of
|
||
|
the hold space.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]h\fP
|
||
|
Replace the contents of the hold space with the contents of the pattern
|
||
|
space.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]H\fP
|
||
|
Append to the hold space a <newline> followed by the contents of the
|
||
|
pattern space.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI1addr\fP\fB]i\\\fP
|
||
|
.TP 7
|
||
|
\fItext\fP
|
||
|
Write \fItext\fP to standard output.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]l\fP
|
||
|
(The letter ell.) Write the pattern space to standard output in a
|
||
|
visually unambiguous form. The characters listed in the Base
|
||
|
Definitions volume of IEEE\ Std\ 1003.1-2001, Table 5-1, Escape Sequences
|
||
|
and Associated Actions ( \fB'\\\\'\fP ,
|
||
|
\fB'\\a'\fP , \fB'\\b'\fP , \fB'\\f'\fP , \fB'\\r'\fP , \fB'\\t'\fP
|
||
|
, \fB'\\v'\fP ) shall be written as the
|
||
|
corresponding escape sequence; the \fB'\\n'\fP in that table is not
|
||
|
applicable. Non-printable characters not in that table shall
|
||
|
be written as one three-digit octal number (with a preceding backslash)
|
||
|
for each byte in the character (most significant byte
|
||
|
first). If the size of a byte on the system is greater than 9 bits,
|
||
|
the format used for non-printable characters is
|
||
|
implementation-defined.
|
||
|
.LP
|
||
|
Long lines shall be folded, with the point of folding indicated by
|
||
|
writing a backslash followed by a <newline>; the length
|
||
|
at which folding occurs is unspecified, but should be appropriate
|
||
|
for the output device. The end of each line shall be marked with
|
||
|
a \fB'$'\fP .
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]n\fP
|
||
|
Write the pattern space to standard output if the default output has
|
||
|
not been suppressed, and replace the pattern space with
|
||
|
the next line of input, less its terminating <newline>.
|
||
|
.LP
|
||
|
If no next line of input is available, the \fBn\fP command verb shall
|
||
|
branch to the end of the script and quit without starting
|
||
|
a new cycle.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]N\fP
|
||
|
Append the next line of input, less its terminating <newline>, to
|
||
|
the pattern space, using an embedded <newline> to
|
||
|
separate the appended material from the original material. Note that
|
||
|
the current line number changes.
|
||
|
.LP
|
||
|
If no next line of input is available, the \fBN\fP command verb shall
|
||
|
branch to the end of the script and quit without starting
|
||
|
a new cycle or copying the pattern space to standard output.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]p\fP
|
||
|
Write the pattern space to standard output.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]P\fP
|
||
|
Write the pattern space, up to the first <newline>, to standard output.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI1addr\fP\fB]q\fP
|
||
|
Branch to the end of the script and quit without starting a new cycle.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI1addr\fP\fB]r\ \fP \fIrfile\fP
|
||
|
Copy the contents of \fIrfile\fP to standard output as described previously.
|
||
|
If \fIrfile\fP does not exist or cannot be read,
|
||
|
it shall be treated as if it were an empty file, causing no error
|
||
|
condition.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]s/\fP\fIBRE\fP\fB/\fP\fIreplacement\fP\fB/\fP\fIflags\fP
|
||
|
.sp
|
||
|
Substitute the replacement string for instances of the BRE in the
|
||
|
pattern space. Any character other than backslash or
|
||
|
<newline> can be used instead of a slash to delimit the BRE and the
|
||
|
replacement. Within the BRE and the replacement, the BRE
|
||
|
delimiter itself can be used as a literal character if it is preceded
|
||
|
by a backslash.
|
||
|
.LP
|
||
|
The replacement string shall be scanned from beginning to end. An
|
||
|
ampersand ( \fB'&'\fP ) appearing in the replacement
|
||
|
shall be replaced by the string matching the BRE. The special meaning
|
||
|
of \fB'&'\fP in this context can be suppressed by
|
||
|
preceding it by a backslash. The characters \fB"\\\fP\fIn"\fP, where
|
||
|
\fIn\fP is a digit, shall be replaced by the text matched
|
||
|
by the corresponding backreference expression. The special meaning
|
||
|
of \fB"\\\fP\fIn"\fP where \fIn\fP is a digit in this
|
||
|
context, can be suppressed by preceding it by a backslash. For each
|
||
|
other backslash ( \fB'\\'\fP ) encountered, the following
|
||
|
character shall lose its special meaning (if any). The meaning of
|
||
|
a \fB'\\'\fP immediately followed by any character other than
|
||
|
\fB'&'\fP , \fB'\\'\fP , a digit, or the delimiter character used
|
||
|
for this command, is unspecified.
|
||
|
.LP
|
||
|
A line can be split by substituting a <newline> into it. The application
|
||
|
shall escape the <newline> in the
|
||
|
replacement by preceding it by a backslash. A substitution shall be
|
||
|
considered to have been performed even if the replacement
|
||
|
string is identical to the string that it replaces. Any backslash
|
||
|
used to alter the default meaning of a subsequent character shall
|
||
|
be discarded from the BRE or the replacement before evaluating the
|
||
|
BRE or using the replacement.
|
||
|
.LP
|
||
|
The value of \fIflags\fP shall be zero or more of:
|
||
|
.TP 7
|
||
|
\fIn\fP
|
||
|
.RS
|
||
|
Substitute for the \fIn\fPth occurrence only of the BRE found within
|
||
|
the pattern space.
|
||
|
.RE
|
||
|
.TP 7
|
||
|
\fBg\fP
|
||
|
.RS
|
||
|
Globally substitute for all non-overlapping instances of the BRE rather
|
||
|
than just the first one. If both \fBg\fP and \fIn\fP
|
||
|
are specified, the results are unspecified.
|
||
|
.RE
|
||
|
.TP 7
|
||
|
\fBp\fP
|
||
|
.RS
|
||
|
Write the pattern space to standard output if a replacement was made.
|
||
|
.RE
|
||
|
.TP 7
|
||
|
\fBw\ \fP \fIwfile\fP
|
||
|
.RS
|
||
|
Write. Append the pattern space to \fIwfile\fP if a replacement was
|
||
|
made. A conforming application shall precede the
|
||
|
\fIwfile\fP argument with one or more <blank>s. If the \fBw\fP flag
|
||
|
is not the last flag value given in a concatenation of
|
||
|
multiple flag values, the results are undefined.
|
||
|
.RE
|
||
|
.sp
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]t\ [\fP\fIlabel\fP\fB]\fP
|
||
|
.sp
|
||
|
Test. Branch to the \fB:\fP command verb bearing the \fIlabel\fP if
|
||
|
any substitutions have been made since the most recent
|
||
|
reading of an input line or execution of a \fBt\fP. If \fIlabel\fP
|
||
|
is not specified, branch to the end of the script.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]w\ \fP \fIwfile\fP
|
||
|
.sp
|
||
|
Append (write) the pattern space to \fIwfile\fP.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]x\fP
|
||
|
Exchange the contents of the pattern and hold spaces.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI2addr\fP\fB]y/\fP\fIstring1\fP\fB/\fP\fIstring2\fP\fB/\fP
|
||
|
.sp
|
||
|
Replace all occurrences of characters in \fIstring1\fP with the corresponding
|
||
|
characters in \fIstring2\fP. If a backslash
|
||
|
followed by an \fB'n'\fP appear in \fIstring1\fP or \fIstring2\fP,
|
||
|
the two characters shall be handled as a single
|
||
|
<newline>. If the number of characters in \fIstring1\fP and \fIstring2\fP
|
||
|
are not equal, or if any of the characters in
|
||
|
\fIstring1\fP appear more than once, the results are undefined. Any
|
||
|
character other than backslash or <newline> can be used
|
||
|
instead of slash to delimit the strings. If the delimiter is not \fIn\fP,
|
||
|
within \fIstring1\fP and \fIstring2\fP, the delimiter
|
||
|
itself can be used as a literal character if it is preceded by a backslash.
|
||
|
If a backslash character is immediately followed by a
|
||
|
backslash character in \fIstring1\fP or \fIstring2\fP, the two backslash
|
||
|
characters shall be counted as a single literal
|
||
|
backslash character. The meaning of a backslash followed by any character
|
||
|
that is not \fB'n'\fP , a backslash, or the delimiter
|
||
|
character is undefined.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI0addr\fP\fB]:\fP\fIlabel\fP
|
||
|
Do nothing. This command bears a \fIlabel\fP to which the \fBb\fP
|
||
|
and \fBt\fP commands branch.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI1addr\fP\fB]=\fP
|
||
|
Write the following to standard output:
|
||
|
.sp
|
||
|
.RS
|
||
|
.nf
|
||
|
|
||
|
\fB"%d\\n", <\fP\fIcurrent line number\fP\fB>
|
||
|
\fP
|
||
|
.fi
|
||
|
.RE
|
||
|
.TP 7
|
||
|
\fB[\fP\fI0addr\fP\fB]\fP
|
||
|
Ignore this empty command.
|
||
|
.TP 7
|
||
|
\fB[\fP\fI0addr\fP\fB]#\fP
|
||
|
Ignore the \fB'#'\fP and the remainder of the line (treat them as
|
||
|
a comment), with the single exception that if the first
|
||
|
two characters in the script are \fB"#n"\fP , the default output shall
|
||
|
be suppressed; this shall be the equivalent of specifying
|
||
|
\fB-n\fP on the command line.
|
||
|
.sp
|
||
|
.SH EXIT STATUS
|
||
|
.LP
|
||
|
The following exit values shall be returned:
|
||
|
.TP 7
|
||
|
\ 0
|
||
|
Successful completion.
|
||
|
.TP 7
|
||
|
>0
|
||
|
An error occurred.
|
||
|
.sp
|
||
|
.SH CONSEQUENCES OF ERRORS
|
||
|
.LP
|
||
|
Default.
|
||
|
.LP
|
||
|
\fIThe following sections are informative.\fP
|
||
|
.SH APPLICATION USAGE
|
||
|
.LP
|
||
|
Regular expressions match entire strings, not just individual lines,
|
||
|
but a <newline> is matched by \fB'\\n'\fP in a
|
||
|
\fIsed\fP RE; a <newline> is not allowed by the general definition
|
||
|
of regular expression in IEEE\ Std\ 1003.1-2001.
|
||
|
Also note that \fB'\\n'\fP cannot be used to match a <newline> at
|
||
|
the end of an arbitrary input line; <newline>s
|
||
|
appear in the pattern space as a result of the \fBN\fP editing command.
|
||
|
.SH EXAMPLES
|
||
|
.LP
|
||
|
This \fIsed\fP script simulates the BSD \fIcat\fP \fB-s\fP command,
|
||
|
squeezing excess
|
||
|
blank lines from standard input.
|
||
|
.sp
|
||
|
.RS
|
||
|
.nf
|
||
|
|
||
|
\fBsed -n '
|
||
|
# Write non-empty lines.
|
||
|
/./ {
|
||
|
p
|
||
|
d
|
||
|
}
|
||
|
# Write a single empty line, then look for more empty lines.
|
||
|
/^$/ p
|
||
|
# Get next line, discard the held <newline> (empty line),
|
||
|
# and look for more empty lines.
|
||
|
:Empty
|
||
|
/^$/ {
|
||
|
N
|
||
|
s/.//
|
||
|
b Empty
|
||
|
}
|
||
|
# Write the non-empty line before going back to search
|
||
|
# for the first in a set of empty lines.
|
||
|
p
|
||
|
'
|
||
|
\fP
|
||
|
.fi
|
||
|
.RE
|
||
|
.SH RATIONALE
|
||
|
.LP
|
||
|
This volume of IEEE\ Std\ 1003.1-2001 requires implementations to
|
||
|
support at least ten distinct \fIwfile\fPs, matching
|
||
|
historical practice on many implementations. Implementations are encouraged
|
||
|
to support more, but conforming applications should not
|
||
|
exceed this limit.
|
||
|
.LP
|
||
|
The exit status codes specified here are different from those in System
|
||
|
V. System V returns 2 for garbled \fIsed\fP commands,
|
||
|
but returns zero with its usage message or if the input file could
|
||
|
not be opened. The standard developers considered this to be a
|
||
|
bug.
|
||
|
.LP
|
||
|
The manner in which the \fBl\fP command writes non-printable characters
|
||
|
was changed to avoid the historical
|
||
|
backspace-overstrike method, and other requirements to achieve unambiguous
|
||
|
output were added. See the RATIONALE for \fIed\fP for details of the
|
||
|
format chosen, which is the same as that chosen for \fIsed\fP.
|
||
|
.LP
|
||
|
This volume of IEEE\ Std\ 1003.1-2001 requires implementations to
|
||
|
provide pattern and hold spaces of at least 8192
|
||
|
bytes, larger than the 4000 bytes spaces used by some historical implementations,
|
||
|
but less than the 20480 bytes limit used in an
|
||
|
early proposal. Implementations are encouraged to allocate dynamically
|
||
|
larger pattern and hold spaces as needed.
|
||
|
.LP
|
||
|
The requirements for acceptance of <blank>s and <space>s in command
|
||
|
lines has been made more explicit than in early
|
||
|
proposals to describe clearly the historical practice and to remove
|
||
|
confusion about the phrase "protect initial blanks
|
||
|
[\fIsic\fP] and tabs from the stripping that is done on every script
|
||
|
line" that appears in much of the historical documentation
|
||
|
of the \fIsed\fP utility description of text. (Not all implementations
|
||
|
are known to have stripped <blank>s from text lines,
|
||
|
although they all have allowed leading <blank>s preceding the address
|
||
|
on a command line.)
|
||
|
.LP
|
||
|
The treatment of \fB'#'\fP comments differs from the SVID which only
|
||
|
allows a comment as the first line of the script, but
|
||
|
matches BSD-derived implementations. The comment character is treated
|
||
|
as a command, and it has the same properties in terms of
|
||
|
being accepted with leading <blank>s; the BSD implementation has historically
|
||
|
supported this.
|
||
|
.LP
|
||
|
Early proposals required that a \fIscript_file\fP have at least one
|
||
|
non-comment line. Some historical implementations have
|
||
|
behaved in unexpected ways if this were not the case. The standard
|
||
|
developers considered that this was incorrect behavior and that
|
||
|
application developers should not have to avoid this feature. A correct
|
||
|
implementation of this volume of
|
||
|
IEEE\ Std\ 1003.1-2001 shall permit \fIscript_file\fPs that consist
|
||
|
only of comment lines.
|
||
|
.LP
|
||
|
Early proposals indicated that if \fB-e\fP and \fB-f\fP options were
|
||
|
intermixed, all \fB-e\fP options were processed before
|
||
|
any \fB-f\fP options. This has been changed to process them in the
|
||
|
order presented because it matches historical practice and is
|
||
|
more intuitive.
|
||
|
.LP
|
||
|
The treatment of the \fBp\fP flag to the \fBs\fP command differs between
|
||
|
System V and BSD-based systems when the default
|
||
|
output is suppressed. In the two examples:
|
||
|
.sp
|
||
|
.RS
|
||
|
.nf
|
||
|
|
||
|
\fBecho a | sed 's/a/A/p'
|
||
|
echo a | sed -n 's/a/A/p'
|
||
|
\fP
|
||
|
.fi
|
||
|
.RE
|
||
|
.LP
|
||
|
this volume of IEEE\ Std\ 1003.1-2001, BSD, System V documentation,
|
||
|
and the SVID indicate that the first example should
|
||
|
write two lines with \fBA\fP, whereas the second should write one.
|
||
|
Some System V systems write the \fBA\fP only once in both
|
||
|
examples because the \fBp\fP flag is ignored if the \fB-n\fP option
|
||
|
is not specified.
|
||
|
.LP
|
||
|
This is a case of a diametrical difference between systems that could
|
||
|
not be reconciled through the compromise of declaring the
|
||
|
behavior to be unspecified. The SVID/BSD/System V documentation behavior
|
||
|
was adopted for this volume of
|
||
|
IEEE\ Std\ 1003.1-2001 because:
|
||
|
.IP " *" 3
|
||
|
No known documentation for any historic system describes the interaction
|
||
|
between the \fBp\fP flag and the \fB-n\fP option.
|
||
|
.LP
|
||
|
.IP " *" 3
|
||
|
The selected behavior is more correct as there is no technical justification
|
||
|
for any interaction between the \fBp\fP flag and
|
||
|
the \fB-n\fP option. A relationship between \fB-n\fP and the \fBp\fP
|
||
|
flag might imply that they are only used together, but this
|
||
|
ignores valid scripts that interrupt the cyclical nature of the processing
|
||
|
through the use of the \fBD\fP, \fBd\fP, \fBq\fP, or
|
||
|
branching commands. Such scripts rely on the \fBp\fP suffix to write
|
||
|
the pattern space because they do not make use of the default
|
||
|
output at the "bottom" of the script.
|
||
|
.LP
|
||
|
.IP " *" 3
|
||
|
Because the \fB-n\fP option makes the \fBp\fP flag unnecessary, any
|
||
|
interaction would only be useful if \fIsed\fP scripts
|
||
|
were written to run both with and without the \fB-n\fP option. This
|
||
|
is believed to be unlikely. It is even more unlikely that
|
||
|
programmers have coded the \fBp\fP flag expecting it to be unnecessary.
|
||
|
Because the interaction was not documented, the likelihood
|
||
|
of a programmer discovering the interaction and depending on it is
|
||
|
further decreased.
|
||
|
.LP
|
||
|
.IP " *" 3
|
||
|
Finally, scripts that break under the specified behavior produce too
|
||
|
much output instead of too little, which is easier to
|
||
|
diagnose and correct.
|
||
|
.LP
|
||
|
.LP
|
||
|
The form of the substitute command that uses the \fBn\fP suffix was
|
||
|
limited to the first 512 matches in an early proposal. This
|
||
|
limit has been removed because there is no reason an editor processing
|
||
|
lines of {LINE_MAX} length should have this restriction. The
|
||
|
command \fBs/a/A/2047\fP should be able to substitute the 2047th occurrence
|
||
|
of \fBa\fP on a line.
|
||
|
.LP
|
||
|
The \fBb\fP, \fBt\fP, and \fB:\fP commands are documented to ignore
|
||
|
leading white space, but no mention is made of trailing
|
||
|
white space. Historical implementations of \fIsed\fP assigned different
|
||
|
locations to the labels \fB'x'\fP and
|
||
|
\fB"x\ "\fP . This is not useful, and leads to subtle programming
|
||
|
errors, but it is historical practice, and changing it
|
||
|
could theoretically break working scripts. Implementors are encouraged
|
||
|
to provide warning messages about labels that are never used
|
||
|
or jumps to labels that do not exist.
|
||
|
.LP
|
||
|
Historically, the \fIsed\fP \fB!\fP and \fB}\fP editing commands did
|
||
|
not permit multiple commands on a single line using a
|
||
|
semicolon as a command delimiter. Implementations are permitted, but
|
||
|
not required, to support this extension.
|
||
|
.SH FUTURE DIRECTIONS
|
||
|
.LP
|
||
|
None.
|
||
|
.SH SEE ALSO
|
||
|
.LP
|
||
|
\fIawk\fP , \fIed\fP , \fIgrep\fP
|
||
|
.SH COPYRIGHT
|
||
|
Portions of this text are reprinted and reproduced in electronic form
|
||
|
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
|
||
|
-- Portable Operating System Interface (POSIX), The Open Group Base
|
||
|
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
|
||
|
Electrical and Electronics Engineers, Inc and The Open Group. In the
|
||
|
event of any discrepancy between this version and the original IEEE and
|
||
|
The Open Group Standard, the original IEEE and The Open Group Standard
|
||
|
is the referee document. The original Standard can be obtained online at
|
||
|
http://www.opengroup.org/unix/online.html .
|