ed(1) ed(1)
NAME
ed - interactive line editor
SYNOPSIS
ed [-s] [-p string] [-x] [-C] [--] [file]
DESCRIPTION
ed is an interactive line editor. Furthermore, with the help of ed
scripts (see WORKING WITH ED SCRIPTS), you can easily process several
files with the same sequence of commands. ed can handle the output of
the diff -e command [see diff(1)].
OPTIONS
-s The -s option suppresses the following default outputs:
- number of bytes processed by the ed commands:
e (edit)
r (read)
w (write)
- the question mark character, which warns against inadvertent
deletion of the buffer contents during execution of the ed
commands:
e (edit)
q (quit)
- the exclamation mark ! used as an ed prompt after a ! command.
The -s option corresponds to the old - option, which is still
supported.
-p string
In string you can define the prompt that ed displays in command
mode. string can be one or more characters.
-p string not specified:
ed does not display a prompt string.
-x Encryption option: ed executes its own X command and prompts you
for a key. This key is then used to encrypt and decrypt text
using the algorithm of the crypt command. ed checks whether or
not the text to be read is encrypted. The temporary buffer file
is likewise encrypted, using a transformed version of the key you
enter here [see crypt(1)].
Page 1 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
-C Encryption option; the same as the -x option, except that ed exe-
cutes its C command. The C command is like the X command, except
that all text read in is assumed to have been encrypted [see
crypt(1)].
-- If file begins with a dash (-), the end of the command-line
options must be marked with --.
file The name of the file that you wish to process. ed copies the file
into its internal buffer and saves file as the current file name.
file not specified:
You start by working on an empty buffer and only decide upon a
file name when using the w file command to write the buffer con-
tents into a file.
ED BUFFER
When ed is invoked, a buffer is opened.
If you have not entered a file name, the buffer will be empty. You can
then fill it with text during your editor session.
If you have named a file, a copy of the file is read into the buffer.
During your editor session, you essentially process the contents of
the buffer.
Before you exit the editor again, you must decide whether you want to
save the newly created or modified buffer contents by writing them to
a file.
If you wish to save the buffer contents, you use the w [file] (write)
command to write the contents of the buffer back to the specified file
(by default the one named when ed was invoked) and then exit the edi-
tor with the q (quit) or Q (Quit) commands or with the <CTRL-D> key.
If you do not wish to save the buffer contents, you can exit the edi-
tor without writing back the contents of the buffer with w. You do
this by pressing Q or q twice. When you press q the first time, ed
will issue a ? as a warning to prevent you from inadvertently deleting
the buffer contents. The buffer will not be deleted unless and until
you press q a second time. If you prefer, you can also enter Q or
<CTRL-D> instead of the second q.
OPERATING MODES
ed provides you with two operating modes: command mode and input mode.
ed enters command mode when it is called with ed [file] <RETURN>. In
command mode you specify a command in a line and confirm your input by
pressing <RETURN>.
Page 2 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
Input mode is activated by means of one of the following commands:
a (append)
i (insert)
c (change)
(see ED COMMANDS below).
In input mode, all the input characters which follow, including vari-
ous non-printing characters (e.g. key codes of cursor keys), are writ-
ten to the working copy in the buffer. ed does not accept any commands
in input mode. If you wish, you can define a prompt for command mode
(see option -p and ed command P) so that you can instantly detect the
mode in which you are currently working. You leave input mode either
by pressing <DEL> or by entering a period (.) in the first column and
hitting <RETURN>. When you press <DEL>, ed normally ignores all input
since the last <RETURN> and displays a ? as a warning.
COMMAND STRUCTURE
For ed, there is a current line at all times. As a rule, the line last
processed by a command represents the current line. If you do not
specify another address in front of the commands, they will always
refer to the current line.
Most ed commands have the following structure:
______________________________________________________________________
[range]ed-command[parameter ...] <RETURN>
______________________________________________________________________
range
The range you enter identifies the lines in the buffer to which
the ed command is to be applied. One or two addresses can be
specified in range:
range = address
The line identified by address is selected.
range = address1,address2
limits (inclusive). The search for both addresses begins at
the current line, which is not changed until commands are
executed.
address2 must refer to a line that follows the line refer-
enced by address1 in the buffer. Otherwise, ed reports an
error.
Page 3 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
range = address1;address2
address1;address2 identifies the range between the specified
limits (inclusive). The search for address1 begins at the
current line. The new current line is set to the line iden-
tified by address1, and only then is address2 calculated.
You can use this feature to define the starting line for
forward and backward searches (see ADDRESSES).
address2 must refer to a line that follows the line refer-
enced by address1 in the buffer. Otherwise, ed reports an
error.
range not specified:
ed assumes the default address for each command; this
address is described for each of the ed commands.
If ed requires no address but you have nevertheless specified one, ed
reports an error.
If you have specified more addresses than necessary, ed uses the last
address(es) specified.
ADDRESSES
Addresses are constructed as follows:
Address Meaning
. Current line
$ Last line
n nth line
'x The line marked with the letter x. x must be a letter in
lowercase (see k command).
/RE/ A simple regular expression RE enclosed in /.../ [see
expressions(5)] addresses the first line containing a
character string that matches the regular expression,
searching forward from the current line.
If ed does not find a match in any line, the search wraps
around and continues from the beginning of the file until
a match is found or the current line is reached again. If
the regular expression RE contains the delimiter / or ?,
this must be escaped using \.
The characters \n in a regular expression do not match new-
line characters in the searched text! A regular expression
may only appear once in range. Thus, /RE1/,/RE2/, for exam-
ple, will only address the first line that matches /RE2/.
Page 4 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
// A null regular expression // addresses the line matching
the regular expression last specified.
?RE? Like /RE/, except that the search begins at the current
line and proceeds toward the beginning of the file. If the
regular expression RE contains the delimiter / or ?, this
must be escaped using \.
addr[+]n nth line after the line identified by addr.
addr-n nth line before the line identified by addr.
+n n lines forward from the current line.
-n n lines backward from the current line.
[addr]+ ...
[addr]- ... One line forward (+) or backward (-) from the line identi-
fied by addr. Each occurrence of + or - respectively
increases or decreases the address specification by 1.
Thus, ++ addresses the second line after the current line
(2 lines forward).
, A comma stands for the address pair 1,$ if followed by a
command; if not, the last line is output.
; A semicolon stands for the address pair .,$ if followed by
a command; if not, the last line is output.
ED COMMANDS
The following list includes a systematic overview of all ed commands
that you can enter in the command mode. The detailed command descrip-
tion that follows is arranged in alphabetical order.
Overview of the ed commands
______________________________________________________________________
| Activate input mode |
|__________|_____________|____________________________________________|
| a | append | append text after addressed line |
|__________|_____________|____________________________________________|
| c | change | delete and replace |
|__________|_____________|____________________________________________|
| i | insert | insert text before addressed line |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Output prompt in command mode |
|__________|_____________|____________________________________________|
| P | prompt | use * as prompt |
|__________|_____________|____________________________________________|
Page 5 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
______________________________________________________________________
| Undo commands |
|__________|_____________|____________________________________________|
| u | undo | undo most recent command |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Abort commands |
|__________|_____________|____________________________________________|
| <DEL> | --- | abort execution of command |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Explain errors |
|__________|_____________|____________________________________________|
| h | help | explain last error message |
|__________|_____________|____________________________________________|
| H | Help | Toggle help mode on and off. If help mode |
| | | is on, error messages are printed for all |
| | | subsequent ? diagnostics (see ERROR MES- |
| | | SAGES). |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Modify text |
|__________|_____________|____________________________________________|
| a | append | append text after addressed line |
|__________|_____________|____________________________________________|
| c | change | delete and replace |
|__________|_____________|____________________________________________|
| d | delete | delete lines from buffer |
|__________|_____________|____________________________________________|
| i | insert | insert text before addressed line |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Output lines |
|__________|_____________|____________________________________________|
| p | print | print addressed lines |
|__________|_____________|____________________________________________|
| l | list | output with non-printing characters in |
| | | alternative representation or as octal |
| | | numbers |
|__________|_____________|____________________________________________|
| address | --- | output addressed lines |
|__________|_____________|____________________________________________|
| n | number | print indicated lines with line numbering |
|__________|_____________|____________________________________________|
| <RETURN> | --- | output line following current line |
|__________|_____________|____________________________________________|
Page 6 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
______________________________________________________________________
| Output line numbers |
|__________|_____________|____________________________________________|
| address= | --- | output addressed line number |
|__________|_____________|____________________________________________|
| n | number | output lines with line numbering |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Move specified line ranges |
|__________|_____________|____________________________________________|
| t | transfer | append a copy of addressed lines |
|__________|_____________|____________________________________________|
| m | move | move lines to after addressed line |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Search and replace |
|__________|_____________|____________________________________________|
| s | substitute | search and replace |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Join lines |
|__________|_____________|____________________________________________|
| j | join | join contiguous lines |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Mark lines |
|__________|_____________|____________________________________________|
| k | mark | mark addressed lines |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Process selected lines with commands |
|__________|_____________|____________________________________________|
| g | global | apply command list globally to all lines |
| | | that match the given /RE/ |
|__________|_____________|____________________________________________|
| G | Global | apply interactive command list globally to|
| | | all lines that match the given /RE/ |
|__________|_____________|____________________________________________|
| v | vice-versa | like g, but for all lines that do not |
| | | match /RE/ |
|__________|_____________|____________________________________________|
| V | Vice-versa | like G, but for all lines that do not |
| | | match /RE/ |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Change current file name |
|__________|_____________|____________________________________________|
| f | file-name | change/display current file name |
|__________|_____________|____________________________________________|
Page 7 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
______________________________________________________________________
| Execute shell commands |
|__________|_____________|____________________________________________|
| ! | --- | send command to shell for interpretation |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Read files into buffer |
|__________|_____________|____________________________________________|
| e | edit | delete buffer and read named file into it |
|__________|_____________|____________________________________________|
| E | Edit | clear buffer without warning and reload |
| | | original |
|__________|_____________|____________________________________________|
| r | read | read file into buffer |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Encrypt and decrypt file contents |
|__________|_____________|____________________________________________|
| C | crypt | specify key for reading |
|__________|_____________|____________________________________________|
| X | crypt | specify key for reading and saving |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Save buffer contents |
|__________|_____________|____________________________________________|
| w | write | write buffer contents into file |
|__________|_____________|____________________________________________|
| W | write | append buffer contents to file |
|__________|_____________|____________________________________________|
______________________________________________________________________
| Quit the editor |
|__________|_____________|____________________________________________|
| q | quit | quit ed |
|__________|_____________|____________________________________________|
| Q | Quit | quit ed without warning |
|__________|_____________|____________________________________________|
| <END> | --- | quit ed |
|__________|_____________|____________________________________________|
DESCRIPTION OF THE ED COMMANDS
The square brackets [] are not to be entered. They merely indicate
that the entry enclosed within them is optional.
As a rule, only one command may be entered per line. However, you can
append the suffixes l, n, or p to the commands (with the exception of
e, f, r, and w) if the functions described under l, n, and p are to be
executed.
Page 8 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
[address]a
text
. (append) reads the text input and appends it to the line
addressed in address. The current line is now either the last
line of the inserted text or, if you have not entered any text,
the addressed line. Address 0 is legal for this command: it
causes the "appended" text to be inserted at the beginning of the
buffer. The maximum number of characters that may be entered from
a terminal is 2048 per line (including the newline character).
address not specified:
address = .
[range]c
text
. (change) deletes the specified range and replaces these lines
with the text input. The current line is now either the last line
of the entered text or, if you have not entered any, the line
following the deleted lines.
range not specified:
range = .,.
C Encryption command:
the C command is like the X command, except that all text read in
with the e and r commands is assumed to have been encrypted (see
option -C).
[range]d
(delete) deletes the specified range. The line after the last
line deleted becomes the current line. If the deleted lines were
at the end of the buffer, the new last line becomes the current
line.
range not specified:
range = .,.
e [file]
(edit) deletes the entire buffer and reads in a copy of the con-
tents of the named file. If the contents of the buffer have been
modified but not saved with w, ed prevents inadvertent deletion
of the buffer by first issuing a ? as a warning. If you now enter
e again, the old buffer contents are deleted without further com-
ment. The number of bytes read is output provided you did not
call ed with the -s option. The current line is the last line of
the buffer. The specified file name file is remembered for possi-
ble use as a current file name in subsequent e, r, and w com-
mands. If file is replaced by an exclamation point !, the rest of
Page 9 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
the line is interpreted as a shell command and executed. The out-
put of the shell command is read into the buffer. A shell command
that is preceded by an ! is not stored as the file name.
file not specified:
file = current file name
E [file]
(Edit) behaves like edit, except that it overwrites the buffer
without issuing a ? as a warning even if the buffer contents have
been modified but not saved.
file not specified:
file = current file name
f [file]
(file) sets the current file name to file. The current file name
is used by the commands e, E, r and w.
file not specified:
ed outputs the current file name.
[range] g/RE/commandlist
(global) first marks all lines containing a character string
which matches the regular expression RE. RE is a simple regular
expression [see expressions(5)]. Then commandlist is executed for
each line marked, with the current line being set to the next
marked line in each case.
A single command or the first in a commandlist must be entered in
the same line as the g command. All lines in commandlist except
the last one must end with \<RETURN>; the last command itself
with <RETURN>.
The commands a, i, and c with their associated input text are
allowed. All lines in text must also be terminated with
\<RETURN>. The period "." usually used to terminate input can be
omitted from the last line of commandlist.
An empty commandlist is equivalent to the p command.
The g, G, v, and V commands are not permitted in the commandlist.
The global command must not be combined with the ! command.
range not specified:
range = 1,$
Page 10 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
[range]G/RE/
(Global) is the interactive variant of the g command. First,
every line that matches RE is marked. RE is a simple regular
expression [see expressions(5)]. The first of the marked lines is
printed. At the same time, this line becomes the current line.
You now have the option of specifying a command to be executed
(other than a, c, i, g, G, v, or V). After the execution of that
command, the next marked line is printed, and so on.
A newline acts as a null command; an ampersand & causes the 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 pressing the <DEL> key.
range not specified:
range = 1,$
h (help) issues a short error message that explains the reason for
the most recent ? symbol displayed on the screen. See ERROR MES-
SAGES for a list of possible error messages.
H (Help) causes ed to enter a mode in which error messages are
printed instead of the ? symbol for all errors that follow. It
will also explain the previous ?, if any. You can deactivate the
help feature by calling the H command again.
The H mode is normally off. See ERROR MESSAGES for a list of pos-
sible error messages.
[address]i
text
. (insert) inserts the given text before the line referenced by
address. The last inserted line of text, if any, becomes the
current line; otherwise, the addressed line does. This command
differs from the append command only in the placement of the
entered text. Address 0 is not legal. The maximum number of char-
acters that may be entered from a terminal is 2048 per line
(including the newline character).
address not specified:
address = .
Page 11 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
[range]j
(join) joins all lines in the specified range into one line by
removing the appropriate newline characters. If you have only
specified one address, nothing happens. The new line becomes the
current line.
range not specified:
range = .,.
[address]kx
(mark) marks the line referenced by address with the letter
specified in x, where x must be in lowercase. The marker itself
is not output. The marked line can then be addressed by using 'x
(single quote x). The current line is unaffected.
address not specified:
address = .
[range]l
(list), in contrast to p, outputs the specified range as follows:
some non-printing characters are output in alternative represen-
tation (e.g. tab characters), the remaining non-printing charac-
ters are output as octal numbers. Overlength lines are folded
into several lines, each terminated by the line continuation
character \. Each line must end with $. An l command can be
appended to any command other than e, f, r, or w.
The following alternative representations are used:
\\ Backslash (for distinguishing octal characters)
\a Warning, bell
\b Backspace
\f Form Feed
\r Carriage Return
\t Tab
\v Vertical tab
range not specified:
range = .,.
Page 12 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
[range]maddress
(move) re-positions the lines in the specified range after the
line addressed by address. The last of the shifted lines becomes
the current line. If you specify a value of 0 for address, the
range is moved to the beginning of the file. If address falls
within the range of moved lines, ed issues an error message.
range not specified:
range = .,.
[range]n
(number) prints the range of addressed lines, preceding each line
by its line number and a tab character. The last line to be
printed becomes the new line. The n command can be appended to
any command other than e, f, r, or w.
range not specified:
range = .,.
[range]p
(print) prints the range of addressed lines. Non-printing charac-
ters are not output unchanged. Overlength lines are continued in
the next line, i.e. are not identifiable as such. The current
line is the last line printed. The p command can be appended to
any command other than e, f, r and w. For example, dp deletes the
current line and prints the new current line.
range not specified:
range = .,.
P (Prompt) causes ed to use an asterisk * or the defined prompt
string as a prompt in command mode (see option -p). You can deac-
tivate this mode again by calling P a second time. This mode is
normally deactivated.
q (quit) terminates ed. If you have changed the buffer contents
since the last time the buffer was saved or overwritten, but have
not yet written these changes to a file with w, ed outputs a ? as
a warning (to prevent inadvertent deletions) and waits for
further input. By entering <CTRL-D>, Q, or the q command a second
time, you can now exit ed without further warnings and without
saving the buffer.
Caution:
If you continue after the first q with actions that do not change
the buffer contents, no new warning will be issued when you press
q again. ed will be terminate without saving the buffer.
Page 13 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
Q (Quit) terminates ed immediately without warning even if you have
changed the buffer contents since last saving or overwriting and
have not yet written these changes to a file with w.
[address]r [file]
(read) reads the named file and inserts its contents after the
line identified by address.
The address 0 is legal for this command and causes the file con-
tents to be written at the start of the buffer. If the read was
successful, the number of bytes read is output unless you called
ed with the -s option. The current line is the last line read in.
The currently remembered file name is not changed to file unless
you invoked ed without a file name and file is the very first
file name mentioned since ed was invoked. If file is replaced by
the ! character, the rest of the line is interpreted as a shell
command and executed. The output of this command is then read.
Such a command is not remembered as the current file name.
address not specified:
address = $
file not specified:
file = current file name
[range]s/RE/replacementstring/[g|n]
(substitute) searches each line in range for strings which match
RE. RE is a simple regular expression [see expressions(5)]. On
each line in which a match is found, strings that match RE are
replaced by replacementstring: without g and n, only the first
occurrence of the matched string is replaced; with g, all matches
are replaced; and with n, the nth occurrence on the line is
replaced. n is a positive integer between 1 and 512. If ed does
not find a matching string, it returns a ? to indicate an error.
To delimit the regular expression RE from the s command and the
replacementstring, any other character except the blank or new-
line can be used instead of /. The character selected is recog-
nized as a delimiter by virtue of coming immediately after s.
Afterwards, the current line is the line in which the last sub-
stitution took place.
Page 14 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
Metacharacters in the replacement string
Metacharacters Meaning
& is replaced by the string which matches regular
expression RE in a successful search.
\n is replaced by the character string matching the
nth regular subexpression, delimited by \(...\),
of RE where n is a decimal digit. If nested
parenthesized subexpressions are present, n is
determined by counting occurrences of \( from
left to right.
% is replaced by the replacement string from the
most recent s command if replacementstring con-
sists solely of the % character.
The special meaning of these characters can be suppressed by
preceding each of them with a backslash \.
Splitting a line in the replacement string
To split a line you can include an escaped newline character,
i.e. \<RETURN>, in replacementstring. This type of substitution
command cannot be used in a commandlist with a g or v command.
range not specified:
range = .,.
[range]taddress
copies the addressed range after the specified line address. 0 is
allowed for address. The current line is the last of the copied
lines.
range not specified:
range = .,.
u (undo) nullifies the effect of the most recent command that modi-
fied anything in the buffer. The following commands can be
undone: a, c, d, g, i, j, m, r, s, t, v, G, and V.
[range]v/RE/commandlist
(vice versa) causes commandlist to be executed on all lines con-
taining no string that matches the regular expression RE. RE is a
simple regular expression [see expressions(5)]. v functions
identically to the global command g with the selection criterion
reversed.
v should not be combined with the ! command.
Page 15 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
range not specified:
range = 1,$
[range]V/RE/
(Vice versa) is the interactive variant of the v command. You can
use V to process all lines that contain no string matching the
regular expression RE. RE is a simple regular expression [see
expressions(5)]. V functions identically to the global command G
with the selection criteria reversed.
range not specified:
range = 1,$
[range]w [file]
(write) writes the specified range into the named file. The
currently remembered file name is not changed if previously set.
If the file name has not been set, file is used as the new
current file name. The old contents of file are overwritten in
the process. If the named file does not exist, it is created. The
current line remains unchanged. After a successful write opera-
tion the number of characters written is displayed, provided you
did not invoke ed with the -s option.
If file is replaced by !, the rest of the line is interpreted as
a shell command and executed. The standard input for the shell
command is the range of addressed lines. Such a command is not
remembered as the current file name.
range not specified:
range = 1,$
file not specified:
file = current file name
[range]W [file]
(Write) appends the specified range to the end of the named file.
This command is the same as the w command above, except that w
overwrites existing files. If file does not exist, it is created.
X Encryption command: ed prompts you for a key, which it uses in
subsequent e, r, and w commands to decrypt and encrypt text using
the algorithm of the crypt command. A null key turns off encryp-
tion. ed verifies whether or not text it reads in (e and r) is
encrypted. The temporary buffer file is likewise encrypted, using
a transformed version of the key you type in here (see the -x
option).
Page 16 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
[address]=
The line number of the line identified by address is output; the
current line is not changed by this command.
address not specified:
address = $
address
The line identified by address is output.
!command
The remainder of the line after the ! is interpreted and executed
as a shell command. If an unescaped % character appears within
the text of command, it is replaced by the remembered file name.
!! causes the last command to be repeated. In both cases, the
expanded command line is echoed. On completion of the command, ed
is reactivated. The current line remains unaffected. This command
must not be combined with g and v!
<RETURN>
Entering <RETURN> alone in the command mode causes the line after
the current line to be printed. This is the same as specifying
.+1p. You can use this feature to step through the buffer.
The input of a command in command mode or a text line in input
mode must be terminated by pressing the <RETURN> key.
<DEL>
You can use the <DEL> key to interrupt a currently executing ed
command or to cancel the entry of a line. ed will then respond by
displaying a "?".
<CTRL-D>
The <CTRL-D> key has the same effect as the q command.
If the closing delimiter of a regular expression or of a replace-
ment string (e.g. a /) is the last character before a newline,
you may omit that delimiter. In such a case, the addressed line
is printed. The following pairs of commands are equivalent:
s/s1/s2 s/s1/s2/p
g/s1 g/s1/p
?s1 ?s1?
WORKING WITH ED SCRIPTS
Since ed reads commands and text that is to be inserted from standard
input, you can redirect the input to a file and have ed read the file
instead. Thus
$ ed - file < edscriptfile > output
Page 17 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
edits the named file and processes it with the ed commands stored in
the specified edscriptfile. The -s option suppresses default output
of message texts to the screen.
The advantage of using ed scripts is that they allow you to reproduce
specific command sequences at any time and use them as often as
required. Furthermore, this feature enables you to perform these tasks
in a background process while you continue working at the terminal
without interruptions:
$ ed file < edscriptfile&
If your ed script contains errors, ed will exit on encountering the
first error.
EXIT STATUS
0 if successful
>0 if ed is called incorrectly or if a script is aborted due to
incorrect use of ed commands.
ERROR MESSAGES
If you make a mistake when entering ed commands:
?
This means that the command contains syntax errors.
?file
The indicated file is not available or cannot be read.
More detailed information can be obtained by using the h and H com-
mands. The commonest error messages are listed below. They are all
self-explanatory:
Line out of range
Warning: expecting 'w'
No space after command
Unknown command
Bad range
Cannot open input file
Illegal or missing delimiter
Illegal suffix
Page 18 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
Illegal or missing filename
No match
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed.
In regular expressions in square brackets, the LCCOLLATE environment
variable governs the scope of character ranges, equivalence classes
and collating elements, and the
LCCTYPE environment variable governs the scope of character classes.
LCCTYPE also governs which characters the ed command l treats as
non-printing.
If LCMESSAGES, LCCOLLATE or LCCTYPE is undefined or is defined as
the null string, it defaults to the value of LANG. If LANG is likewise
undefined or null, the system acts as if it were not international-
ized.
If any of the locale variables has an invalid value, the system acts
as if none of the variables were set.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
EXAMPLES
Example 1
Example of an ed script:
The first three lines of a file are to be replaced by one line with
the text "Addresses", and all occurrences of the word "Street" are to
be replaced by "Plaza".
Contents of the edscript:
1,3c
Addresses
.
1,$s/Street/Plaza/g
w
q
Processing of a file with commands from edscript:
$ ed file < edscript
When ed reads commands from a file instead of the keyboard, the editor
is exited as soon as ed encounters the first incomprehensible command.
Page 19 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
Example 2
Example of a Here script [see sh(1)]:
In a set of files that are to be passed as command-line arguments to
the script xy, the first three lines are to be replaced by one line
with the text "Addresses", and all occurrences of the word "Street"
are to be replaced by "Plaza".
Contents of the script file xy:
for i in $*
do
ed $i << scrend
1,3c
Addresses
.
1,\$s/Street/Plaza/g
w
scrend
done
Processing of the files text1, text2 and text3 with script xy:
$ sh xy text1 text2 text3
The string << scrend after the ed call causes the shell to transfer
the text up to the string scrend as input to ed. The second string
scrend must exist as a single word in a line, without leading blanks.
The special meaning of the $ symbol must be escaped with a \ in the
address specification 1,$. This is because the shell would otherwise
interpret the following s as the name of a shell variable.
Example 3
The following example demonstrates and explains a number of ed com-
mands:
$ ed - Call ed
P - Output prompt * in command mode
*a - Append to current line, in this case beginning of file
line1 - Input text
line2
line3
. - Terminate input mode
*1,$p - Output line 1 through to last line
line1
line2
line3
*p - Output current line (= last line processed)
line3
*n - Output current line with line number
Page 20 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
3 line3
*1,$n - Output line 1 through to last line with line numbers
1 line1
2 line2
3 line3
*2c - Replace line 2 with following input including tabs
line2 <TAB> line2 <TAB> line2
another line2
. - Terminate input mode
*p - Output current line
another line2
*1,$n - Output line 1 through to last line with line numbers
1 line1
2 line2 line2 line2
3 another line2
4 line3
*4s/3/4 - In line 4, search for the character 3 and replace it with 4
line4
*n - Output current line with line number
4 line4
*1,$s/l/L/g - Search from line 1 through to last line for all l
characters and replace them with L
*1,$p - Output line 1 through to last line
Line1
Line2 Line2 Line2
another Line2
Line4
*2l - Output line 2 with non-printing characters as mnemonic
overstrikes
Line2\tLine2\tLine2$
*
*2r file - Read the contents of file into the buffer after line 2
and output the number of characters read in. file must
already exist!
46
*1,$n - Output line 1 through to last line with line numbers
1 Line1
2 Line2 Line2 Line2
- This line and the two which follow are out of file
3 line1 of file
4 line2 of file
5 last line of file
6 another Line2
7 Line4
*6,7c - Replace lines 6 to 7 by the following input
very last line
. - Terminate input mode
*1,$n - Output line 1 through to last line with line numbers
1 Line1
2 Line2 Line2 Line2
3 line1 of file
4 line2 of file
Page 21 Reliant UNIX 5.44 Printed 11/98
ed(1) ed(1)
5 last line of file
6 very last line
*q - Attempt to quit editor with q
? - Warning
*h - Explain question mark
warning:expecting 'w'
- Explanation: w command expected by ed (to save contents
of the buffer) was not entered
*w exp - Write the contents of the buffer to the file exp
85 - Number of characters written is output
*q - Quit editor
$ - Shell prompt
FILES
ed.hup
Data is saved in this file if ed receives the SIGHUP signal [see
kill(2)].
/var/tmp
If this directory exists, it is used as the directory for storing
the temporary work file.
$TMPDIR
If this variable is set and is not null, it is used instead of
/var/tmp as the directory for storing the temporary work file.
/tmp /tmp is the directory used to store the temporary work file if
the TMPDIR variable is not assigned the name of an existing
directory and /var/tmp does not exist.
SEE ALSO
ced(1), crypt(1), edit(1), ex(1), grep(1), sed(1), sh(1), stty(1),
umask(1), vi(1), fspec(4), expressions(5), regexp(5).
Page 22 Reliant UNIX 5.44 Printed 11/98