Previous Table of Contents Next


Module 39
ed

DESCRIPTION

The external ed command is a line editor. It is the standard text editor for UNIX. A line editor displays a prompt while waiting for you to enter editing commands. It does not display a screen full of text for you to move the cursor around on and make changes. Instead, you display text with an edit command, then use other edit commands to change existing text or add new text.

You may wonder why anyone would still want to learn and use a line editor. There are a couple of reasons. The first is you may need a shell script to manipulate the contents of a file. Your choices are to use ed, nawk, or sed. But remember, ed is the base of the other two, and the benefit of using ed is you read and write in the same file. It takes time to read a file with nawk, redirect the output to another file, then move the file back to the original filename. The other reason for using ed is speed. It is fast compared to a full-screen editor when you need to perform simple known changes to text -- especially on extremely large files.

COMMAND FORMAT

Following is the general format of the ed command.

ed  [ -p prompt] [ -s ] [ -x ] [ -C ] file
red [ -p prompt] [ -s ] [ -x ] [ -C ] file

BSD (Berkeley)
ed [ - ] [ -x ] file

Options

The following list describes the options and their arguments that may be used to control how ed functions.

-C Same as -x below except all text read into the buffer is assumed to be encrypted, unless a null key is specified.
-p prompt The -p specifies that a user-defined prompt follows. This allows you to define your own for an internal edit command prompt. For example,
$ ed -p "infile: " infile
infile: _
causes ed to prompt you with infile: instead of the default : (colon) prompt.
-s Suppresses the printing of information type messages. Character counts from the e, r, and w commands are suppressed. The diagnostics from the e and q commands are suppressed. The ! prompt displayed after a !command is also suppressed. On versions prior to SVR4 this was the - option.
-x An encrypted file is to be edited. The file is encrypted when it is saved to disk. It is decrypted when it is read into the editing buffer. You are required to enter an encryption key each time you start editing the file.

NOTE:  
The -x option is part of the Security Administration Utilities. These utilities are only available in the United States.




NOTE:  
The ed editor no longer supports the - option. The - option has been replaced with the -s option. System V only.




BSD (Berkeley)
- Same as System V -s option.
-x Same as System V -x option.

Arguments

The following argument may be passed to the ed command.

file The name of the file you want to edit.

FUNCTIONAL OVERVIEW

ed does not actually edit the specified file. It edits a copy of the file it has read into a temporary work file called a buffer. Thus all the edit commands you perform have no effect on the original file until you write the buffer back to the original file using the w command.

The red editor is a restricted version of ed. It only allows editing of files in the current directory. It does not allow you to escape to the shell from within the editor. If you do try to perform these types of commands, red responds with the message "restricted shell."

The tab formatting capability is supported by both ed and red. You must first perform a stty -tabs or a stty tab3, which sets your terminal mode for tab expansion. Then if the first line of the file being edited is a tab stop definition as defined by the tabs command (see Module 130), ed expands all tabs to the corresponding positions. For example, if the first line of your file is

  :t4,8,12,16,20,24,28,32,36

tab stops are set at columns 4, 8, 12, and so on. Tabs embedded with the input of an a, c, and i command are not expanded according to the definition on the first line but, instead, to the default of every eight spaces.

MODES

The ed editor has two modes: the input (insert) and command mode. In input mode the editor reads text (input) from your keyboard or a command file and inserts the text into the work buffer. In command mode the editor reads commanda (input) from your keyboard or a command file and performs the requested editing command. To enter insert mode you use one of the insert commands, a, c, or i. To exit insert mode and return to command mode you enter a . at the beginning of the line followed by a Return. When you enter the editor you are placed in command mode on the last line of the buffer.

REGULAR EXPRESSIONS

The following table contains each regular expression and the task that it performs when used inside a pattern. A regular expression consists of regular alphanumeric characters matching themselves and special characters matching certain patterns of text. Regular Expressions are often referred to as REs in UNIX terminology. Thus we use the RE notation for uniformity and briefness.


Alphanumeric
RE Description

c Matches the character c.
string Matches the set of characters string.

Metacharacters

Metacharacters are the special characters used in regular expression patterns that have special meanings. Metacharacters are often referred to as special or magic characters or wild cards.


Special
RE Description

\ Escapes the meaning of a metacharacter.
^ Matches the beginning of the line.
$ Matches the end of the line.
. Matches any single character.
[class] A character class. Matches any one character in the class.
[c1-c2] Match any one of the ASCII characters in the range defined within the brackets.
[^class] Do NOT match any of the ASCII characters listed within the brackets. Ranges may be specified.
RE* Matches zero or more occurrences of the preceding regular expression.
\(\) Group the regular expression RE for later reference; each parenthetical RE is referenced by a corresponding number (see \n). The first group is referenced by \1, the second group by \2, and so on. You can have a maximum of nine groups in one regular expression pattern.
\n Matches the nth grouped \(RE\) within the same regular expression.
RE\{m\} Matches exactly m occurrences of the preceding one-character RE.
RE\{m,\} Matches m or more occurrences of the preceding one-character RE.
RE\{m,n\} Matches m through occurrences of the preceding one-character RE.
// The last RE is used if it has been defined.

To have a metacharacter interpreted as a normal character precede it with a backslash (\).

ADDRESSING

The ex editor has many ways of addressing lines of text within the current session buffer. The addresses should always precede the command they are affecting.

Basic Addressing The following table describes the basic addressing syntax available for your use.


Address
Symbol Description

If no address is specified, the current line is affected or used for the address.
. Refers to the current line of the buffer. When you enter ed, the last line is the current line.
$ The last line of the buffer.
; Refers to the current line to the end of the buffer (.,$).
>
, Refers to line one to the current line (1,.).
<
n Refers to line number n.
+ Refers to the next line.
+n Refers to n lines forward.
^ Refers to the previous line.
-
-n Refers to n lines backward.
/pat/ Searches forward for the next line containing the string pat.
?pat? Searches backward for the first previous line containing the string pat.
// Repeats the last forward search.
?? Repeats the last backward search.
'x Refers to line with mark x. The mark must be lowercase.
line Any of the above basic addresses.

Address Ranges

Some ex commands accept ranges of lines to work with. The following table describes the available ranges.


Address
Symbol Description

a1,a2 Specifies a range of lines beginning at line address a1and continuing through line address a2. Any of the basic addresses are valid. But a1must address a line that precedes address a2. For example,
/John/,/Mary/ d
deletes all lines from the line where the string John was found to the line where the string Mary was found. This assumes John resides on a line previous to the line where Mary resides.
a1;a2 Specifies a new current line and the number of lines to offset from the new current line. The address a1 becomes your new current line and a2 becomes the number of lines past the current line. For example,
10;5 co 50
copies line 10 through 15 to line 50. The 10 sets your current line to line 10 and the 5 includes the next 5 lines.
range Any of the above addresses.

NOTE:  
If NO address is specified in an ed edit command, the current line is usually the default.



EDIT COMMANDS

Each edit command you enter in ed must be followed by pressing Return. Some of the edit commands assume default addresses if you do not specify an address on the command line. These default addresses are shown in [ ] (brackets). The following table describes each of the edit commands.


Previous Table of Contents Next