HPUX ed[1]

ed(1) ed(1)
NAME
ed, red - text editor
SYNOPSIS
ed [-s | -] [-p string] [-x] [file]
red [-s | -] [-p string] [-x] [file]
DESCRIPTION
ed is a line-oriented text editor. It is most commonly used in
scripts and non-interactive editing applications because, even though
it can be used interactively, other editors such as vi and ex are
typically easier to use in an interactive environment.
If file is specified, ed simulates an e command (see below) on the
named file; that is to say, the file is read into ed's buffer so that
it can be edited.
Options
The following options are recognized:
-s Suppress printing of byte counts by e, E, r, and w
commands, and suppress the ! prompt after a !
command.
- Same as -s option. The - option is obsolescent and
will be removed in a future release.
-p string Use string as the prompt string when in command mode.
By default, there is no prompt string.
-x Simulate an x command first to handle an encrypted
file.
File Handling
ed operates on a copy of the file it is editing; changes made to the
copy have no effect on the original file until a w (write) command is
given. The copy of the text being edited resides in a temporary file
called the buffer. There is only one buffer.
red is a restricted version of ed that only allows editing of files in
the current directory and prohibits executing shell commands via
!shell command. Attempts to bypass these restrictions result in a
restricted shell error message.
Both ed and red support the fspec(4) formatting capability. After
including a format specification as the first line of file and
invoking ed with the controlling terminal in stty -tabs or stty tab3
mode (see stty(1)), the specified tab stops are automatically used
when scanning file. For example, if the first line of a file
contained:
Hewlett-Packard Company - 1 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
<:t5,10,15 s72:>
tab stops would be set at columns 5, 10, and 15, and a maximum line
length of 72 would be imposed. NOTE: When inputting text, ed expands
tab characters as they are typed to every eighth column as a default.
Editor Commands Structure
Commands to ed have a simple and regular structure: zero, one, or two
addresses followed by a single-character command, possibly followed by
parameters to that command. These addresses specify one or more lines
in the buffer. Every command that requires addresses has default
addresses, so that the addresses can very often be omitted.
In general, only one command is allowed on a line. Append, change,
and insert commands accept text input which is then placed in the
buffer as appropriate. While ed is accepting text following an
append, change, or insert command, it is said to be in input mode.
While in input mode, no editor commands are recognized; all input is
merely collected. To terminate input mode, type a period (.) alone at
the beginning of a line.
Regular Expressions
ed supports the Basic Regular Expression (RE) syntax (see regexp(5))
with the following additions:
o The null RE (e.g., //) is equivalent to the last RE
encountered.
o If the closing delimiter of a RE or of a replacement string
(e.g., /) would be the last character before a new-line, that
delimiter can be omitted, in which case the addressed line is
printed. The following pairs of commands are equivalent:
s/s1/s2 g/s1 ?s1
s/s1/s2/p g/s1/p ?s1?
Line Addresses
To understand line addressing, remember that ed maintains a pointer to
the default current line. Generally speaking, the current line is the
last line affected by a command. The exact effect of a given command
on the current line is discussed under the description of each
command. Addresses are interpreted according to the following rules:
1. The character . refers to the current line.
2. The character $ refers to the last line of the buffer.
3. A decimal number n refers to the n-th line of the buffer.
Hewlett-Packard Company - 2 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
4. A 'x refers to the line marked with the mark name character
x, which must be a lowercase letter. Lines are marked with
the k command described below.
5. A RE enclosed by slashes (/RE/) refers to the first line
found by searching forward from the line following the
current line toward the end of the buffer and stopping at the
first line containing a string matching the RE. If
necessary, the search wraps around to the beginning of the
buffer and continues up to and including the current line, so
that the entire buffer is searched. (Also see WARNINGS
below.)
6. A RE enclosed in question marks (?RE?) addresses the first
line found by searching backward from the line preceding the
current line toward the beginning of the buffer and stopping
at the first line containing a string matching the RE. If
necessary, the search wraps around to the end of the buffer
and continues up to and including the current line. (Also
see WARNINGS below.)
7. An address followed by a plus (+) or minus (-) sign followed
by a decimal number specifies that address plus or minus the
indicated number of lines. The plus sign can be omitted.
8. If an address begins with + or -, the addition or subtraction
is calculated with respect to the current line. For example,
-5 is interpreted as .-5.
9. If an address ends with + or -, 1 is added to or subtracted
from the address, respectively. As a consequence of this and
rule 8 above, the address - refers to the line preceding the
current line. (To maintain compatibility with earlier
versions of the editor, the circumflex (^) and - characters
are interpreted identically when encountered in addresses.)
Moreover, multiple trailing + and - characters have a
cumulative effect, so -- refers to the second line preceding
the current line.
10. For convenience, a comma (,) represents the address pair 1,$,
while a semicolon (;) represents the pair .,$.
Commands require zero, one, or two addresses. Commands that do not
use addresses treat the presence of an address as an error. Commands
that accept one or two addresses assume default addresses when the
number of addresses specified is insufficient. If more addresses are
specified than a given command requires, the last one or two are used
as appropriate.
Addresses are usually separated from each other by a comma (,). They
can also be separated by a semicolon (;), in which case the current
Hewlett-Packard Company - 3 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
line (.) is set to the first address, after which the second address
is calculated. This feature can be used to determine the starting
line for forward and backward searches (see rules 5 and 6 above). The
second address of any two-address sequence must correspond to a line
in the buffer that follows the line corresponding to the first
address.
Editor Commands
In the following list of ed commands, the default addresses are shown
in parentheses (parentheses are not part of the address and should not
be placed in an actual command except for other purposes).
It is generally illegal for more than one command to appear on a line.
However, any command (except e, f, r, or w) can be suffixed by l, n,
or p in which case the current line is respectively either listed,
numbered, or printed, as discussed below under the l, n, and p
commands.
(.)a The a (append) command reads <text> and appends it
<text> after the addressed line. Upon completion, the new
. current line is the last inserted line, or, if no text
was added, at the addressed line. Address 0 is legal
for this command, causing the appended text to be
placed at the beginning of the buffer.
(.,.)c The c (change) command deletes the addressed lines then
<text> accepts input text to replace the deleted lines. Upon
. completion, the new current line is the last line in
text or, if no text was provided, at the first line
after the deleted line or lines.
(.,.)d The d (delete) command deletes the addressed lines from
the buffer. Upon completion, the new current line is
the first line following the deleted text, or the last
line in the file if the deleted line or lines were at
the end of the buffer.
e file The e (edit) command deletes the entire contents of the
buffer, then reads in the named file. Upon completion,
the new current line is the last line in the buffer.
If no file name is given, the currently-remembered file
name, if any, is used (see the f command). The number
of characters read is displayed, and file is remembered
for possible use as a default file name in subsequent
e, r, or w commands. If the file name starts with !,
the rest of the line is interpreted as a shell command
whose standard output is to be read. Such a shell
command is not remembered as the current file name.
(Also see DIAGNOSTICS below.)
E file The E (forced edit) command is identical to e except
that no check is made to ensure that the current buffer
Hewlett-Packard Company - 4 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
has not been altered since the last w command.
f file If file is specified, the f (file-name) command changes
the currently-remembered file name to file. Otherwise,
it prints the currently-remembered file name.
(1,$)g/RE/command list
The g (global) command first marks every line that
matches the given RE. Then, for every such line, the
given command list is executed with the current line
initially set to that line. A single command or the
first of a list of commands appears on the same line as
the global command. All lines of a multiple-line list
except the last line must end with a backslash (\). a,
i, and c commands and associated input are permitted.
The . that normally terminates input mode can be
omitted if it would be the last line of the command
list. An empty command list is equivalent to the p
command. The g, G, v, and V commands are not permitted
in the command list. (Also see WARNINGS below.)
(1,$)G/RE/ The interactive G (Global) command first marks every
line that matches the given RE. Then, for every such
line, the line is printed, then the current line is
changed to that line and any one command (other than
one of the a, c, i, g, G, v, and V commands) can be
input after which it is executed. After executing that
command, the next marked line is printed, and so on. A
new-line character acts as a null command, and an &
causes re-execution of the most recent command executed
within the current invocation of G. Note that the
commands input as part of the execution of the G
command may address and affect any lines in the buffer.
The G command can be terminated by an interrupt signal
(ASCII DEL or BREAK).
h The h (help) command gives a short error message
explaining the reason for the most recent ? diagnostic.
H The H (Help) command causes ed to enter a mode in which
error messages are printed for all subsequent ?
diagnostics. It also explains the previous ? if there
was one. The H command alternately turns this mode on
and off. Initial default is off.
(.)i The i (insert) command inserts the given text before
<text> the addressed line. Upon completion, the current line
. is the last inserted line, or, if there were none, the
addressed line. This command differs from the a
command only in the placement of the input text.
Address 0 is not legal for this command.
Hewlett-Packard Company - 5 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
(.,.+1)j The j (join) command joins contiguous lines by removing
the appropriate new-line characters. If exactly one
address is given, this command does nothing.
(.)kx The k (mark) command marks the addressed line with name
x which must be a lowercase letter. The address 'x
then addresses this line. Upon completion, the new
current line remains unchanged from before.
(.,.)l The l (list) command writes the addressed lines to
standard ouput in a visually unambiguous form.
Characters listed in the following table are written as
the corresponding escape sequence. Non-printable
characters not in the table are written as a three-
digit octal number (with a preceding backslash
character) for each byte in the character (most
significant byte first).
Long lines are folded with the point of folding
indicated by writing a backslash character followed by
a new-line. The end of each line is marked with a $.
An l (ell) command can be appended to any other command
other than e, E, f, q, Q, r, w, or !. The current line
number is set to the address of the last line written.
center tab(;); cB | cB | cB | cB cf4p+1 | a | cf4p+1 |
a. Escape Sequence;Represents;Escape
Sequence;Represents _ \\;backslash;\r;carriage-return
\a;alert;\t;horizontal tab \b;backspace;\v;vertical tab
\f;form-feed
(.,.)ma The m (move) command repositions the addressed line(s)
after the line addressed by a. Address 0 is legal for
a, causing the addressed line(s) to be moved to the
beginning of the file. It is an error if address a
falls within the range of moved lines; Upon completion,
the new current line is the last line moved.
(.,.)n The n (number) command prints the addressed lines,
preceding each line by its line number and a tab
character. Upon completion, the new current line is
the last line printed. The n command can be appended
to any other command other than e, f, r, or w.
(.,.)p The p (print) command prints the addressed lines. Upon
completion, the new current line is the last line
printed. The p command may be appended to any other
command other than e, E, f, q, Q, r, w, or !. For
example, dp deletes the current line and prints the new
current line.
Hewlett-Packard Company - 6 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
P The P (prompt) command causes ed to prompt with an
asterisk (*) (or string if -p option is specified in
the command line) for all subsequent commands. for all
subsequent commands. The P command alternately turns
this mode on and off; it is initially on if the -p
option is specified, otherwise off. The current line
number is unchanged.
q The q (quit) command causes ed to exit. No automatic
write of a file is done (but see DIAGNOSTICS below).
Q The editor unconditionally exits without checking for
changes in the buffer since the last w command.
($)r file The r (read) command reads the specified file into the
buffer after the addressed line. If no file name is
given, the currently-remembered file name, if any, is
used (see e and f commands). The currently-remembered
file name is not changed unless file is the very first
file name mentioned since ed was invoked. Address 0 is
legal for r and places the contents of file at the
beginning of the buffer. If the read is successful,
the number of characters read is displayed; Upon
completion, the new current line is the last line read
into the buffer. If the file name starts with !, the
rest of the line is interpreted as a shell command
whose standard output is to be read. For example, $r
!ls appends a listing of files in the current directory
to the end of the file being edited. A shell command
is not remembered as the current file name.
(.,.)s/RE/replacement/flags
The s (substitute) command searches each addressed line
for an occurrence of the specified RE. In each line in
which a match is found, all (non-overlapped) matched
strings are replaced by replacement if the global
replacement indicator g appears after the command. If
the global indicator does not appear, only the first
occurrence of the matched string is replaced. If a
number n appears after the command, only the nth
occurrence of the matched string on each addressed line
is replaced. It is an error for the substitution to
fail on all addressed lines. Any character other than
space or new-line can be used instead of / to delimit
the RE and replacement. Upon completion, the new
current line is the last line on which a substitution
occurred. (Also see WARNINGS below.)
If an ampersand (&) appears in replacement, it is
replaced by the string matching the RE on the current
line. The special meaning of & in this context can be
Hewlett-Packard Company - 7 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
suppressed by preceding it with \. As a more general
feature, the characters \n, where n is a digit, are
replaced by the text matched by the n-th regular
subexpression of the specified RE enclosed between \(
and \). When nested parenthesized subexpressions are
present, n is determined by counting occurrences of \(
starting from the left. When the character % is the
only character in replacement, the replacement used in
the most recent substitute command is used as the
replacement in the current substitute command. The %
loses its special meaning when it is in a replacement
string containing more than one character or when
preceded by a \.
A line can be split by substituting a new-line
character into it. The new-line in replacement must be
escaped by preceding it by \. Such substitution cannot
be done as part of a g or v command list.
The value of flags is zero or more of:
count Substitute for the count'th occurrence
only of the RE found on each addressed
line.
l Write to standard output the final line
in which a substitution was made. The
line is written in the format specified
for the l command.
n Write to standard output the final line
in which a substitution was made. The
line is written in the format specified
for the n command.
p Write to standard output the final line
in which a substitution was made. The
line is written in the format specified
for the p command.
(.,.)ta Same as m command, except that a copy of the addressed
lines is placed after address a (which can be 0). Upon
completion, the new current line is the last line of
the copy.
u The u (undo) command nullifies the effect of the most
recent command that modified anything in the buffer;
that is, the most recent a, c, d, g, i, j, m, r, s, t,
v, G, or V command. All changes made to the buffer by
a g, G, v, or V global command are "undone" as a single
change; if no changes were made by the global command
Hewlett-Packard Company - 8 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
(such as with g/RE/p), the u command has no effect.
The current line number is set to the value it had
immediately before the command started.
(1,$)v/RE/command
Same as the global command g except that lines marked
during the first step are those that do not match the
RE.
(1,$)V/RE/ This command is the same as the interactive global
command G except that the lines that are marked during
the first step are those that do not match the RE.
(1,$)wfile The w (write) command writes the addressed lines into
the named file. If the file does not exist, it is
created with mode 666 (readable and writable by
everyone), unless the current umask setting dictates
otherwise (see umask(1). The currently-remembered file
name is not changed unless file is the very first file
name encountered since ed was invoked. If no file name
is given, the currently-remembered file name, if any,
is used (see e and f commands); Upon completion, the
current line address is unchanged. If the command is
successful, the number of characters written is
displayed. If the file name starts with !, the rest of
the line is interpreted as a shell command whose
standard input is the addressed lines. Such a shell
command is not remembered as the current file name.
X A key string is demanded from the standard input.
Subsequent e, r, and w commands will encrypt and
decrypt the text with this key, using the algorithm of
crypt(1). An explicitly empty key turns off
encryption.
($)= The line number of the addressed line is displayed;
Current line address is unchanged by this command.
!shell command The remainder of the line after the ! is sent to the
shell (specified by the SHELL environment variable;
/bin/sh is used if SHELL is not set) to be interpreted
and executed as a command. Within the text of that
command, the unescaped character % is replaced with the
remembered file name. If a ! appears as the first
character of the shell command, it is replaced with the
text of the previous shell command. Thus, !! repeats
the last shell command. If any expansion is performed,
the expanded line is echoed. Upon completion, current
line address is unchanged.
Hewlett-Packard Company - 9 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
(.+1) <new-line>
An address alone on a line causes the addressed line to
be printed. A new-line alone is equivalent to .+1p.
This technique is useful for stepping forward through
the buffer.
If an interrupt signal (ASCII DEL or BREAK) is sent, ed prints a ? and
returns to its command level.
The following size limitations apply: 256 characters per global
command list, 64 characters per file name, and 32M characters in the
buffer. The limit on the number of lines depends on the amount of
user memory: each line takes 1 word.
EXTERNAL INFLUENCES
Environment Variables
SHELL determines the preferred command-line interpreter for use in all
!-style commands. If this variable is null or not set, sh is used
(see sh(1)).
LC_COLLATE determines the collating sequence used in evaluating
regular expressions.
LC_CTYPE determines the interpretation of text as single and/or
multi-byte characters, the classification of characters as non-
printing, and the characters matched by character class expressions in
regular expressions.
LANG determines the language in which messages are displayed.
If LC_COLLATE or LC_CTYPE is not specified in the environment or is
set to the empty string, the value of LANG is used as a default for
each unspecified or empty variable. If LANG is not specified or is
set to the empty string, a default of "C" (see lang(5)) is used
instead of LANG. If any internationalization variable contains an
invalid setting, ed behaves as if all internationalization variables
are set to "C". See environ(5).
When set, the TMPDIR environment variable specifies a directory to be
used for temporary files, overriding the default directory /tmp.
International Code Set Support
Single- and multi-byte character code sets are supported.
DIAGNOSTICS
? Command error. Use h or H for detailed explanations.
?file Inaccessible file. Use h or H for detailed
explanations.
Hewlett-Packard Company - 10 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
If changes have been made in the buffer since the last w command that
wrote the entire buffer, ed warns the user if an attempt is made to
destroy the buffer by means of an e or q command. ed displays ? then
continues normal editing unless a second e or q command is given, in
which case the second command is executed. The -s or - command-line
option inhibits this feature.
EXAMPLES
Make a simple substitution in file_1 from a shell script, changing the
first occurrence of abc in any line to xyz, and save the changes in
file_2.
cat - << EOF | ed -s file_1 1,$ s/abc/xyz/ w file_2 q EOF
FILES
/tmp/e# temporary buffer file where # is the process
number.
ed.hup work is saved here if the terminal is hung up.
SEE ALSO
awk(1), crypt(1), edit(1), ex(1), grep(1), sed(1), sh(1), stty(1),
vi(1), fspec(4), lang(5), regexp(5).
The ed Editor in Text Processing Users Guide .
WARNINGS
A ! command cannot be subject to a g or a v command.
The ! command and the ! escape from the e, r, and w commands cannot be
used if the the editor is invoked from a restricted shell (see sh(1)).
The sequence \n in a regular expression does not match a new-line
character.
The l command does not handle DEL correctly.
Files encrypted directly with the crypt command with the null key
cannot be edited (see crypt(1)).
If the editor input is coming from a command file (i.e., ed file <
ed-cmd-file), the editor exits at the first failure of a command in
the command file.
When reading a file, ed discards ASCII NUL characters and all
characters after the last new-line. This can cause unexpected
behavior when using regular expressions to search for character
sequences containing NUL characters or text near end of file.
STANDARDS CONFORMANCE
ed: SVID2, XPG2, XPG3, POSIX.2
Hewlett-Packard Company - 11 - HP-UX Release 9.0: August 1992
ed(1) ed(1)
red: SVID2, XPG2, XPG3
Hewlett-Packard Company - 12 - HP-UX Release 9.0: August 1992