Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ed - interactive line editor

&pagelevel(4)&pagelevel

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).


Syntax


ed[ -| -s][ -p string][ file]

- | -s

The -s option corresponds to the old - option, which is still supported.

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.

-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.

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 contents 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 editor with the q (quit) or Q (Quit) commands or with CTRL+D.

If you do not wish to save the buffer contents, you can exit the editor 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]. In command mode you specify a command in a line and confirm your input by pressing the enter key.

Input mode is activated by means of one of the following commands:

a (append)
i (insert)
c (change)


(see ed commands).


In input mode, all the input characters which follow, including various non-printing characters (e.g. key codes of cursor keys), are written 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 CTRL+D or by entering a period (.) in the first column and hitting the enter key. When you press CTRL+Ded normally ignores all input since the last enter key stroke 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...]

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

address1,address2 identifies the range between the specified 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 referenced by address1 in the buffer. Otherwise, ed reports an error.

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 identified by address1, and only then is address2 calculated. You can use this feature to define the starting line for forward and backward searches (see the section Addresses, // and /RE/ below) .
address2 must refer to a line that follows the line referenced 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:

AddressMeaning

. Current line
$ Last line
n nth line
'xThe 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 section “Regular POSIX shell expressions”) 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 newline characters in the searched text! A regular expression may only appear once in range. Thus, /RE1/,/RE2/, for example, will only address the first line that matches /RE2/.
// 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[+]nnth line after the line identified by addr.
addr[-]nnth line before the line identified by addr.
+nn lines forward from the current line.
-nn lines backward from the current line.
[addr]+...
[addr]- ...
One line forward (+) or backward (-) from the line identified 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 description that follows is arranged in alphabetical order.

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

Undo commands

u (undo)

undo most recent command

Abort commands

CTRL+D 

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)

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

address +number

output addressed lines

n (number)

print indicated lines with line numbering

enter key

output line following current line

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

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

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

CTRL+D

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.


[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 thetext input. The current line is now either the last line of the entered text or, if you have not entered any, the line preceding the deleted lines.

range not specified:
range = .,.

[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 contents 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 comment. 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 possible use as a current file name in subsequent e, r, and w commands. If file is replaced by an exclamation point !, the rest of the line is interpreted as a shell command and executed. The output 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 section “Regular POSIX shell expressions”). 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 \ and the enter key; the last command itself with the enter key.

The commands a, i, and c with their associated input text are allowed. All lines in text must also be terminated with \ and the enter key. 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,$

[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 section “Regular POSIX shell expressions”). 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 CTRL+C.

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 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 for a list of possible 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 characters that may be entered from a terminal is 2048 per line (including the newline character).

address not specified:address = .

[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 representation (e.g. tab characters), the remaining non-printing characters 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)
\aWarning, Bell *)
\bBackspace *)
\fForm Feed
\nNewline
\rCarriage Return
\tTab
\vVertical tab *)


*)  These non-printing characters are supported on character terminals only.


range not specified:
range = .,.

[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 characters 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 above). You can deactivate 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.


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 contents 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/replacement_string/[g][l][n][p][count]

(substitute)

searches each line in range for strings which match RE. RE is a simple regular expression (see section “Regular POSIX shell expressions”). On each line in which a match is found, strings that match RE are replaced by replacement_string.
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 replacement_string, any other character except the blank or newline can be used instead of /. The character selected is recognized as a delimiter by virtue of coming immediately after s. Afterwards, the current line is the line in which the last substitution took place.

count

Substitute for the count-th occurence only of the RE found on each addressed line.

g

Globally substitute for all non-overlapping instances of the RE rather than just the first one. If both g and count are specified, the results are unspecified.

l

Write to standard output the final line in which a substitution was made. The line will be written in the format specified for the l (list) command.

n

Write to standard output the final line in which a substitution was made. The line will be written in the format specified for the n (number) command.

p

Write to standard output the final line in which a substitution was made. The line will be written in the format specified for the p (print) command.


Metacharacters in the replacement string

Metacharacters and Meaning



&is replaced by the string which matches regular expression RE in a successful search.
\mis replaced by the character string matching the m-th regular subexpression, delimited by\(...\), of RE where m is a decimal digit. If nested parenthesized subexpressions are present, m is determined by counting occurrences of \ from left to right.
%is replaced by the replacement string from the most recent s command if replacement_string consists 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. \ before newline, in replacement_string. This type of substitution command cannot be used in a commandlist with a g or v command.


range not specified:
range = .,.

[range]ta

copies the addressed range after the specified line a. 0 is allowed for a. 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 modified 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 containing no string that matches the regular expression RE. RE is a simple regular expression (see section “Regular POSIX shell expressions”). v functions identically to the global command g with the selection criterion reversed.

v should not be combined with the ! command.

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 section “Regular POSIX shell expressions”). 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 operation 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.

address

The line identified by address is output.

[address]+num

This command outputs the line that is num lines after the line identified by address.

address not specified:
address = .

[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 = $

!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!

enter key

Pressing the enter key 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 enter key.

CTRL+C or @@c

You can use CTRL+C or @@c to interrupt a currently executing ed command or to cancel the entry of a line. ed will then respond by displaying a ’?’.

CTRL+D or @@d

CTRL+D or @@d has the same effect as the q command.

If the closing delimiter of a regular expression or of a replacement 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/s2s/s1/s2/p
g/s1g/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 < ed_scriptfile > output

edits the named file and processes it with the ed commands stored in the specified ed_scriptfile. 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 < ed_scriptfile&
If your ed script contains errors, ed will exit on encountering the first error.

Exit status

0if successful
>0if ed is called incorrectly or if a script is aborted due to incorrect use of ed commands.

Error

If you do not enter a blank between option p and string in the ed call or if you forget to enter the string:

ed: -p arg missing


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 commands. 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
illegal or missing filename
no match 

File

ed.hup

Data is saved in this file if ed receives the SIGHUP signal (see kill).

/var/tmp

If this directory exists, it is used 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.

Variable

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.

HOME

Determine the pathname of the user’s home directory.

Locale

The following environment variables affect the execution of ed:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE

Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

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

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.

Example 2

Example of a here script (see sh):

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.

See also

ex, grep, sed, sh, stty, umask, vi
Tables and directories, Regular expressions