Previous Table of Contents Next


DIAGNOSTICS AND BUGS

ed has very brief and usually useless error messages. Fortunately, there usually isn't too much to explain with a line editor.

? Indicates that you made a mistake on the edit command. Usually, you have a syntax problem. Check the edit command format and try again. Turn on the help feature by typing H and pressing Return.
?file Indicates that ed could not read or write to the specified file. You might receive this message when using the e, r, and w commands. It usually means the file exists and you do not have read or write permissions (whatever the case may be). It may mean you do not have read or write permissions for the directory. In some instances it could also mean the file system is out of disk space. Try the !df command to verify disk space is available.

RELATED COMMANDS

Refer to the nawk, ex, sed, and vi commands described in modules 6, 43, 117, and 151.

RELATED FILES

The editor uses several files and directories while you are editing.

/var/tmp The directory where the temporary file buffer is stored.
TMPDIR The shell variable containing the name of the directory to use instead of the default /var/tmp directory. If the variable is set, the specified path is used; otherwise, /var/tmp is used.
ed.hup The contents of the buffer are saved to this file if the line hangs up (you get disconnected from the system). This is usually caused by a phone line or modem problem, although even direct cabling has been know to be pulled loose from the computer or terminal by clumsy people.

APPLICATIONS

The ed editor can be used to create new text files or update existing ones. It is a fairly powerful line editor. Although most computer users have moved to screen editors, there is still a need for line editors. The most common use is in shell scripts. It is often necessary to make changes to a file from within a shell script. The easiest way to do this may be by using a line editor like ed. One thing that sets UNIX editors apart from most editors is the rich set of wild cards, called Regular Expressions on UNIX. Regular Expressions allow you to match almost any existing string of text you can imagine.

You may find the need to use a line editor when you cannot figure out what the proper terminal type is for a certain terminal you are using. Until you find out the terminal type you cannot use the vi screen editor. Therefore, it is a good idea to know a little about the line editors just in case you get in a time squeeze and are forced to do some editing before you can use vi.

TYPICAL OPERATION

In this activity you use the ed command to create and modify a text file. Begin at the shell prompt.

1.  Type ed ed_file and press Return. Notice the following display:
    cj> ed ed_file
    ?ed_file
    _

Notice there is no internal prompt.
2.  Type a (for append) and press Return to begin entering text. Notice the cursor moves to the next line.
    cj> ed ed_file
    ?ed_file
    a

Again notice no prompt during insert mode either.
3.  Type the following text, pressing Return at the end of each line.
    cj> ed ed_file
    ?ed_file
    a
    This is a new file created by the ed editor.
    It is a line editor.
    I prefer the screen editor.
    But I need to know how to use this editor in certain situations.
    Time to return to the command mode.
4.  Type a . (period) at the beginning of the next line to return to the command mode.
5.  Type w and press Return to save the contents of the buffer to disk and exit the editor.
6.  Type q and press Return to save the contents of the buffer to disk and exit the editor.
7.  Type ed -p "ed " ed_file and press Return to re-edit the ed_file. Notice the following display:
    cj> ed -p "ed " ed_file
    195
    ed>

The 195 is the number of characters ed read from the ed_file file.
8.  Display the entire contents of the buffer by typing ,p and pressing Return.
9.  Display the current line by typing p and pressing Return. The current line is the last line in the buffer.
10.  Delete line 2 by typing 2d and pressing Return.
11.  Insert a new line before the last line of the file as follows:
a.  Type $i and press Return.
b.  Type the line This is a newly inserted line of text. and press Return.
c.  Type . and press Return to return to command mode.
12.  Display the contents of the buffer by typing ,p and pressing Return. Notice the changes to the buffer.
    ed> ,p
    This is a new file created by the ed editor.
    I prefer the screen editor.
    But I need to know how to use this editor in certain situations.
    This is a newly inserted line of text.
    Time to return to the command mode.
13.  Change the word "new" on line 1 to "existing" by typing 1s/ new/n existing/p and pressing Return.
    ed> 1s/ new/n existing/p
    This is an existing file created by the ed editor.

The space before the word "new" is replaced by the character "n" followed by a space. This corrects the preceding "a" to be "an."
14.  Move lines three and four to the beginning of the file. Type 3,4m0 and press Return.
15.  Type ,p and press Return to display the new changes.
    ed> ,p
    But I need to know how to use this editor in certain situations.
    This is a newly inserted line of text.
    This is an existing file created by the ed editor.
    I prefer the screen editor.
    Time to return to the command mode.
16.  To save the changes to disk type w and press Return.
17.  To exit the editor type q and press Return.
18.  To remove unneeded files and conserve disk space remove the ed_file by typing rm ed_file and pressing Return.
19.  Turn to Module 43 to continue the learning sequence.


Previous Table of Contents Next