ed
PURPOSE
Edits text by line.
SYNOPSIS
ed [ - ] [ file ]
red [ - ] [ file ]
DESCRIPTION
The ed command is a line editing program that works on
only one file at a time by copying it into a temporary
file buffer and making changes to that copy. ed 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 the edit subcommands.
When ed reads a new file into the buffer, the contents of
that file replaces the buffer's previous contents, if
any.
There is also a restricted version of ed, the red
command, for use with the restricted shell (see "sh").
With red, you can edit only files that reside in the
current directory, or in the /tmp directory, and you
cannot use the !AIX-cmd subcommand (see page ).
An ed subcommand consists of zero, one, or two addresses,
followed by a single-character subcommand, possibly fol-
lowed by parameters 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.
The ed program operates in one of two modes, command mode
and text mode. In command mode, ed recognizes and exe-
cutes subcommands. In text mode, ed adds text to the
file buffer but does not recognize subcommands. To leave
text mode, enter a . (dot) alone at the beginning of a
line.
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 patterns in addresses to specify lines and in
some subcommands to specify portions of a line.
Regular Expressions (REs)
The following REs match a single character:
char An ordinary character (one other than one of
the special pattern-matching characters),
matches itself.
. A period (.) matches any single character
except for the new-line character.
[string] A string enclosed in square brackets ([ ])
matches any one character in that string.
Certain pattern-matching characters have
special meanings within square brackets:
^ If the first character of string is
a circumflex, then the RE
([^string]) matches any character
except the characters in string and
the new-line character. A ^ has
this special meaning only if it
occurs first in the string.
- You can use a minus (-) to indicate
a range of consecutive ASCII char-
acters according to the current
collating sequence. For example,
"[a-f]" might be equivalent to
"[abcdef]" or "[aAbBcCdDeEfF]" or
"[aa<a>bcdee<e>f]". The collating
sequence is defined by the environ-
ment variable NLCTAB or NLFILE.
See Managing the AIX Operating
System for more information. A
collating sequence may define
"equivalence classes" for charac-
ters. For example, if three char-
acters--e, e<, and e>--are
equivalent, the following
expressions identify the same
sequence of characters:
[a-e]
[a-e>]
The minus character loses its
special meaning if it occurs first
([-string]), if it immediately
follows an initial circumflex
([^-string]), or if it appears last
([string-]) in the string.
] When the right square bracket (])
is the first character in the
string ([]string]) or when it imme-
diately follows an initial
circumflex ([^]string]), it is
treated as a part of the string
rather than as the string termi-
nator.
\sym A \ (backslash) followed by a special pattern-
matching character matches the special char-
acter itself (as a literal character). These
special pattern-matching characters are:
. "*" [ \ Always special except when they
appear within square brackets ([]).
^ Special at the beginning of an
entire pattern or when it imme-
diately follows the left bracket of
a pair of brackets ([^ . . . ]).
"$" Special at the end of an entire
pattern.
In addition, the character used to delimit an
entire pattern is special for that pattern.
(For example, see how slash (/) is used in the
g subcommand on page .)
Forming Patterns
The following rules describe how to form patterns from
REs:
1. An RE that consists of a single, ordinary character
matches that same character in a string.
2. An RE followed by an asterisk ("*") matches zero or
more occurrences of the character that the RE
matches. For example, the following pattern:
ab*cd
matches each of the following strings:
acd
abcd
abbcd
abbbcd
but not the following string:
abd
If there is any choice, the longest matching leftmost
string is chosen. For example, given the following
string:
122333444
the pattern ".*" matches "122333444", the pattern
".*3" matches "122333", and the pattern ".*2" matches
"122".
3. An RE followed by:
\{m\} Matches exactly m occurrences of the char-
acter matched by the RE.
\{m,\} Matches at least m occurrences of the char-
acter matched by the RE.
\{m,n\} Matches any number of occurrences of the
character matched by the RE from m to n
inclusive.
m and n must be integers from 0 to 255,
inclusive. Whenever a choice exists, this
pattern matches as many occurrences as pos-
sible.
4. You can combine REs into patterns that match strings
containing that same sequence of characters. For
example, "AB\*CD" matches the string "AB*CD" and
"[A-Za-z]*[0-9]*" matches any string that contains
any combination of alphabetic characters (including
none), followed by any combination of numerals
(including none).
5. The character sequence "\("pattern"\)" marks a sub-
pattern that matches the same string it would match
if it were not enclosed.
6. The characters \num match the same string of charac-
ters that a subpattern matched earlier in the pattern
(see the preceding discussion of item 5). num is a
digit. The pattern \num matches the string matched
by the numth subpattern, counting from left to right.
For example, the following pattern:
\(A\)\(B\)C\2\1
matches the string "ABCBA". You can nest subpat-
terns.
Restricting What Patterns Match
A pattern can be restricted to match only the first
segment of a line, the final segment, or both:
1. A ^ (circumflex) at the beginning of a pattern causes
the pattern to match only a string that begins in the
first character position on a line.
2. A "$" (dollar sign) at the end of a pattern causes
that pattern to match only a string that ends with
the last character (not including the new-line char-
acter) on a line.
3. The construction ^pattern"$" restricts the pattern to
matching only an entire line.
In addition, the null pattern (that is, //) duplicates
the previous pattern.
ADDRESSING
There are three types of ed addresses: line number
addresses, addresses relative to the current line, and
pattern addresses. The current line (usually the last
line affected by a command) is the point of reference in
the buffer. This is the default address for several ed
commands. (See "Subcommands" to find out how each sub-
command affects the current line.)
Following are guidelines for constructing addresses:
1. . (dot) addresses the current line.
2. "$" (dollar sign) addresses the last line of the
buffer.
3. n addresses the nth line of the buffer.
4. 'x addresses the line marked with a lowercase ASCII
letter, x, by the k subcommand (see page ).
5. /pattern/ (a pattern enclosed in slashes) addresses
the next line 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.
6. "?"pattern"?" (a pattern enclosed in question marks)
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.
7. An address followed by +n or -n (a plus sign or a
minus sign followed by a decimal number) specifies an
address plus or minus the indicated number of lines.
(The + sign is optional.)
8. 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).
9. 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.
10. For convenience, a "," (comma) stands for the address
pair "1,$" (first line through last line) and a ;
(semicolon) 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 (rightmost) one(s).
In most cases, commas (",") separate addresses (for
example "2,8"). Semicolons (";") also can separate
addresses. A semicolon between addresses causes ed to
set the current line to the first address and then calcu-
late the second address (for example, to set the starting
line for a search based on rules 5 and 6 above). In a
pair of addresses, the first must be numerically smaller
than the second.
For many purposes, you may prefer to use a different
editor that has different features:
o "edit," a simple line editor for novice or casual
users
o "sed," a stream editor often used for writing pro-
grams
o "ex," an extended (line) editor with interactive sub-
command features
o "vi, vedit, view," a visual (screen) editor that also
accesses ex line editing features while letting you
view the text.
The following is a list of ed size limitations:
o 64 characters per file name.
o 512 characters per line (although there is currently
a system-imposed limit of 255 characters per line
entered from the keyboard).
o 256 characters per global subcommand list.
o 128K characters 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) available or on the maximum number of lines per-
mitted 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 sub-
commands, which can be added to any subcommand except e,
f, r, or w. The e, f, r, and w subcommands accept file
names as parameters. The ed program stores the last file
name used with a subcommand as a default file name. The
next e, f, r, or w given without a file name uses the
default file name.
The ed program responds to an error condition with one of
two messages: "?" (question mark) or "?"file. When ed
receives an INTERRUPT signal (Alt-Pause), it displays a
"?" and returns to command mode. When ed reads a file,
it discards ASCII NULL characters and all characters
after the last new-line character. ed cannot edit a file
that contains characters not in the ASCII set (for
example, an a.out file with bit 8 set on).
In the following list of ed subcommands, default
addresses are shown in parentheses. Do not key in the
parentheses. The address . (period) refers to the
current line. When a . is shown in the first position on
an otherwise empty line, it is the signal to return to
command mode.
(.)a
<text>
. The append subcommand adds text to the
buffer after the addressed line. 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 subcommand to
add text at the beginning of the
buffer.
(.)c
<text>
. The change subcommand deletes the
addressed lines, then replaces them
with new input. 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.
(.,.)d The 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 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
characters read in to the buffer. If
the buffer has been 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 file name to be used, if neces-
sary, by subsequent e, r, or w subcom-
mands. (See the f subcommand.)
When the "!" character replaces file, e
takes the rest of the line as a AIX
shell (sh) command and reads the
command output. The e subcommand does
not store the name of the shell command
as a default file name.
E file The Edit subcommand works like e, with
one exception: E does not check for
changes made to the buffer since the
last w subcommand.
f [file] The file name subcommand changes the
default file name (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 file
name.
(1,"?")g/pattern/subcmd-list The 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 executes subcmd-list.
A single subcommand, or the first sub-
command of a list, should appear on the
same line with the g subcommand; subse-
quent subcommands should appear on sep-
arate lines. Except for the last line,
each of these lines should end with a
\.
The subcmd-list can include the a, i,
and c subcommands and their input. If
the last command in subcmd-list would
normally be the . (dot) that ends input
mode, the . (dot) is optional. If
there is no subcmd-list, ed displays
the current line. The subcmd-list
cannot include the g, G, v, or V sub-
commands.
Note: The g subcommand is similar to
the v subcommand, which executes
subcmd-list for every line that does
not contain a match for the pattern.
(1,"?")G/pattern/ The interactive 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 sub-
commands: a, c, i, g, G, v, and V.
After the subcommand finishes, G dis-
plays the next marked line, and so on.
G takes a new-line character as a null
subcommand. An :& causes G to execute
the previous subcommand again, if there
was one. Note that subcommands exe-
cuted within the G subcommand can
address and change any lines in the
buffer. The G subcommand can be termi-
nated by pressing INTERRUPT
(Alt-Pause).
h The help subcommand gives a short
explanation (help message) for the most
recent ? diagnostic or error message.
H The Help subcommand causes ed to
display the help messages for all sub-
sequent "?" diagnostics. H also
explains the previous "?" if there was
one. H alternately turns this mode on
and off; it is initially off.
(.)i
<text>
. The insert subcommand inserts text
before the addressed line and sets the
current line to the last inserted line.
If there 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.
(.,.+1)j The join subcommand joins contiguous
lines by removing the intervening new-
line characters. If given only one
address, j does nothing. (For split-
ting lines, see the s subcommand.)
(.)kx The mark subcommand marks the addressed
line with name x, which must be a low-
ercase ASCII letter. The address 'x
(single quotation mark before the
marking character) then addresses this
line. The k subcommand does not change
the current line.
(.,.)l The list subcommand displays the
addressed line(s). The l subcommand
wraps long lines and, unlike the p sub-
command, represents non-printing char-
acters, either with mnemonic
overstrikes or in octal notation. An l
subcommand may be appended to any ed
subcommand except: e,f,r, or w.
(.,.)ma The move subcommand repositions the
addressed line(s). The first moved
line follows the line addressed by a.
Address "0" for a causes m to move the
addressed line(s) to the beginning of
the file. Address a cannot be one of
the lines to be moved. The m subcom-
mand sets the current line to the last
moved line.
(.,.)n The number subcommand displays the
addressed lines, each preceded by its
line number and a tab character (dis-
played as blank spaces); n leaves the
current line at the last line dis-
played. An n subcommand may be
appended to any ed subcommand except e,
f, r, or w.
(.,.)p The print subcommand displays the
addressed line(s) and sets the current
line set to the last line displayed. A
p subcommand may be appended to any ed
subcommand except: e, f, r, or w. For
example, the subcommand dp deletes the
current line and displays the new
current line.
P The P subcommand turns on or off the ed
prompt string "*" (asterisk). Ini-
tially, P is off.
q The quit subcommand exits the ed
program. Before ending the program q
checks to determine whether the buffer
has been written to a file since the
last time it was changed. If not, q
displays the ? message.
Q The Quit subcommand exits the ed
program without checking for changes to
the buffer since the last w subcommand
(compare with the q subcommand).
("?")r file The 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 subcom-
mands). r does not change the default
file name. 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 char-
acters read into the buffer and sets
the current line to the last line read.
If "!" (exclamation point) replaces
file in a r subcommand, r takes the
rest of the line as a AIX shell (sh)
command whose output is to be read.
The r subcommand does not store the
names of shell commands as default file
names.
(.,.)s/pattern/replacement/
(.,.)s/pattern/replacement/g The substitute subcommand
searches each addressed line for a
string that matches the pattern and
then replaces the string with the spec-
ified replacement string. Without the
global indicator (g), s replaces only
the first matching string on each
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
new-line 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/are/&n't/
has the same effect as the subcommand
s/are/aren't/ and replaces are with
aren't on the current line. A back-
slash before the ampersand (\"&")
removes this special meaning of "&" in
replacement.
A subpattern is part of a pattern
enclosed by the strings \( and \); the
pattern works as if the enclosing char-
acters were not present. In replace-
ment, the characters \n refer to
strings that match subpatterns; n, a
decimal number, refers to the nth sub-
pattern, counting from the left. (for
example, s/\(t\)\(h\) \(e\)/t\1\2ose)
replaces the with those if there is a
match for the pattern the on the
current line). Whether subpatterns are
nested or in a series, \n refers to the
nth occurrence, counting from the left,
of the delimiting characters, \).
The "%" (percent sign) character, when
used by itself as replacement, 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 \.
Lines may be split by substituting new-
line characters into them. In replace-
ment, the sequence \Enter quotes the
new-line character (not displayed) and
moves the cursor to the next line for
the remainder of the string. New-lines
cannot be substituted as part of a g or
v subcommand list.
(.,.)ta The transfer subcommand inserts a copy
of the addressed lines after address a.
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 undo subcommand restores the buffer
to the state it was in before it was
last modified by an ed subcommand. The
commands that u can undo are: a, c, d,
g, G, i, j, m, r, s, t, v, and V.
(1,"?")v/pattern/subcmd-list The v subcommand executes
the subcommands in subcmd-list for each
line that does not contain a match for
the pattern.
Note: The v subcommand is a complement
for the global subcommand g, which exe-
cutes subcmd-list for every line that
does contain a match for the pattern.
(1,$)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 subcommand.
Note: The V subcommand complements the
G subcommand, which marks the lines
that do match the pattern.
(1,"?")w file The 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 code 666 (read and
write permission for everyone), unless
the umask setting specifies another
file creation mode. (For information
about file permissions, see "umask"
and "chmod.") The w subcommand does not
change the default file name (unless
file is the first file name used since
you started ed). If you do not provide
a file name, ed uses the default file
name, if any (see the e and f subcom-
mands). The w subcommand does not
change the current line.
If ed successfully writes the file, it
displays the number of characters
written. When "!" replaces file, ed
takes the rest of the line as a AIX
shell (sh) command whose output is to
be read; w does not save shell command
names as default file names.
Note: " 0" is not a legal address for
the w subcommand. Therefore, it is not
possible to create an empty file with
ed.
("$")= Without an address, the = (equal sign)
subcommand displays the current line
number. With the address "$", = dis-
plays 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.
!AIX-cmd The "!" (exclamation point) subcommand
allows AIX commands to be run from
within ed. Anything following ! on an
ed subcommand line is interpreted as an
AIX command. Within the text of that
command string, ed replaces the unes-
caped character "%" with the current
file name, if there is one.
When used as the first character of a
shell command (after the "!" that runs
a subshell) ed replaces the "!" char-
acter with the previous AIX command;
for example, the command !! repeats the
previous AIX command. If the AIX
command interpreter (the sh command),
expands the command string, ed echoes
the expanded line. The "!" subcommand
does not change the current line.
num
+num
-num ed interprets a number alone on a line
as an address and displays the
addressed line. Addresses can be abso-
lute (line numbers or "$") or relative
to the current line ("+"num or - num).
Entering a new-line character (a blank
line) is equivalent to +1p and is
useful for stepping forward through the
buffer one line at a time.
FLAGS
- Suppresses character counts that the editor
displays with the e, r, and w subcommands,
suppresses diagnostic messages for the e and
q subcommands, and suppresses the "!" prompt
after a !AIX-cmd.
-p string Sets the editor prompt to string. The
default for string is null (no prompt).
FILES
/tmp/e# Temporary file; # is the process number.
ed.hup Work is saved here if the terminal hangs up
while ed is running.
RELATED INFORMATION
The following commands: "grep," "sed," "sh," "stty,"
and "regcmp."
The regexp system call in AIX Operating System Technical
Reference.
The environment miscellaneous facility in Text Formatting
Guide.
The discussion and examples of ed in Using the AIX Oper-
ating System.
The "Overview of International Character Support" in Man-
aging the AIX Operating System.