ed editor

I just want to briefly touch on the ed editor. Because the vi editor was derived from the ed editor, after you understand ed, vi operates much the same way. The ed editor is strictly a line editor, which means you edit a file line by line, whereas vi is a full-screen editor.

The ed utility operates on a copy of the file it is editing; changes made to the copy have no effect on the file until a w (write) command is given. The copy of the text being edited resides in a temporary file called the buffer. There is only one buffer.

The ed utility supports a limited form of regular expression notation. Regular expressions are used in addresses to specify lines and in some commands (for example, s) to specify portions of a line that are to be substituted. To understand addressing in ed, you need to be aware of your current line. Generally speaking, the current line is the last line affected by a command; the exact effect on the current line is discussed under the description of each command.

With ed, lines can be addressed in several ways, but the most easily understood method is by line number. Addressing lines by numbers can be very awkward, so ed provides other mechanisms for addressing lines, such as by the line’s textual contents.

One other thing to keep in mind with ed is that it operates in various modes:

  • Command mode

  • Input mode

  • End-of-line mode

In addition, it’s difficult to determine which mode you are in.

As with most of these editors, I can best describe each editor by providing examples. ed is not a verbose text editor in that it does not always display a prompt. In addition, if it doesn’t understand something, it responds with a ?. Let’s start by using the ed text editor to edit a file named file1:

ed file1 <cr> 

Because this file does not exist, ed responds with a ? as follows:

?junk 

I’ll type q to quit the editor and return to a prompt. This time I’ll edit a file that already exists named file2, the contents of which are as follows:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
vi rules! 

To edit the file, I’ll type the following:

ed file2 

The system responds with this:

173 

Because the file already exists, ed responded by displaying the number of characters in the file.

Another thing I can do in ed is run any shell command by prefixing that command with a !. For example, while in the ed editor, I’ll verify that I am in the /tmp directory by typing the following:

!pwd 

The system responds with this:

    /tmp 
! 

While you are working in ed, the buffer keeps track of which line is the line on which you last performed some operation, and that line is referred to as the “current line.” You can find out the contents of your current line by typing . (the dot), as follows:

. 

The system responds with the contents of the current line:

vi rules! 

Note

Think back to Chapter 1, “Fundamental Commands for Managing Files and Getting Help,” when I described navigating the directory tree. There, the . (dot) referred to the current working directory. Notice the similarities? In ed, the dot refers to the current line.


So far, I’ve just started editing a file, and without doing anything, I’m on the last line of the file.

Up to now, everything I’ve entered in ed has been a command. With all of the typing I’ve done so far, I have not added any text to the file. That’s because I’m in command mode. In command mode, ed is waiting for me to enter a command. To add text to a file, I need to switch from command mode to input mode. To switch, I’ll enter the command i (insert). Start by opening a file:

ed file2 <cr> 

The system displays the number of characters:

173 

Print the contents of the current line by entering the p command (we’re still in command mode):

p <cr> 

The system displays the current line:

vi rules! 

Now, enter i and press Return to get into input mode and start entering text:

i <cr> 
This is a new line of inserted text 

That wasn’t too bad. Now here’s the tricky part: It’s not easy to tell when you’ve ended text input mode and are back at the stage of typing ed commands. All text input must be terminated with a . (dot) at the very beginning of a line and immediately followed by a carriage return. There can be no spaces or other characters on the line either before or after the dot. To make matters worse, there is nothing to tell you that you are out of input mode and back into command mode. One way I like to verify that I am in command mode and out of input mode is to type a p to print the current line. If it displays the line, I’m in command mode.

So, to get out of input mode, enter a Return after the line of text you’ve just input followed by a . (dot), as follows:

This is a new line of inserted text<cr> 
.<cr> 

Now type p to print the line. I like to give the p command a range of lines to print by typing the following:

1,$p <cr> 

This tells ed to print line 1 thru the last line (indicated by the $). The system responds with the following:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
This is a new line of inserted text 
vi rules! 

