....TM 78-1270-1 39199 39199-11
SED \(em A Non-interactive Text Editor
is a non-interactive context editor
designed to be especially useful in
To edit files too large for comfortable
To edit any size file when the sequence
of editing commands is too complicated to be comfortably
typed in interactive mode.
To perform multiple `global' editing functions
efficiently in one pass through the input.
This memorandum constitutes a manual for users of
is a non-interactive context editor designed to be especially useful in
To edit files too large for comfortable
To edit any size file when the sequence
of editing commands is too complicated to be comfortably
typed in interactive mode;
To perform multiple `global' editing functions
efficiently in one pass through the input.
Since only a few lines of the input reside in core
at one time, and no temporary files are used,
the effective size of file that can be edited is limited only
by the requirement that the input and output fit simultaneously
into available secondary storage.
Complicated editing scripts can be created separately and given
For complex edits, this saves considerable typing, and its
running from a command file is much more efficient than any interactive
editor known to the author, even if that editor
can be driven by a pre-written script.
The principal loss of functions compared to an interactive editor
are lack of relative addressing (because of the line-at-a-time
operation), and lack of immediate verification that a command has
is a lineal descendant of the UNIX editor,
Because of the differences between interactive and non-interactive
operation, considerable changes have been made between
will frequently be surprised (and probably chagrined),
without reading Sections 2 and 3 of this document.
The most striking family resemblance between the two
editors is in the class of patterns (`regular expressions') they
the code for matching patterns is copied almost
verbatim from the code for
and the description of regular expressions in Section 2
is copied almost verbatim from the UNIX Programmer's
Manual[1]. (Both code and description were written by Dennis
by default copies the standard input to the standard output,
perhaps performing one or more editing commands on each
line before writing it to the output.
This behavior may be modified by flags on the command line;
The general format of an editing command is:
[address1,address2][function][arguments]
One or both addresses may be omitted; the format of addresses is
Any number of blanks or tabs may separate the addresses
The function must be present; the available commands are discussed
The arguments may be required or optional, according to which function
is given; again, they are discussed in Section 3 under each individual
Tab characters and spaces at the beginning of lines are ignored.
Three flags are recognized on the command line:
not to copy all lines, but only those specified by
functions (see Section 3.3);
to take the next argument as an editing command;
to take the next argument as a file name;
the file should contain editing commands, one to a line.
1.2. Order of Application of Editing Commands
Before any editing is done (in fact, before any input file is
even opened), all the editing commands are compiled into
a form which will be moderately efficient during
the execution phase (when the commands are actually applied to
lines of the input file).
The commands are compiled in the order in which they are
encountered; this is generally the order in which they will
be attempted at execution time.
The commands are applied one at a time; the input to each command
is the output of all preceding commands.
The default linear order of application of editing commands can
be changed by the flow-of-control commands,
Even when the order of application is changed
by these commands, it is still true that the input line to any
command is the output of any previously applied command.
The range of pattern matches is called the pattern space.
Ordinarily, the pattern space is one line of the input text,
but more than one line can be read into the pattern space
Examples are scattered throughout the text.
Except where otherwise noted,
the examples all assume the following input text:
A stately pleasure dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
(In no case is the output of the
commands to be considered an improvement
will quit after copying the first two lines of the input.
A stately pleasure dome decree:
2. ADDRESSES: Selecting lines for editing
Lines in the input file(s) to which editing commands are
to be applied can be selected by addresses.
Addresses may be either line numbers or context addresses.
The application of a group of commands can be controlled by
one address (or address-pair) by grouping
the commands with curly braces (`{ }')(Sec. 3.6.).
2.1. Line-number Addresses
A line number is a decimal integer.
As each line is read from the input, a line-number counter
a line-number address matches (selects) the input
line which causes the internal counter to equal the
The counter runs cumulatively through multiple input files;
it is not reset when a new input file is opened.
As a special case, the character
matches the last line of the last input file.
A context address is a pattern (`regular expression') enclosed in slashes (`/').
The regular expressions recognized by
are constructed as follows:
An ordinary character (not one of those discussed below)
is a regular expression, and matches that character.
A circumflex `^' at the beginning of a regular expression
matches the null character at the beginning of a line.
A dollar-sign `$' at the end of a regular expression
matches the null character at the end of a line.
The characters `\en' match an imbedded newline character,
but not the newline at the end of the pattern space.
A period `.' matches any character except the terminal newline
A regular expression followed by an asterisk `*' matches any
number (including 0) of adjacent occurrences of the regular
A string of characters in square brackets `[ ]' matches any character
in the string, and no others.
If, however, the first character of the string is circumflex `^',
the regular expression matches any character
the characters in the string and the terminal newline of the pattern space.
A concatenation of regular expressions is a regular expression
which matches the concatenation of strings matched by the
components of the regular expression.
A regular expression between the sequences `\e(' and `\e)' is
identical in effect to the unadorned regular expression, but has
side-effects which are described under the
command below and specification 10) immediately below.
means the same string of characters matched
by an expression enclosed in `\e(' and `\e)'
earlier in the same pattern.
the string specified is that beginning with the
occurrence of `\e(' counting from the left.
For example, the expression
`^\e(.*\e)\e1' matches a line beginning with
two repeated occurrences of the same string.
The null regular expression standing alone (e.g., `//') is
equivalent to the last regular expression compiled.
To use one of the special characters (^ $ . * [ ] \e /) as a literal
(to match an occurrence of itself in the input), precede the
special character by a backslash `\e'.
For a context address to `match' the input requires that
the whole pattern within the address match some
portion of the pattern space.
The commands in the next section can have 0, 1, or 2 addresses.
Under each command the maximum number of allowed addresses is
For a command to have more addresses than the maximum allowed
If a command has no addresses, it is applied to every line
If a command has one address, it is applied to all
lines which match that address.
If a command has two addresses, it is applied to the first
line which matches the first address, and to all subsequent lines
until (and including) the first subsequent line which matches
Then an attempt is made on subsequent lines to again match the first
address, and the process is repeated.
Two addresses are separated by a comma.
/an/ matches lines 1, 3, 4 in our sample text
/r*an/ matches lines 1,3, 4 (number = zero!)
/\e(an\e).*\e1/ matches line 1
All functions are named by a single character.
In the following summary, the maximum number of allowable addresses
is given enclosed in parentheses, then the single character
function name, possible arguments enclosed in angles (< >),
an expanded English translation of the single-character name,
and finally a description of what each function does.
The angles around the arguments are
part of the argument, and should not be typed
in actual editing commands.
3.1. Whole-line Oriented Functions
function deletes from the file (does not write to the output)
all those lines matched by its address(es).
It also has the side effect that no further commands are attempted
on the corpse of a deleted line;
function is executed, a new line is read from the input, and
the list of editing commands is re-started from the beginning
function reads the next line from the input, replacing
The current line is written to the output if it should
The list of editing commands is continued
function causes the argument <text> to be written to the
output after the line matched by its address.
command is inherently multi-line;
must appear at the end of a line, and <text> may contain
To preserve the one-command-to-a-line fiction,
the interior newlines must be hidden by a
backslash character (`\e') immediately preceding the
The <text> argument is terminated by the first unhidden
newline (the first one not immediately preceded
function is successfully executed, <text> will be
written to the output regardless of what later commands do to
the line which triggered it.
The triggering line may be
deleted entirely; <text> will still be written to the output.
The <text> is not scanned for address matches, and no editing
commands are attempted on it.
It does not cause any change in the line-number counter.
function behaves identically to the
function, except that <text> is written to the output
All other comments about the
function deletes the lines selected by its address(es),
and replaces them with the lines in <text>.
must be followed by a newline hidden by a backslash;
and interior new lines in <text> must be hidden by
command may have two addresses, and therefore select a range
If it does, all the lines in the range are deleted, but only
one copy of <text> is written to the output,
one copy per line deleted.
<text> is not scanned for address matches, and no
editing commands are attempted on it.
It does not change the line-number counter.
After a line has been deleted by a
function, no further commands are attempted on the corpse.
If text is appended after a line by
functions, and the line is subsequently changed, the text
function is described in Section 3.4.)
Within the text put in the output by these functions,
leading blanks and tabs will disappear, as always in
To get leading blanks and tabs into the output, precede the first
desired blank or tab by a backslash; the backslash will not
The list of editing commands:
applied to our standard input, produces:
In Xanadu did Kubhla Khan
Where Alph, the sacred river, ran
the same effect would be produced by either
of the two following command lists:
One very important function changes parts of lines selected by
a context search within the line.
(2)s<pattern><replacement><flags> -- substitute
of a line (selected by <pattern>) with <replacement>.
Substitute for <pattern>, <replacement>
The <pattern> argument contains a pattern,
exactly like the patterns in addresses (see 2.2 above).
The only difference between <pattern> and a context address is
that the context address must be delimited by slash (`/') characters;
<pattern> may be delimited by any character other than space or
By default, only the first string matched by <pattern> is replaced,
<replacement> argument begins immediately after the
second delimiting character of <pattern>, and must be followed
immediately by another instance of the delimiting character.
instances of the delimiting character.)
The <replacement> is not a pattern,
and the characters which are special in patterns
do not have special meaning in <replacement>.
Instead, other characters are special:
& is replaced by the string matched by <pattern>
is a single digit) is replaced by the \fId\fRth substring
matched by parts of <pattern> enclosed in `\e(' and `\e)'.
If nested substrings occur in <pattern>, the \fId\fRth
is determined by counting opening delimiters (`\e(').
As in patterns, special characters may be made
literal by preceding them with backslash (`\e').
The <flags> argument may contain the following flags:
g -- substitute <replacement> for all (non-overlapping)
instances of <pattern> in the line.
After a successful substitution, the scan for the next
instance of <pattern> begins just after the end of the
inserted characters; characters put into the line from
<replacement> are not rescanned.
p -- print the line if a successful replacement was done.
flag causes the line to be written to the output if and only
if a substitution was actually made by the
functions, each followed by a
flag, successfully substitute in the same input line,
multiple copies of the line will be written to the
output: one for each successful substitution.
w <filename> -- write the line to a file if a successful
flag causes lines which are actually substituted by the
function to be written to a file named by <filename>.
If <filename> exists before
is run, it is overwritten;
A single space must separate
The possibilities of multiple, somewhat different copies of
one input line being written are the same as for
A maximum of 10 different file names may be mentioned after
functions (see below), combined.
The following command, applied to our standard input,
produces, on the standard output:
In Xanadu did Kubhla Khan
A stately pleasure dome decree:
Where Alph, the sacred river, ran
Through caverns measureless by man
and, on the file `changes':
Through caverns measureless by man
If the nocopy option is in effect, the command:
A stately pleasure dome decree*P:*
Where Alph*P,* the sacred river*P,* ran
Down to a sunless sea*P.*
Finally, to illustrate the effect of the
produces (assuming nocopy mode):
In XANadu did Kubhla Khan
In XANadu did Kubhla KhAN
3.3. Input-output Functions
The print function writes the addressed lines to the standard output file.
They are written at the time the
function is encountered, regardless of what succeeding
editing commands may do to the lines.
(2)w <filename> -- write on <filename>
The write function writes the addressed lines to the file named
If the file previously existed, it is overwritten; if not, it is created.
The lines are written exactly as they exist when the write function
is encountered for each line, regardless of what subsequent
editing commands may do to them.
Exactly one space must separate the
A maximum of ten different files may be mentioned in write
(1)r <filename> -- read the contents of a file
The read function reads the contents of <filename>, and appends
them after the line matched by the address.
The file is read and appended regardless of what subsequent
editing commands do to the line which matched its address.
functions are executed on the same line,
functions is written to the output in the order that
the functions are executed.
Exactly one space must separate the
function cannot be opened, it is considered a null file,
not an error, and no diagnostic is given.
Since there is a limit to the number of files that can be opened
simultaneously, care should be taken that no more than ten
functions or flags; that number is reduced by one if any
(Only one read file is open at one time.)
Assume that the file `note1'
has the following contents:
Note: Kubla Khan (more properly Kublai Khan; 1216-1294)
was the grandson and most eminent successor of Genghiz
(Chingiz) Khan, and founder of the Mongol dynasty in China.
Then the following command:
Note: Kubla Khan (more properly Kublai Khan; 1216-1294)
was the grandson and most eminent successor of Genghiz
(Chingiz) Khan, and founder of the Mongol dynasty in China.
A stately pleasure dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Multiple Input-line Functions
Three functions, all spelled with capital letters, deal
specially with pattern spaces containing imbedded newlines;
they are intended principally to provide pattern matches across
The next input line is appended to the current line in the
pattern space; the two input lines are separated by an imbedded
Pattern matches may extend across the imbedded newline(s).
(2)D -- Delete first part of the pattern space
Delete up to and including the first newline character
in the current pattern space.
If the pattern space becomes empty (the only newline
was the terminal newline),
read another line from the input.
In any case, begin the list of editing commands again
(2)P -- Print first part of the pattern space
Print up to and including the first newline in the pattern space.
functions are equivalent to their lower-case counterparts
if there are no imbedded newlines in the pattern space.
3.5. Hold and Get Functions
Four functions save and retrieve part of the input for possible later
(2)h -- hold pattern space
functions copies the contents of the pattern space
into a hold area (destroying the previous contents of the
(2)H -- Hold pattern space
function appends the contents of the pattern space
to the contents of the hold area; the former and new contents
are separated by a newline.
(2)g -- get contents of hold area
function copies the contents of the hold area into
the pattern space (destroying the previous contents of the
(2)G -- Get contents of hold area
function appends the contents of the hold area to the
contents of the pattern space; the former and new contents are separated by
The exchange command interchanges the contents
of the pattern space and the hold area.
applied to our standard example, produce:
In Xanadu did Kubla Khan :In Xanadu
A stately pleasure dome decree: :In Xanadu
Where Alph, the sacred river, ran :In Xanadu
Through caverns measureless to man :In Xanadu
Down to a sunless sea. :In Xanadu
3.6. Flow-of-Control Functions
These functions do no editing on the input
lines, but control the application of functions
to the lines selected by the address part.
command causes the next command
(written on the same line), to be applied to all and only those input lines
selected by the adress part.
The grouping command `{' causes the
next set of commands to be applied
(or not applied) as a block to the
input lines selected by the addresses
The first of the commands under control of the grouping
may appear on the same line as the
The group of commands is terminated by a
matching `}' standing on a line by itself.
(0):<label> -- place a label
The label function marks a place in the list
of editing commands which may be referred to by
The <label> may be any sequence of eight or fewer characters;
if two different colon functions have identical labels,
a compile time diagnostic will be generated, and
(2)b<label> -- branch to label
The branch function causes the sequence of editing commands being
applied to the current input line to be restarted immediately
after the place where a colon function with the same <label>
If no colon function with the same label can be found after
all the editing commands have been compiled, a compile time diagnostic
is produced, and no execution is attempted.
function with no <label> is taken to be a branch to the end of the
list of editing commands;
whatever should be done with the current input line is done, and
another input line is read; the list of editing commands is restarted from the
beginning on the new line.
(2)t<label> -- test substitutions
successful substitutions have been made on the current input
if so, it branches to <label>;
The flag which indicates that a successful substitution has
been executed is reset by:
1) reading a new input line, or
3.7. Miscellaneous Functions
The = function writes to the standard output the line number of the
line matched by its address.
function causes the current line to be written to the
output (if it should be), any appended or read text to be written, and
execution to be terminated.
Ken Thompson and Dennis M. Ritchie,
The UNIX Programmer's Manual.