NAME
ed, red - Edits a file line by line
SYNOPSIS
ed [-p string] [-s] [file]
red [-p string] [-s] [file]
The ed command is a line editing program that works on one
file at a time by copying it into a temporary edit buffer
and making changes to that copy.
FLAGS
-p string
Sets the ed prompt to string. The default for string is
null (no prompt).
-s Suppresses character counts that the editor displays
with the e, E, r, and w subcommands, suppresses diagnos-
tic messages for the e, E, and q subcommands, and
suppresses the ! (exclamation point) prompt after a
!system_command. This also holds true if you invoke ed
with a filename.
Note that if you quit without writing the file, you do
not get an error message. (Generally, before ending the
program, q checks to determine whether the buffer was
written to a file since the last time it was changed.
If not, q displays the ? message.)
DESCRIPTION
The ed command does not alter the file itself until you use
the write subcommand. You can specify on the command line
the file you want to edit, or you can use ed subcommands to
read a file into the buffer. When ed reads a new file into
the buffer, the contents of that file replace the buffer's
previous contents, if any.
There is also a restricted version of ed, the red command,
for use with the restricted shell (rsh). With red, you can
edit only files that reside in the current directory, or in
the /tmp directory, and you cannot use the !system_command
subcommand (see SUBCOMMANDS).
The ed Modes
The ed program operates in one of two modes, command mode
and text mode. In command mode, ed recognizes and executes
subcommands. In text mode, ed adds text to the file buffer,
but does not recognize subcommands. You enter text mode by
using the a subcommand, c subcommand, or i subcommand. To
leave text mode, enter . (dot) alone at the beginning of a
line.
The e command is a supported alias for the ed command.
Subcommand Syntax
An ed subcommand consists of zero, one, or two addresses,
followed by a single-character subcommand, possibly followed
by arguments to that subcommand. These addresses specify
one or more lines in the buffer. Because every subcommand
has default addresses, you frequently do not need to specify
addresses.
Pattern Matching
The ed command supports a limited form of special pattern-
matching characters that you can use as regular expressions
(REs) to construct pattern strings. You can use these pat-
terns in addresses to specify lines and in some subcommands
to specify portions of a line.
For information about regular expressions (REs), see the
grep command.
Addressing
There are three types of ed addresses: line number
addresses, addresses relative to the current line, and pat-
tern addresses.
The current line is the point of reference in the buffer and
is represented by a . (dot). When you start the ed program,
the current line is the last line in the buffer. As you
edit a file, the current line changes to the last line
affected by a subcommand. The current line is the default
address for several ed commands. (See SUBCOMMANDS to find
out how each subcommand affects the current line.)
Subcommands for determining and changing the current line
are described in the section SUBCOMMANDS. The following are
guidelines for constructing addresses:
o . (dot) addresses the current line.
o $ addresses the last line of the buffer.
o n addresses the nth line of the buffer.
o 'x addresses the line marked with a lowercase letter,
x, by the k subcommand.
o /pattern/ addresses the next line that contains a
matching string. The search begins with the line after
the current line and stops when it finds a match for
the pattern. If necessary, the search moves to the end
of the buffer, wraps around to the beginning of the
buffer, and continues until it either finds a match or
returns to the current line.
o ?pattern? addresses the previous line that contains a
match for the pattern. The ?pattern? construct, like
/pattern/, can search the entire buffer, but it does so
in the opposite direction.
o An address followed by +number or -number specifies an
address plus or minus the indicated number of lines.
(The + (plus sign) is optional.)
o An address that begins with + or - specifies a line
relative to the current line. For example, -5 is the
equivalent of .-5 (five lines above the current line).
o An address that ends with - or + specifies the line
immediately before (-) or immediately after (+) the
addressed line. Used alone, the - character addresses
the line immediately before the current line. The +
character addresses the line immediately after the
current line; however, the + character is optional.
The + and - characters have a cumulative effect; for
example, the address -- addresses the line two lines
above the current line.
o For convenience, a , (comma) stands for the address
pair 1,$ (first line through last line) and a ; (semi-
colon) stands for the pair .,$ (current line through
last line).
Commands that do not accept addresses regard the presence of
an address as an error. Commands that do accept addresses
can use either given or default addresses. When given more
addresses than it accepts, a command uses the last (right-
most) ones.
In most cases, a , (comma) separates addresses (for example
2,8). A ; (semicolon) can also separate addresses. A ;
between addresses causes ed to set the current line to the
first address and then calculate the second address (for
example, to set the starting line for a search based on
guidelines 5 and 6). In a pair of addresses, the first must
be numerically smaller than the second.
The ed Limits
The following is a list of ed size limitations:
o 64 bytes per filename.
o 512 bytes per line.
o 256 bytes per global subcommand list.
o 128 kilobyte buffer size. (Note that the buffer not
only contains the original file, but also editing
information. Each line occupies one word in the
buffer.)
The maximum number of lines depends on the amount of memory
available to you. The maximum file size depends on the
amount of physical data storage (disk or tape drive) avail-
able or on the maximum number of lines permitted in user
memory.
SUBCOMMANDS
In most cases, only one ed subcommand can be entered on a
line. The exceptions to this rule are the p and l subcom-
mands, which can be added to any subcommand except e, E, f,
r, or w.
The e, E, f, r, and w subcommands accept filenames as argu-
ments. The ed command stores the last filename used with a
subcommand as a default filename. The next e, E, f, r, or w
given without a filename uses the default filename.
The ed command responds to an error condition with one of
two messages: ? (question mark) or ?file. When the ed com-
mand receives an INT signal, it displays a ? and returns to
command mode. When ed reads a file, it discards NULL char-
acters and all characters after the last newline character.
Unless otherwise noted, all subcommands work by default on
the current line; an address is optional. If you specify an
address, you do not type the brackets.
When used as an address, a . (dot) refers to the current
line. When a . (dot) is shown in the first position on an
otherwise blank line, it terminates text mode and returns to
command mode. text denotes user input in text mode. Note
that address need not be a number; it can be a regular
expression of the form /RE/,/RE/ or /RE/;/RE/.
[address]a
text
. The a (append) subcommand adds text to the buffer after
the addressed line. Enter a . (dot) to return to com-
mand mode. The a subcommand sets the current line to
the last inserted line, or, if no lines were inserted,
to the addressed line. Address 0 causes the a subcom-
mand to add text to the beginning of the buffer.
[address1,address2]c
text
. The c (change) subcommand deletes the addressed lines,
then replaces them with new input. Enter a . (dot) to
return to command mode. The c command sets the current
line to the last new line of input, or, if there were
none, to the first line that was not deleted.
[address1,address2]d
The d (delete) subcommand removes the addressed lines
from the buffer. The line after the last line deleted
becomes the current line. If the deleted lines were
originally at the end of the buffer, the new last line
becomes the current line.
e file
The e (edit) subcommand first deletes any contents from
the buffer, then loads another file into the buffer,
sets the current line to the last line of the buffer,
and displays the number of bytes read in to the buffer.
If the buffer was changed since its contents were last
saved (with the w subcommand), e displays ? before it
clears the buffer.
The e subcommand stores file as the default filename to
be used, if necessary, by subsequent e, E, r, or w sub-
commands. (See the f subcommand.)
When the ! (exclamation point) character replaces file,
e takes the rest of the line as a shell (sh) command and
reads the command output. The e subcommand does not
store the name of the shell command as a default
filename.
E file
The E (Edit) subcommand works like e, with one excep-
tion: E does not check for changes made to the buffer
since the last w subcommand.
f [file]
The f (file name) subcommand changes the default
filename (the stored name of the last file used) to
file, if file is given. If file is not given, the f
subcommand prints the default filename.
[address1,address2]g/pattern/subcommand_list
The g (global) subcommand first marks every line that
matches the pattern. Then, for each marked line, this
subcommand sets the current line to that line and exe-
cutes subcommand_list. Place a single subcommand, or
the first subcommand of a list, on the same line with
the g subcommand; place subsequent subcommands on
separate lines. Except for the last line, end each of
these lines with a \ (backslash).
The subcommand_list can include the a, i, and c subcom-
mands and their input. If the last command in
subcommand_list is normally the . (dot) that ends input
mode, the . (dot) is optional. If there is no
subcommand_list, ed displays the current line. The
subcommand_list cannot include the g, G, v, V, or ! sub-
commands.
The g subcommand is similar to the v subcommand, which
executes subcommand_list for every line that does not
contain a match for the pattern. Note that this subcom-
mand default to the entire file, not to the current
line.
[address1,address2]G/pattern/
The interactive G (Global) subcommand first marks every
line that matches the pattern, then displays the first
marked line, sets the current line to that line, and
waits for a subcommand. G accepts any but the following
ed subcommands: a, c, i, g, G, v, V, and !. After the
subcommand finishes, G displays the next marked line,
and so on. G takes a newline character as a null sub-
command. :& causes G to execute the previous subcommand
again, if there is one. Note that subcommands executed
within the G subcommand can address and change any lines
in the buffer. The G subcommand can be terminated by
pressing the Interrupt key sequence. Note that this
subcommand default to the entire file, not to the
current line.
h The h (help) subcommand displays a short message that
explains the reason for the most recent ? notification.
H The H (Help) subcommand causes ed to enter a mode in
which help messages (see the h command) are displayed
for all subsequent ? notifications. The H subcommand
toggles this mode, and is initially set to "off".
[address]i
text
. The i (insert) subcommand inserts text before the
addressed line and sets the current line to the last
inserted line. Enter . (dot) to return to command mode.
If no lines are inserted, i sets the current line to the
addressed line. This subcommand differs from the a sub-
command only in the placement of the input text.
Address 0 is not legal for this subcommand.
[address1,address1+1]j
The j (join) subcommand joins contiguous lines by remov-
ing the intervening newline characters. If given only
one address, j does nothing. (For splitting lines, see
the s subcommand.) Note that lines that exceed the line
length limit cannot be joined.
[address]kx
The k (mark) subcommand marks the addressed line with
name x, which must be a lowercase letter. The address
'x (single quotation mark before the marking character)
then addresses this line. The k subcommand does not
change the current line. Note that marks attached to
lines are deleted with the line.
[address1,address2]l
The l (list) subcommand displays the addressed lines.
The l subcommand wraps long lines and, unlike the p sub-
command, represents nonprinting characters as 3-digit
octal numbers with a \ (backslash) preceding each byte
in the character. The following characters, however,
are written as escape sequences:
\\ Backslash
\a Alert
\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
An l subcommand can be appended to any ed subcommand
except: e, E, f, q, Q, r, w, or !.
[address1,address2]maddress3
The m (move) subcommand repositions the addressed lines.
The first moved line follows the line addressed by a.
Address 0 for address3 causes m to move the addressed
lines to the beginning of the file. The line specified
by address3 cannot be one of the lines to be moved. The
m subcommand sets the current line to the last moved
line.
[address1,address2]n
The n (number) subcommand displays the addressed lines,
each preceded by its line number and a tab character
(displayed as spaces); the n subcommand leaves the
current line at the last line displayed. An n subcom-
mand can be appended to any ed subcommand except e, E,
f, r, or w.
[address1,address2]p
The p (print) subcommand displays the addressed lines
and sets the current line set to the last line
displayed. A p subcommand can be appended to any ed
subcommand except: e, E, f, r, or w. For example, the
subcommand dp deletes the current line and displays the
new current line.
P The P (Prompt) subcommand turns the ed prompt string *
or the string specified by the -p flag on or off. Ini-
tially, P is off.
q The q (quit) subcommand exits the ed program. Before
ending the program, q checks to determine whether the
buffer was written to a file since the last time it was
changed. If not, q displays the ? message. Note that
you do not get more than one prompt in a row; a second
consecutive q quits the ed program without displaying a
prompt.
Q The Q (Quit) subcommand exits the ed program without
checking for changes to the buffer since the last w sub-
command (compare with the q subcommand).
[address]r file
The r (read) subcommand reads a file into the buffer
after the addressed line; r does not delete the previous
contents of the buffer. When entered without file, r
reads the default file, if any, into the buffer (see e
and f subcommands). r does not change the default
filename. Address 0 causes r to read a file in at the
beginning of the buffer. After it reads a file success-
fully, r, displays the number of characters read into
the buffer and sets the current line to the last line
read.
If ! (exclamation point) replaces file in a r subcom-
mand, r takes the rest of the line as an OSF/1 shell
(sh) command whose output is to be read. The r subcom-
mand does not store the names of shell commands as
default filenames.
[address1,address2]s/pattern/replacement/flags
The s (substitute) subcommand searches each addressed
line for a string that matches the pattern and then
replaces the string with the specified replacement
string. Without a number n or the global indicator g, s
replaces only the first matching string on each
addressed line. With n, s replaces the nth occurrence
of pattern on the addressed line. With the g indicator,
s replaces every occurrence of the matching string on
each addressed line.
If s does not find a match for the pattern, it returns
the error message ?. Any character except a space or a
newline character can separate (delimit) the pattern and
replacement. The s subcommand sets the current line to
the last line changed.
An & (ampersand) in the replacement string is a special
symbol that has the same value as the pattern string.
So, for example, the subcommand s/out/&ing/ has the same
effect as the subcommand s/out/outing/ and replaces out
with outing on the current line. A backslash before the
ampersand (\&) removes this special meaning of & in
replacement.
The % (percent sign), when used by itself as replace-
ment, causes s to use the previous replacement again.
The % character does not have this special meaning if it
is part of a longer replacement or if it is preceded by
a \ (backslash).
Lines can be split by substituting newline characters
into them. In replacement, the sequence \<Return>
quotes the newline character (not displayed) and moves
the cursor to the nextline for the remainder of the
string.
The value of flags can be the following:
count
Substitutes for the countth occurrence only of the
regular expression that is found on each addressed
line.
g Substitutes globally for all nonoverlapping
instances of the regular expression, instead of just
substituting for the first instance.
l Displays the final line in which a substitution was
made in the format specified for the l subcommand.
n Displays the final line in which a substitution was
made in the format specified for the n subcommand.
p Displays the final line in which a substitution was
made in the format specified for the p subcommand.
[address1,address2]taddress3
The t (transfer) subcommand inserts a copy of the
addressed lines after address3. The t subcommand
accepts address 0 (for inserting lines at the beginning
of the buffer). The t subcommand sets the current line
to the last line copied.
u The u (undo) subcommand restores the buffer to the state
it was in before it was last modified by an ed subcom-
mand. The commands that u can undo are a, c, d, g, G,
i, j, m, r, s, t, u, v, and V.
[address1,address2]v/pattern/subcommand_list
The v subcommand executes the subcommands in
subcommand_list for each line that does not contain a
match for the pattern. The v subcommand is a complement
for the global subcommand g, which executes
subcommand_list for every line that does contain a match
for the pattern.
[address1,address2]V/pattern/
The V subcommand first marks every line that does not
match the pattern, then displays the first marked line,
sets the current line to that line, and waits for a sub-
command. The V subcommand complements the G subcommand,
which marks the lines that do match the pattern.
[address1,address2]w file
The w (write) subcommand copies the addressed lines from
the buffer to the file named in file. If the file does
not exist, the w subcommand creates it with permission
mode 666 (read and write permission for everyone),
unless the umask setting specifies another file creation
mode. (For information about file permissions, see
umask in sh and chmod.)
The w subcommand does not change the default filename
(unless file is the first filename used since you
started ed). If you do not provide a filename, ed uses
the default filename, if any (see the e, E, and f sub-
commands). The w subcommand does not change the current
line.
If ed successfully writes the file, it displays the
number of characters written. When ! (exclamation
point) replaces file, ed takes the rest of the line as a
shell (sh) command whose output is to be read; w does
not save shell command names as default filenames (same
effect as !).
The address 0 is not a legal address for the w subcom-
mand. Therefore, it is not possible to create an empty
file with ed.
[address]=
Without an address, the = (equal sign) subcommand
displays the current line number. With the address $, =
(equal sign) displays the number of the last line in the
buffer. The = subcommand does not change the current
line and cannot be included in a g or v subcommand list.
!system_command
The ! (exclamation point) subcommand allows system com-
mands to be run from within ed. Anything following ! on
an ed subcommand line is interpreted as a system com-
mand. Within the text of that command string, ed
replaces the unescaped character %with the current
filename, if there is one.
When used as the first character of a shell command
(after the ! that runs a subshell), ed replaces the !
character with the previous system command; for example,
the command !! repeats the previous system command. If
the command interpreter (the sh command) expands the
command string, ed echoes the expanded line. The ! sub-
command does not change the current line.
number
+number
-number
The ed editor interprets a number alone on a line as an
address and displays the addressed line. Addresses can
be absolute (line numbers or $) or relative to the
current line (+number or -number). Entering a newline
character (a blank line) is equivalent to +1p and is
useful for stepping forward through the buffer one line
at a time.
RELATED INFORMATION
Commands: chmod(1), edit(1), ex(1),
grep(1)/egrep(1)/fgrep(1), rsh(1), sed(1), sh(1), stty(1),
vi(1)/vedit(1)/view(1).
Functions: regexp(3).
"Creating and Editing Files with ed"" in the OSF/1 User's
Guide.
Acknowledgement and Disclaimer