I see a problem. The line of text I just added is not where I wanted it. If the current line read vi rules! and I insert another line, the line gets inserted before the current line. I need to issue the a (append) command to append a new line after the current line, but what is my current line? Type p to find out:

p <cr> 

The system displays the following:

vi rules! 

So now I type a to append a new line:

This is a new line of APPENDED text<cr> 
.<cr> 
1,$p<cr> 

The system prints out all lines of text, as follows:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
This is a new line of inserted text 
vi rules! 
This is a new line of APPENDED text 

Now I need to delete the line that was put before the vi rules! line. I’ll use the d command to do that, but first I need to get to the line I want to delete. I’ll determine which line is my current line by typing p:

p<cr> 

The system displays the following:

This is a new line of APPENDED text 

I need to go to line 5, so I’ll type this:

5<cr> 

The system displays the new current line:

This is a new line of inserted text 

I’ll remove it by typing d, which deletes the current line:

d<cr> 
1,$p<cr> 

The system displays the following:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
vi rules! 
This is a new line of APPENDED text 

The line is gone.

Each ed command has the following syntax:

[line[,line]]operation[parameter] 

Therefore, I could have deleted the line by specifying the line address:

5d<cr> 

The system would have deleted line 5 regardless of what the current line was. When one-line address is specified, the command is performed on that specific line. If a two-line address is specified, the command is performed on all lines in the range, as follows:

1,5d<cr> 
1,$p<cr> 

The system displays the following:

vi rules! 
This is a new line of APPENDED text 

Lines 1 through 5 were deleted.

Note

As shown earlier with the p command, the $ could be used as a line address and would indicate the last line of the file, regardless of how many lines are in the file.


The most common use of the ed editor is to quickly find and replace several words or characters in a file. We can do this by combining two commands, the s (substitute) command and the g (global) command. Let’s go back to our original file, which looks like this:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
vi rules! 

To substitute the first occurrence of the word ed, issue the following command:

      ed file2<cr> 
      1<cr> 
      s/ed/vi/ 
      1,$p<cr> 
This is a file namvi file2. 
It has only a few lines of text. 
We will use it to demonstrate the ed facility. 
I don't really like the ed facility, I like vi better. 
vi rules! 

Not the results we might have expected, but ed performed exactly what we told it to.

I’m now going to substitute every occurrence of the word ed with vi.We call this a global substitution:

      ed file2<cr> 
      1,$s/ ed / vi / 
1,$p 

The system displays the following:

This is a file named file2. 
It has only a few lines of text. 
We will use it to demonstrate the vi facility. 
I don't really like the vi facility, I like vi better. 
vi rules! 

Note

Notice that this time I used spaces before and after ed and vi in the substitution command so that the word named would not get changed.


To save the modifications to the file, use the w (write) command as follows:

w<cr> 

The system will respond with the new number of characters in the file:

173 

If a filename is stated with the w (write) command as follows:

w /tmp/file3<cr> 

the contents of the buffer will be saved as /tmp/file3. Another way to save the contents of the buffer is to specify the line-address with the w (write) command, as follows:

1,5w /tmp/file3<cr> 

Lines 1 through 5 are saved to a file named /tmp/file3. After saving the file, you can then exit the file by typing q (quit), and you’ll be returned to the system prompt:

q<cr> 
# 

Note

The q (quit) command does not automatically write the contents of the buffer to disk. To capture any changes made during the editing session, an explicit w (write) command must be given. As a safeguard, ed makes a note of whether the editing session has changed the buffer in any way. If a q (quit) command has been entered without having previously written the buffer to disk and the buffer has been changed, ed responds with its usual ? (question mark) and will not let you out of the file. If you issue a second q (quit), you’ll force ed to exit to the shell.


Several more commands can be used in the ed editor to navigate, change, and delete lines of a file, but realistically, you will rarely use the ed editor.

The Solaris editors red, sed, edit, and ex are all variants of the ed editor and work in much the same manner.

The red utility is a restricted version of ed. It will only allow editing of files in the current directory. It prohibits executing shell commands via !<shell command>. Any attempts to bypass these restrictions result in an error message that states ?.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset