psed - a stream editor |
psed - a stream editor
psed [-an] script [file ...] psed [-an] [-e script] [-f script-file] [file ...]
s2p [-an] [-e script] [-f script-file]
A stream editor reads the input stream consisting of the specified files
(or standard input, if none are given), processes is line by line by
applying a script consisting of edit commands, and writes resulting lines
to standard output. The filename `-
' may be used to read standard input.
The edit script is composed from arguments of -e options and script-files, in the given order. A single script argument may be specified as the first parameter.
If this program is invoked with the name s2p, it will act as a sed-to-Perl translator. See sed Script Translation.
sed returns an exit code of 0 on success or >0 if an error occurred.
sed command syntax is defined as
[address[,address]][!]function[argument]
with whitespace being permitted before or after addresses, and between
the function character and the argument. The addresses and the
address inverter (!
) are used to restrict the application of a
command to the selected line(s)
of input.
Each command must be on a line of its own, except where noted in the synopses below.
The edit cycle performed on each input line consist of reading the line (without its trailing newline character) into the pattern space, applying the applicable commands of the edit script, writing the final contents of the pattern space and a newline to the standard output. A hold space is provided for saving the contents of the pattern space for later use.
A sed address is either a line number or a pattern, which may be combined arbitrarily to construct ranges. Lines are numbered across all input files.
Any address may be followed by an exclamation mark (`!
'), selecting
all lines not matching that address.
$
) is the line number of the last line of the input stream.
/
.
Any other character except \
or newline may be used to delimit a
pattern address when the initial delimiter is prefixed with a
backslash (`\
').
If no address is given, the command selects every line.
If one address is given, it selects the line (or lines) matching the address.
Two addresses select a range that begins whenever the first address matches, and ends (including that line) when the second address matches. If the first (second) address is a matching pattern, the second address is not applied to the very same line to determine the end of the range. Likewise, if the second address is a matching pattern, the first address is not applied to the very same line to determine the begin of another range. If both addresses are line numbers, and the second line number is less than the first line number, then only the first line is selected.
The maximum permitted number of addresses is indicated with each function synopsis below.
The argument text consists of one or more lines following the command. Embedded newlines in text must be preceded with a backslash. Other backslashes in text are deleted and the following character is taken literally.
\
' at the point of the split; the true end of a line is marked with
a `$
'. Escapes are: `\a', `\t', `\n', `\f', `\r', `\e' for
BEL, HT, LF, FF, CR, ESC, respectively, and `\' followed by a three-digit
octal number for all other non-printable characters.
\
').
Literal newlines may be embedded in the replacement string by preceding a newline with a backslash.
Within the replacement, an ampersand (`&
') is replaced by the string
matching the regular expression. The strings `\1
' through `\9
' are
replaced by the corresponding subpattern (see Basic Regular Expressions).
To get a literal `&
' or `\
' in the replacement text, precede it
by a backslash.
The following flags modify the behaviour of the s command:
address(es)
given on the
{ command (but may still have individual addresses).
#n
', automatic printing of output is
suppressed, as if the -n option were given on the command line.
A Basic Regular Expression (BRE), as defined in POSIX 1003.2, consists of atoms, for matching parts of a string, and bounds, specifying repetitions of a preceding atom.
The possible atoms of a BRE are: ., matching any single character; ^ and $, matching the null string at the beginning or end of a string, respectively; a bracket expressions, enclosed in [ and ] (see below); and any single character with no other significance (matching that character). A \ before one of: ., ^, $, [, *, \, matching the character after the backslash. A sequence of atoms enclosed in \( and \) becomes an atom and establishes the target for a backreference, consisting of the substring that actually matches the enclosed atoms. Finally, \ followed by one of the digits 0 through 9 is a backreference.
A ^ that is not first, or a $ that is not last does not have a special significance and need not be preceded by a backslash to become literal. The same is true for a ], that does not terminate a bracket expression.
An unescaped backslash cannot be last in a BRE.
The BRE bounds are: *, specifying 0 or more matches of the preceding atom; \{count\}, specifying that many repetitions; \{minimum,\}, giving a lower limit; and \{minimum,maximum\} finally defines a lower and upper bound.
A bound appearing as the first item in a BRE is taken literally.
A bracket expression is a list of characters, character ranges and character classes enclosed in [ and ] and matches any single character from the represented set of characters.
A character range is written as two characters separated by - and represents all characters (according to the character collating sequence) that are not less than the first and not greater than the second. (Ranges are very collating-sequence-dependent, and portable programs should avoid relying on them.)
A character class is one of the class names
alnum digit punct alpha graph space blank lower upper cntrl print xdigit
enclosed in [: and :] and represents the set of characters as defined in ctype(3).
If the first character after [ is ^, the sense of matching is inverted.
To include a literal `^
', place it anywhere else but first. To
include a literal ']
' place it first or immediately after an
initial ^. To include a literal `-
' make it the first (or
second after ^) or last character, or the second endpoint of
a range.
The special bracket expression constructs [[:<:]]
and [[:>:]]
match the null string at the beginning and end of a word respectively.
(Note that neither is identical to Perl's `\b' atom.)
Since some sed implementations provide additional regular expression atoms (not defined in POSIX 1003.2), psed is capable of translating the following backslash escapes:
[[:>:]]
.[[:<:]]
.[[:alnum:]_]
.[^[:alnum:]_]
.To enable this feature, the environment variable PSEDEXTBRE must be set
to a string containing the requested characters, e.g.:
PSEDEXTBRE='<>wW'
.
The environment variable PSEDEXTBRE
may be set to extend BREs.
See Additional Atoms.
address(es)
The basic material for the preceding section was generated by running the sed script
#no autoprint s/^.*Warn( *"\([^"]*\)".*$/\1/ t process b :process s/$!/%s/g s/$[_[:alnum:]]\{1,\}/%s/g s/\\\\/\\/g s/^/=item / p
on the program's own text, and piping the output into sort -u
.
If this program is invoked with the name s2p it will act as a sed-to-Perl translator. After option processing (all other arguments are ignored), a Perl program is printed on standard output, which will process the input stream (as read from all arguments) in the way defined by the sed script and the option setting used for the translation.
perl(1), re_format(7)
The l command will show escape characters (ESC) as `\e
', but
a vertical tab (VT) in octal.
Trailing spaces are truncated from labels in :, t and b commands.
The meaning of an empty regular expression (`//
'), as defined by sed,
is ``the last pattern used, at run time''. This deviates from the Perl
interpretation, which will re-use the ``last last successfully executed
regular expression''. Since keeping track of pattern usage would create
terribly cluttered code, and differences would only appear in obscure
context (where other sed implementations appear to deviate, too),
the Perl semantics was adopted. Note that common usage of this feature,
such as in /abc/s//xyz/
, will work as expected.
Collating elements (of bracket expressions in BREs) are not implemented.
This sed implementation conforms to the IEEE Std1003.2-1992 (``POSIX.2'') definition of sed, and is compatible with the OpenBSD implementation, except where otherwise noted (see BUGS).
This Perl implementation of sed was written by Wolfgang Laun, Wolfgang.Laun@alcatel.at.
This program is free and open software. You may use, modify, distribute, and sell this program (and any modified variants) in any way you wish, provided you do not restrict others from doing the same.
psed - a stream editor |