ed or red Command

Purpose

Line editor for text files.

Syntax

ed [ -p String] [ -s | -] [File]

red [ -pString] [ -s | -] [File]

Description

The ed command starts the ed editor line-editing program. The ed editor works on only one file at a time by copying it into a temporary edit buffer and making changes to that copy. The ed editor is part of a family of editors that also includes the edit editor, ex editor, and vi editor. The ed editor makes the changes you specify in a buffer. It does not alter the file itself until you use the write (w) subcommand.

You can specify the name of the file you want to edit when you start the ed editor with the ed command, or you can use the e subcommand. When the ed command reads a new file into the buffer, the contents of that file replace the buffer's previous contents.

The red command is a restricted version of the ed command, for use with the restricted shell (rsh). With the red command, you edit only files that reside in the current directory or in the /tmp directory; you cannot use the ! subcommand.

An ed editor subcommand consists of zero, one, or two addresses, followed by a single-character subcommand, followed by optional parameters to that subcommand. The addresses specify one or more lines in the buffer. Because every subcommand has default addresses, it is frequently unnecessary to specify addresses.

The ed editor allows editing only the current line unless you address another line in the buffer. You can move and copy only complete lines of data. The ed editor is useful for editing large files or for editing within a shell program.

The ed editor operates in one of two modes:

Item Description
command mode In command mode, the ed editor recognizes and runs subcommands. When you start the ed editor, it is in command mode. Type a . (period) and press Enter to confirm that you are in command mode.
text input mode In text input mode, the ed editor allows you to enter text into the file buffer but does not recognize subcommands. You enter text input mode by using the asubcommand, c subcommand, or i subcommand. You exit text input mode and return to the command mode by typing a . (period) alone at the beginning of a line. To place a . (period) into the buffer while in text input mode, enter a character followed by the . (period). Then, exit text input mode and use the s subcommand to remove the character.

The following list provides the maximum limits of the ed editor.

The maximum number of lines depends on the amount of memory available. The maximum file size depends on the amount of physical data storage (disk or tape drive) available or on the maximum number of lines permitted in user memory.

Flags

Item Description
-p String Sets the editor prompt to the String parameter. The default for String is a null value (no prompt).
-s Suppresses character counts that the editor displays with the e subcommand, r subcommand, and w subcommand. This flag also suppresses diagnostic messages for the e subcommand and the q subcommand, and suppresses the ! (exclamation point) prompt after an ! subcommand.
- Provides the same functions as the -s flag.

Pattern Matching

The ed editor supports a limited form of special pattern-matching characters that you can use as regular expressions (REs) to construct pattern strings. You can use these patterns in addresses to specify lines and in some subcommands to specify portions of a line.

Regular Expressions

The following REs match a single character or a collating element as follows:

Item Description
Character Matches itself and can be any ordinary character (other than one of the special pattern-matching symbols).
. Matches any single character except the new-line character.
[String] Matches any one character in the string. Certain pattern-matching characters have special meanings within brackets as follows:
^
Matches any character except the characters in the String parameter and the new-line character if the first character of the String parameter is a ^ (circumflex). This condition is true only if the ^ is the first character in the string,[^String].
-
Indicates a range of consecutive ASCII characters according to the current collating sequence. For example, [a-f] can be equivalent to [abcdef] or [aAbBcCdDeEfF] or [abcdef] and could even include accented a and e characters. A collating sequence can define equivalence classes for characters.

The minus sign loses its significance if it occurs as the first character in the string, [-String]; if it immediately follows an initial circumflex, [^-String]; or if it appears as the last character in the string, [String-].

]
Functions as a part of the string rather than as the string terminator, when the ] (right bracket) is the first character in the string, []String], or when it immediately follows an initial circumflex, [^]String].

Forming Patterns

The following rules describe how to form patterns from REs:

Restricting What Patterns Match

You can restrict a pattern to match only the first segment of a line, the final segment, or the entire line. The null pattern, // (two slashes), duplicates the previous pattern.

Matching the First Segment of a Line

The ^Pattern parameter matches only a string that begins in the first character position on a line.

Matching the Last Segment of a Line

The Pattern$ parameter matches only a string that ends with the last character (not including the new-line character) on a line.

Matching the Entire Line

The ^Pattern$ parameter restricts the pattern to match an entire line.

Addressing Lines

The ed editor uses three types of addresses: line number addresses, addresses relative to the current line, and pattern addresses. The current line (usually the last line affected by a subcommand) is the point of reference in the buffer.

You can use line addressing to do the following:

Subcommands that do not accept addresses regard the presence of an address as an error. Subcommands that accept addresses can use either given or default addresses. When given more addresses than it accepts, a command uses the last (rightmost) ones.

In most cases, commas (,) separate addresses (for example 2,8). Semicolons (;) also can separate addresses. A semicolon between addresses causes the ed editor to set the current line to the first address and then calculate the second address (for example, to set the starting line for a search). In a pair of addresses, the first address must be numerically smaller than the second.

You can use line numbers and symbolic addresses to perform the following tasks:

Addressing the Current Line

A . (period) addresses the current line. The . (period) is the default for most ed editor subcommands and does not need to be specified.

Addressing a Line by Number

To address a specified line of the buffer, type:

Number

where the Number parameter represents a line number. For example:

2253

addresses line number 2253 as the current line.

Addressing the Line before the First Line

To address the line before the first line of the buffer, type:

0

Addressing the Last Line

To address the last line of the buffer, type:

$

Addressing a Line above an Addressed Line

To specify an address that is a specified number of lines above the current line, type:

-Number

where the Number parameter is the specified number of lines above the current line that you want to address. For example:

-5

addresses the line five lines above the current line as the current line.

You also can specify only a - to address the line immediately above the current line. The minus sign has a cumulative effect. For example, the address - - (two minus signs) addresses the line two lines above the current line.

Addressing a Line below an Addressed Line

To specify an address that is a specified number of lines below the current line, type:

+Number

where the Number parameter is the specified number of lines below the current line that you want to address. The + (plus sign) is optional. For example:

+11

addresses the line 11 lines below the current line as the current line.

You also can specify only a + to address the line immediately below the current line. The + has a cumulative effect. For example, the address + + (two plus signs) addresses the line two lines below the current line.

Addressing the First Line through the Last Line

To address the first line through the last line, type:
,

The , (comma) represents the address pair 1,$ (first line through last line). The first line becomes the current line.

Addressing the Current Line through the Last Line

To address the current line through the last line, type:
;

The ; (semicolon) represents the address pair .,$ (current line through last line).

Addressing a Group of Lines

To address a group of lines, type:
FirstAddress,LastAddress
where the FirstAddress parameter is the line number (or symbolic address) of the first line in the group you want to address, and the LastAddress parameter is the line number (or symbolic address) of the last line in the group. The first line in the group becomes the current line. For example:
3421,4456

addresses the lines 3421 through 4456. Line 3421 becomes the current line.

Addressing the Next Line That Contains a Specified Pattern

To address the next line that contains a matching string, type:
/Pattern/
where the Pattern parameter is a character string or regular expression. The search begins with the line after the current line and stops when it finds a match for the pattern. If necessary, the search moves to the end of the buffer, wraps around to the beginning of the buffer, and continues until it either finds a match or returns to the current line. For example:
/Austin, Texas/
addresses the next line that contains Austin, Texas as the current line.

Addressing the Previous Line That Contains a Specified Pattern

To address the previous line that contains a match for the pattern, type:

?Pattern?
where the Pattern parameter is a character string or regular expression. The ?Pattern? construction, like /Pattern/, can search the entire buffer, but it searches in the opposite direction. For example:
?Austin, Texas?

addresses the previous line that contains Austin, Texas as the current line.

Addressing a Marked Line

To address a marked line with the k subcommand, type:

'x
where the x parameter is a lowercase letter a to z. For example:
'c
addresses the line marked as c with the k subcommand.

Subcommands

Use the ed editor subcommands to perform the following actions:

In most cases, you can enter only one ed editor subcommand on a line. However, you can add the l (list) and p (print) subcommands to any subcommand except the e (edit), E (Edit), f (file), q (quit), Q (Quit), r (read), w (write), and ! (operating system commands) subcommands.

The e, f, r, and w subcommands accept file names as parameters. The ed editor stores the last file name used with a subcommand as a default file name. The next e, E, f, r, or w subcommand given without a file name uses the default file name.

The ed editor responds to an error condition with one of two messages: ? (question mark) or ?File. When the ed editor receives an Interrupt signal (the Ctrl-C key sequence), it displays a ? and returns to command mode. When the ed editor reads a file, it discards ASCII null characters and all characters after the last new-line character.

Editing a File

You can use the ed editor subcommands to perform the following tasks:

Adding Text

Item Description
(.)a [l] [n] [p] Text. The a (append) subcommand adds text to the buffer after the addressed line. The a subcommand sets the current line to the last inserted line, or, if no lines were inserted, to the addressed line. A 0 address adds text to the beginning of the buffer.

Type the l (list), n (number), or p (print) optional subcommand if you want to display the added text.

Type your text, pressing the Enter key at the end of each line. If you do not press Enter at the end of each line, the ed editor automatically moves your cursor to the next line after you fill a line with characters. The ed editor treats everything you type before you press Enter as one line, regardless of how many lines it takes up on the screen.

Type a . (period) at the start of a new line, after you have typed all of your text.

(.)i [l] [n] [p]Text. The i (insert) subcommand inserts text before the addressed line and sets the current line to the last inserted line. If no lines are inserted, the i subcommand sets the current line to the addressed line. You cannot use a 0 address for this subcommand.

Type the l (list), n (number), or p (print) optional subcommand if you want to display the inserted text.

Type your text, pressing the Enter key at the end of each line. If you do not press Enter at the end of each line, the ed editor automatically moves your cursor to the next line after you fill a line with characters. The ed editor treats everything you type before you press Enter as one line, regardless of how many lines it takes up on the screen.

Type a . (period) at the start of a new line, after you have typed all of your text.

Note: The i subcommand differs from the a subcommand only in the placement of the text.

To Add Text after the Current Line

  1. Type the following subcommand:
    a[l][n][p]
    where l, n, and p are optional subcommands that display the added text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Add Text before the Current Line

  1. Type the following subcommand:
    i[l][n][p]
    where l, n, and p are optional subcommands that display the added text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Add Text after an Addressed Line

  1. Type the following subcommand:
    Addressa[l][n][p]
    where the Address parameter is the line number of the line that the inserted text should follow. The l, n, and p optional subcommands display the added text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Add Text before an Addressed Line

  1. Type the following subcommand:
    Addressi[l][n][p]
    where the Address parameter is the line number of the line that the inserted text should precede. The l, n, and p optional subcommands display the added text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Add Text after Lines That Contain a Search Pattern

  1. Type the following subcommand:
    [Address]g/Pattern/a[l][n][p]
    where Address is an optional parameter that specifies the range of lines to search for the pattern specified in the Pattern parameter. The Pattern parameter is a character string or regular expression. If you omit the Address parameter, the ed editor searches the entire file for lines that contain the pattern. The l, n, and p optional subcommands display the added text.
  2. Type a backslash:
    \
  3. Type the text. To start new lines within the added text, type a backslash:
    \
    and press Enter. The text you type is added after every line that contains the pattern specified in the command.
  4. To return to command mode, press Enter.

To Add Text before Lines That Contain a Search Pattern

  1. Type the following subcommand:
    [Address]g/Pattern/i[l][n][p]
    where Address is an optional parameter that specifies the range of lines to search for the pattern specified in the Pattern parameter. The Pattern parameter is a character string or regular expression. If you omit the Address parameter, the ed editor searches the entire file for lines that contain the pattern. The l, n, and p optional subcommands display the added text.
  2. Type a backslash:
    \
  3. Type the text. To start new lines within the added text, type a backslash:
    \
    and press Enter. The text you type is added before every line that contains the pattern specified in the command.
  4. To return to command mode, press Enter.

To Add Text after Lines That Do Not Contain a Search Pattern

  1. Type the following subcommand:
    [Address]g/Pattern/a[l][n][p]
    where Address is an optional parameter that specifies the range of lines to search for lines that do not contain the pattern specified in the Pattern parameter. The Pattern parameter is a character string or regular expression. If you omit the Address, the ed editor searches the entire file for lines that do not contain the pattern. The l, n, and p optional subcommands display the added text.
  2. Type a backslash:
    \
  3. Type the text. To start new lines within the added text, type a backslash:
    \
    and press Enter. The text you type is added after every line that does not contain the pattern specified in the command.
  4. To return to command mode, press Enter.

To Add Text before Lines That Do Not Contain a Search Pattern

  1. Type the following subcommand:
    [Address]g/Pattern/i[l][n][p]
    where Address is an optional parameter that specifies the range of lines to search for lines that do not contain the pattern specified in the Pattern parameter. The Pattern parameter is a character string or regular expression. If you omit the Address parameter, the ed editor searches the entire file for lines that do not contain the pattern. The l, n, and p optional subcommands display the added text.
  2. Type a backslash:
    \
  3. Type the text. To start new lines within the added text, type a backslash:
    \
    and press Enter. The text you type is added before every line that does not contain the pattern specified in the command.
  4. To return to command mode, press Enter.

Changing Text

Item Description
(.,.)c [l] [n] [p]Text. The c (change) subcommand deletes the addressed lines you want to replace and then replaces them with the new lines you enter. The c subcommand sets the current line to the last new line of input, or, if no input existed, to the first line that was not deleted.

Type the l (list), n (number), or p (print) optional subcommand if you want to display the inserted text.

Type the new text, and press Enter at the end of each line. When you have entered all of the new text, type a . (period) on a line by itself.

You can change text in several different ways with the ed editor. Use the preceding format to perform the following editing tasks:

To Change the Text of the Current LIne

  1. Type the following subcommand:
    c[l][n][p]
    where l, n, and p are optional subcommands that display the changed text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Change the Text of a Line or Group of Lines

  1. Type the following subcommand:
    Addressc[l][n][p]
    where the Address parameter is the address of the line or group of lines to change. The l, n, and p optional subcommands display the changed text.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Change the Text of Lines That Contain a Specified Pattern

  1. Type the following subcommand:
    Addressg/Pattern/c[l][n][p]
    where the Address parameter is the address of the group of lines that you want to search for the pattern specified with the Pattern parameter. The l, n, and p optional subcommands display the changed text.
  2. Type a backslash:
    \
  3. Type the new text. To start new lines within the new text, type a backslash:
    \
    and press Enter.
  4. To return to command mode, press Enter again, type a . (period), and press Enter again.

To Change the Text of Lines That Do Not Contain a Specified Pattern

  1. Type the following subcommand:
    Addressv/Pattern/c[l][n][p]
    where the Address parameter is the address of the group of lines that you want to search for the pattern specified with the Pattern parameter. The l, n, and p optional subcommands display the changed text.
  2. Type a backslash:
    \
  3. Type the new text. To start new lines within the new text, type a backslash:
    \
    and press Enter.
  4. To return to command mode, press Enter again, type a . (period), and press Enter again.

Copying Text

Item Description
(.,.)tAddress [p] [l] [n] The t (transfer) subcommand inserts a copy of the addressed lines after the line specified by the Address parameter. The t subcommand accepts the 0 address to insert lines at the beginning of the buffer.

The t subcommand sets the current line to the last line copied.

Type the l (list), n (number), or p (print) optional subcommand if you want to display the transferred text.

Copying a line or a set of lines leaves the specified lines in their original location and puts a copy in the new location. You can select the lines to copy by specifying an address or pattern. Use the preceding format to perform the following editing tasks:

To Copy the Current Line

  1. Type the following subcommand:
    tAddress[l][n][p]
    where the Address parameter is the line number or symbolic address of the line you want a copy of the current line to follow. The l, n, and p optional subcommands display the copied line.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Copy Lines Specified by Address

  1. Type the following subcommand:
    LineNumbertDestinationAddress[l][n][p]
    where the LineNumber parameter is the address of the lines you want to copy, and the DestinationAddress parameter is the line you want the copy to follow. The l, n, and p optional subcommands display the copied line.
  2. Type the text, and press Enter.
  3. Type a . (period), and press Enter again to return to command mode.

To Copy Lines That Contain a Specified Pattern

Type the following subcommand:
[Address]g/Pattern/t[DestinationAddress][l][n][p]

where Address is an optional parameter that specifies the range of lines to search for lines that contain the specified pattern, the Pattern parameter is the text you are searching for, and the DestinationAddress is an optional parameter that identifies the line you want the copied text to follow. The l, n, and p optional subcommands display the copied line.

If you omit the Address parameter, the ed editor searches the entire file for lines that contain the pattern. If you omit the DestinationAddress parameter, the copied text is placed after the current line.

To Copy Lines That Do Not Contain a Specified Pattern

Type the following subcommand:
[Address]v/Pattern/t[DestinationAddress][l][n][p]

where Address is an optional parameter that specifies the range of lines to search for lines that do not contain the specified pattern, the Pattern parameter is the text, and the DestinationAddress is an optional parameter that identifies the line you want the copied text to follow. The l, n, and p optional subcommands display the copied line.

If you omit the Address parameter, the ed editor searches the entire file for lines that do not contain the pattern. If you omit the DestinationAddress parameter, the copied text is placed after the current line.

Deleting Text

Item Description
(.,.)d [l] [n] [p] The d (delete) subcommand removes the addressed lines from the buffer. The line after the last line deleted becomes the current line. If the deleted lines were originally at the end of the buffer, the new last line becomes the current line.

Type the l (list), n (number), or p (print) optional subcommand if you want to display the deletion.

To Delete the Current Line

Type the following subcommand:
d[l][n][p]
where l, n, and p are optional subcommands that display the deleted line.

To Delete a Line or Group of Lines

Type the following subcommand:
Addressd[l][n][p]
where the Address parameter is the line number or symbolic address of the lines you want to delete, and l, n, and p are optional subcommands that display the deleted line or lines.

To Delete a Line or Group of Lines That Contain a Specified Pattern

Type the following subcommand:
[Address]g/Pattern/d[l][n][p]
where Address is an optional parameter that specifies the line number or symbolic address of the lines you want to search, and the Pattern parameter is a character string or regular expression that represents the text you want to find. If you omit the Address parameter, the ed editor searches the entire file for lines that contain the specified pattern. The l, n, and p optional subcommands display the deleted line or lines.

To Delete a Line or Group of Lines That Does Not Contain a Specified Pattern

Type the following subcommand:
[Address]v/Pattern/d[l][n][p]
where Address is an optional parameter that specifies the line number or symbolic address of the lines you want to search, and the Pattern parameter is a character string or regular expression that represents the text you want to find. If you omit the Address parameter, the ed editor searches the entire file for lines that do not contain the specified pattern. The l, n, and p optional subcommands display the deleted line or lines.

To Delete Text from the Current Line

  1. Type the following subcommand:
    s/Pattern
    where the Pattern parameter is a character string or regular expression that represents the text you want to delete.
  2. To delete the first instance of the pattern from the line, type:
    //
    OR

    To delete every instance of the pattern from the line, type:

    //g
  3. If you want to display the deletion, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Delete Text within Selected Lines

  1. Type the address of a group of lines to select (or skip this step to select all lines).
  2. To select the lines indicated by the Pattern parameter in step 4, type:
    g
    OR

    To select the lines not indicated by the Pattern parameter in step 4, type:

    v
  3. To enter the text you want to search, type the following subcommand:
    /Pattern/s
    where the Pattern parameter is the text you want to search.
  4. Type one of the following commands to make the desired deletion:
    To delete the first instance of the Pattern parameter within each selected line, type:
    ///
    To delete every instance of the Pattern parameter within each selected line, type:
    ///g
    To delete the first specified number of occurrences of the Pattern parameter on each selected line (where the Number parameter is an integer), type:
    ///Number
    To delete the first character string indicated by the OtherPattern parameter within each line selected by the Pattern parameter (where the OtherPattern parameter is the pattern you want to search), type:
    /OtherPattern//
    To delete every instance of the OtherPattern parameter within each line selected by the Pattern parameter, type:
    /OtherPattern//g
    To delete the first specified number of occurrences of the OtherPattern parameter on each line selected by the Pattern parameter (where the Number parameter is an integer), type:
    /OtherPattern//Number
  5. If you want to display the deletion, type one of the following optional subcommands:

    l

    n

    p

  6. Press Enter.
For example, to delete all instances of a pattern from a range of lines, type:
38,$g/tmp/s/gn

The previous example searches all the lines from line 38 to the last line (38,$) for the tmp character string and deletes every instance (/g) of that character string within those lines. It then displays the lines that had text deleted from them and their line numbers (n).

To delete all instances of a pattern from all lines that contain that pattern, type:
g/rem/s///gl

The previous example searches the entire file (address parameter is omitted) for all lines that contain (g) the rem character string. It deletes all instances (///g) of the rem character string from each of those lines and then displays the lines that had text deleted from them, including the nonprinting characters in those lines (l).

To Delete Text from Addressed Lines

  1. Type the following subcommand:
    Addresss/Pattern
    Note: The Address parameter is followed by the s subcommand, where the Address parameter is the line number, range of line numbers, or symbolic address of the lines from which you want to delete the pattern, and the Pattern parameter is a character string or regular expression that represents the text you want to delete.
  2. To delete the first instance of the pattern from each line, type:
    //
    OR
    To delete every instance of the pattern from each line, type:
    //g
  3. If you want to display the deletion, type one of the following optional subcommands:

    l
      
    n
      
    p

  4. Press Enter.

To Delete Text from Lines That Contain a Specified Pattern

  1. Type the following subcommand:
    [Address]g/Pattern/s
    where Address is an optional parameter that specifies the line number, range of line numbers, or symbolic address of the lines that contains a specified pattern, and the Pattern parameter is a character string or regular expression that represents the text you want to find and delete. If you omit the Address parameter, the ed editor searches all lines in the file for the pattern.
  2. To delete the first instance of the pattern from each line that contains it, type:
    ///
    OR
    To delete every instance of the pattern from each line that contains it, type:
    ///g
  3. If you want to display the deletion, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Delete a Pattern from Lines That Contain a Different Specified Pattern

  1. Type the following subcommand:
    [Address]g/SearchPattern/s
    where Address is an optional parameter that specifies the line number, range of line numbers, or symbolic address of the lines that contains a specified pattern, and the SearchPattern parameter is a character string or regular expression that represents text that is in the lines you want to change. If you omit the Address parameter, the ed editor searches all lines in the file for the specified pattern.
  2. To specify the text you want to delete, type:
    /DeletePattern/
  3. To delete the first instance of the pattern from each line, type:
    /
    OR
    To delete every instance of the pattern from each line, type:
    /g
    Note: The entire subcommand string looks like this:
    [Address]g/SearchPattern/s/DeletePattern//[g]
  4. If you want to display the deletion, type one of the following optional subcommands:

    l

    n

    p

  5. Press Enter.
For example, to delete the first instance of a pattern from lines that contain a different specified pattern, type:
1,.g/rem/s/tmp//l

The previous example searches from the first line to the current line (1,.) for all lines that contain (g) the rem character string. It deletes the first instance of the tmp character string from each of those lines (/), then displays the lines that had text deleted from them, including the nonprinting characters in those lines (l).

To Delete a Pattern from Lines That Do Not Contain a Different Specified Pattern

  1. Type the following subcommand:
    [Address]v/SearchPattern/s
    where Address is an optional parameter that specifies the line number, range of line numbers, or symbolic address of the lines that contains a specified pattern, and the SearchPattern parameter is a character string or regular expression that represents text that is not in the lines you want to find and change. If you omit the Address parameter, the ed editor searches all lines in the file for the specified pattern.
  2. To specify the text you want to delete, type:
    /DeletePattern/
  3. To delete the first instance of the pattern, type:
    /
    OR

    To delete every instance of the pattern from each line, type:

    /g
    Note: The entire subcommand string looks like this:
    [Address]v/SearchPattern/s/DeletePattern//[g]
  4. If you want to display the deletion, type one of the following optional subcommands:

    l

    n

    p

  5. Press Enter.
For example, to delete the first instance of a pattern from lines that do not contain a specified pattern, type:
1,.v/rem/s/tmp//l

The previous example searches from the first line to the current line (1,.) for all lines that do not contain (v) the rem character string. It deletes the first instance of the tmp character string from each of those lines (/), then displays the lines that had text deleted from them, including the nonprinting characters in those lines (l).

Displaying Text

Item Description
(.,.)l The l (list) subcommand writes the addressed lines to standard output in a visually unambiguous form and writes the characters \\\, \\a, \\b, \\f, \\r, \\t, and \\v in the corresponding escape sequence. The lsubcommand writes nonprintable characters as one 3-digit octal number, with a preceding \ (backslash) for each byte in the character (most significant byte first).

The l subcommand wraps long lines, and you can indicate the wrap point by writing the \ (backslash)/new-line character sequence. Wrapping occurs at the 72nd column position. The $ (dollar sign) marks the end of each line. You can append the l subcommand to any ed editor subcommand except the e, E, f, q, Q, r, w, or ! subcommand. The current line number is set to the address of the last line written.

(.,.)n The n (number) subcommand displays the addressed lines, each preceded by its line number and a tab character (displayed as blank spaces); n sets the current line to the last line displayed. You can append the n subcommand to any ed editor subcommand except e, f, r, or w. For example, the dn subcommand deletes the current line and displays the new current line and line number.
(.,.)p The p (print) subcommand displays the addressed lines and sets the current line to the last line displayed. You can append the p subcommand to any ed editor subcommand except e, f, r, or w. For example, the dp subcommand deletes the current line and displays the new current line.
(.)= Without an address, the = (equal sign) subcommand displays the current line number. When preceded by the $ address, the = subcommand displays the number of the last line in the buffer. The = subcommand does not change the current line and cannot be appended to a g subcommand or v subcommand.

To Display an Addressed Line or Group of Lines

Type the following subcommand:
Addressp
where the Address parameter is the line number or symbolic address of the lines you want to display.

The line or lines addressed are displayed on the screen. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display an Addressed Line or Group of Lines and Their Nonprinting Characters

Type the following subcommand:
Addressl
where the Address parameter is the line number or symbolic address of the lines you want to display.

The line or lines addressed and their nonprinting characters are displayed on the screen. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display an Addressed Line or Group of Lines and Their Line Numbers

Type the following subcommand:
Addressn
where the Address parameter is the line number or symbolic address of the lines you want to display.

The line or lines addressed are displayed on the screen. The line number for each line is displayed beside the line. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Contain a Search Pattern

Type the following subcommand:
Addressg/Pattern/p
where the Address parameter is the range of lines and the Pattern parameter is the character string or regular expression that you want to search.

The line or lines that contain the specified pattern are displayed on the screen. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Contain a Search Pattern, Including Their Nonprinting Characters

Type the following subcommand:
[Address]g/Pattern/l
where Address is an optional parameter that specifies the range of lines and the Pattern parameter is the character string or regular expression that you want to search. If you omit the Address parameter, the ed editor searches the entire file.

The line or lines that contain the specified pattern are displayed on the screen. Nonprinting characters show up in the display. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Contain a Search Pattern, Including Their Line Numbers

Type the following subcommand:
[Address]g/Pattern/n
where Address is an optional parameter that specifies the range of lines and the Pattern parameter is the character string or regular expression that you want to search. If you omit the Address parameter, the ed editor searches the entire file.

The line or lines that contain the specified pattern are displayed on the screen. The line number for each line is displayed beside the line. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Do Not Contain a Search Pattern

Type the following subcommand:
[Address]v/Pattern/p
where Address is an optional parameter that specifies the range of lines and the Pattern parameter is the character string or regular expression that you want to search. If you omit the Address parameter, the ed editor searches the entire file.

The line or lines that do not contain the specified pattern are displayed on the screen. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Do Not Contain a Search Pattern, Including Their Nonprinting Characters

Type the following subcommand:
[Address]v/Pattern/l
where Address is an optional parameter that specifies the range of lines and the Pattern parameter is the character string or regular expression that you want to search. If you omit the Address parameter, the ed editor searches the entire file.

The line or lines that do not contain the specified pattern are displayed on the screen, including the nonprinting characters. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

To Display Lines That Do Not Contain a Search Pattern, Including Their Line Numbers

Type the following subcommand:
[Address]v/Pattern/n

where Address is an optional parameter that specifies the range of lines and the Pattern parameter is the character string or regular expression that you want to search. If you omit the Address parameter, the ed editor searches the entire file.

The line or lines that do not contain the specified pattern are displayed on the screen, along with their line numbers. If the group of lines is too long to fit on the screen, the ed editor displays as many as will fit, beginning with the first line addressed.

Joining and Splitting Lines

Item Description
(.,.+1)j [l] [n] [p] The j (join) subcommand joins contiguous lines by removing the intervening new-line characters. If given only one address, the j subcommand does nothing.

Type the l (list), n (number), or p (print) subcommand if you want to display the joined lines. These subcommands are optional.

The ed editor provides several ways to join or split a line. Use the preceding format to perform the following editing tasks:

To Join the Current and Next Lines

Type the following subcommand:
j[l][n][p]
where l, n, and p are optional subcommands that display the joined lines.

To Join Addressed Lines

Type the following subcommand:
Addressj[l][n][p]
where the Address parameter is a set of contiguous lines that will form one line, and l, n, and p are optional subcommands that display the joined lines.

To Split the Current Line

  1. To split the current line after a specified pattern, type the following subcommand:
    s/Pattern/Pattern\
    where the Pattern parameter is the character string that you want to split the line after.
    Note: Make sure that both strings represented by the Pattern parameter are exactly alike.
  2. Press Enter.
  3. Type the following backslash:
    /
  4. To display the split line, type one of the following optional subcommands:

    l

    n

    p

  5. Press Enter.

To Split an Addressed Line

  1. To split an addressed line after a specified pattern, type the following subcommand:
    Addresss/Pattern/Pattern\
    where the Address parameter is the address of the line to split, and the Pattern parameter is the character string to split the line after.
    Note: Make sure that both strings represented by the Pattern parameter are exactly alike.
  2. Press Enter.
  3. Type the following backslash:
    /
  4. To display the split line, type one of the following optional subcommands:

    l

    n

    p

  5. Press Enter.

Making Global Changes

Item Description
(1,$)g/Pattern/SubcommandList [l] [n] [p] The g (global) subcommand first marks every line that matches the Pattern parameter. The pattern can be a fixed character string or a regular expression. Then, for each marked line, this subcommand sets the current line to the marked line and runs the SubcommandList parameter. Enter a single subcommand or the first subcommand of a list of subcommands on the same line with the g subcommand; enter subsequent subcommands on separate lines. Except for the last line, each of the lines should end with a \ (backslash).

The SubcommandList parameter can include the a, i, and c subcommands and their input. If the last command in the SubcommandList parameter would usually be the . (period) that ends input mode, the . (period) is optional. If no SubcommandList parameter exists, the current line is displayed. The SubcommandList parameter cannot include the g , G, v, or V subcommand.

Type the l (list), n (number), or p (print) subcommand if you want to display the changes. These subcommands are optional.

Note: The g subcommand is similar to the v subcommand, which runs the SubcommandList parameter for every line that does not contain a match for the pattern.
(1,$)G/Pattern/ [l] [n] [p] The interactive G (Global) subcommand marks every line that matches the Pattern parameter, displays the first marked line, sets the current line to that line, and then waits for a subcommand. A pattern can be a fixed character string or a regular expression.

The G subcommand does not accept the a, i, c, g, G, v, and V subcommands. After the subcommand finishes, the G subcommand displays the next marked line, and so on. The G subcommand takes a new-line character as a null subcommand. A :& (colon ampersand) causes the G subcommand to run the previous subcommand again. You can stop the G subcommand by pressing Ctrl+C.

Type the l (list), n (number), or p (print) subcommand if you want to display the changes. These subcommands are optional.

(1,$)v/Pattern/SubcommandList [l] [n] [p] The v subcommand runs the subcommands in the SubcommandList parameter for each line that does not contain a match for the Pattern parameter. A pattern can be a fixed character string or a regular expression.

Type the l (list), n (number), or p (print) subcommand if you want to display the changes. These subcommands are optional.

The v subcommand does not accept the a, i, c, g, G, and V subcommands.

Note: The v subcommand complements the g subcommand, which runs the SubcommandList parameter for every line that contains a match for the pattern.
(1,$)V/Pattern/ [l] [n] [p] The V subcommand marks every line that does not match the Pattern parameter, displays the first marked line, sets the current line to that line, and then waits for a subcommand. A pattern can be a fixed character string or a regular expression.

Type the l (list), n (number), or p (print) subcommand if you want to display the changes. These subcommands are optional.

The V subcommand does not accept the a, i, c, g, G, and v subcommands.

Note: The V subcommand complements the G subcommand, which marks the lines that match the pattern.

Marking Text

Item Description
(.)kx [l] [n] [p] The k (mark) subcommand marks the addressed line with the name specified by the x parameter, which must be a lowercase ASCII letter. The address 'x (single quotation mark before the marking character) then addresses this line. The k subcommand does not change the current line.

Type the l (list), n (number), or p (print) subcommand if you want to display the marked text. These subcommands are optional.

To Mark the Current Line

Type the following subcommand:

kLetter[l][n][p]

where the Letter parameter is the letter a through z for a mark, and l, n, and p are optional subcommands that display the marked text.

To Mark an Addressed Line

Type the following subcommand:
AddresskLetter[l][n][p]
where the Address parameter is the line number or symbolic address of the line you want to mark, and the Letter parameter is the letter a through z for a mark. The l, n, and p optional subcommands display the marked text.

Moving Text

Item Description
(.,.)mA [l] [n] [p] The m (move) subcommand repositions the addressed line or lines. The first moved line follows the line addressed by the A parameter. A parameter of 0 moves the addressed line or lines to the beginning of the file. The address specified by the A parameter cannot be one of the lines to be moved. The m subcommand sets the current line to the last moved line.

Type the l (list), n (number), or p (print) subcommands if you want to display the deletion. These subcommands are optional.

Moving a line or a set of lines deletes the specified lines from their original location and places them in a new location. You can select which lines to move by address or pattern. Use the preceding format to perform the following editing tasks:

To Move the Current Line

Type the following subcommand:
mAddress[l][n][p]
where the Address parameter is the line number or symbolic address of the line you want the current line to follow, and l, n, and p are optional subcommands that display the moved line.

To Move Lines Specified by Address

Type the following subcommand:
LineNumbermDestinationAddress[l][n][p]
where the LineNumber parameter is the address of the lines you want to move, and the DestinationAddress parameter is the line you want the moved lines to follow. The l, n, and p optional subcommands display the moved lines.

To Move Lines That Contain a Specified Pattern

Type the following subcommand:
[Address]g/Pattern/m[DestinationAddress][l][n][p]
where Address is an optional parameter that specifies the range of lines to search for lines that contain the specified pattern, the Pattern parameter is the text you are searching for, and DestinationAddress is an optional parameter that represents the line you want the moved lines to follow. The l, n, and p optional subcommands display the moved lines.

If you omit the Address parameter, the ed editor searches the entire file for lines that contain the pattern. If you omit the DestinationAddress parameter, the moved text is placed after the current line.

To Move Lines That Do Not Contain a Specified Pattern

Type the following subcommand:
[Address]v/Pattern/m[DestinationAddress][l][n][p]
where Address is an optional parameter that specifies the range of lines to search for lines that do not contain the specified pattern, the Pattern parameter is the text, and DestinationAddress is an optional parameter that represents the line you want the moved text to follow. The l, n, and p optional subcommands display the moved lines.

If you omit the Address parameter, the ed editor searches the entire file for lines that do not contain the pattern. If you omit the DestinationAddress parameter, the moved text is placed after the current line.

Saving Text

Item Description
(1,$)w File The w (write) subcommand copies the addressed lines from the buffer to the file specified by the File parameter. If the file does not exist, the w subcommand creates it with permission code 666 (read and write permission for everyone), unless the umask setting specifies another file creation mode.

The w subcommand does not change the default file name (unless the File parameter is the first file name used since you started the ed editor). If you do not provide a file name, the w subcommand uses the default file name. The w subcommand does not change the current line.

If the ed editor successfully writes the file from the buffer, it displays the number of characters written. If you specify the ! Command subcommand instead of a file name, the w subcommand reads the output of the operating system command specified by the Command parameter. The w subcommand does not save the name of the operating system command you specified as a default file name.

Note: Because 0 is not a legal address for the w subcommand, you cannot create an empty file with the ed command.
You can save changes to a file in several ways. Use the preceding format to perform the following actions:

To Save a File to the Current File

Type the following subcommand:
w 

The current file is saved under its current name, and the ed editor displays the number of characters written.

To Save Part of a File to the Current File

Type the following subcommand:
Addressw 
where the Address parameter specifies the line or group of lines to write. The ed editor displays the number of characters written.

To Save a File to a Different File

Type the following subcommand:
w File
where the File parameter is the name of the file to write to.

The current file is saved to the file specified by the File parameter. The ed editor displays the number of characters written.

To Save Part of a File to a Different File

Type the following subcommand:
Addressw File

where the Address parameter specifies the line or group of lines to write and the File parameter specifies the file to write to.

The specified lines are saved to the file specified by the File parameter. The ed editor displays the number of characters written.

Searching Text

You can search forward or backward from the current line for a pattern of text. The pattern can be a character string or a regular expression made up of literal characters and the special characters ^ (circumflex), $ (dollar sign), . (period), [ (left bracket), ] (right bracket), * (asterisk), \ (backslash), % (percent sign), and the & key.

To Search Forward

Type the following subcommand:
/Pattern
where the Pattern parameter is a character string or regular expression that specifies the text to search for.

The cursor moves to the first character of the text specified by the pattern.

To Search Backward

Type the following subcommand:
?Pattern
where the Pattern parameter is a character string or regular expression that specifies the text to search for.

The cursor moves to the first character of the text specified by the pattern.

To Repeat a Search in the Same Direction

Type the following subcommand:
/

The cursor moves to the first character of the closest instance of the text specified by the pattern in the last search command.

To Repeat a Search in the Opposite Direction

Type the following subcommand:
?

The cursor moves to the first character of the closest instance of the text specified by the pattern in the last search command.

Substituting Text

Item Description
(.,.)s/Pattern/Replacement/ [l] [n] [p] (.,.)s/Pattern/Replacement/ng [l] [n] [p] The s (substitute) subcommand searches each addressed line for a string that matches the Pattern parameter and replaces the string with the specified Replacement parameter. A pattern can be a fixed character string or a regular expression. Without the global subcommand (g), the s subcommand replaces only the first matching string on each addressed line. With the g subcommand, the s subcommand replaces every occurrence of the matching string on each addressed line. If the s subcommand does not find a match for the pattern, it returns the error message ? (question mark).

Type the l (list), n (number), or p (print) subcommand to display the substituted text. These subcommands are optional.

Note: Any character except a space or a new-line character can separate (delimit) the Pattern and Replacement parameters. The s subcommand sets the current line to the last line changed.

If the Number parameter (an integer) is specified, then the first number that matches strings in each addressed line is replaced.

An & (ampersand) character used in the Replacement parameter has the same value as the Pattern parameter. For example, the subcommand s/are/&n't/ has the same effect as the subcommand s/are/aren't/ and replaces are with aren't on the current line. A \& (backslash, ampersand) removes the special meaning of the & character in the Replacement parameter.

A subpattern is part of a pattern enclosed by the strings \( (backslash, left parenthesis) and \) (backslash, right parenthesis); the pattern works as if the enclosing characters were not present. In the Replacement parameter, \Number refers to strings that match subpatterns. For example, the s/\(t\)\(h\) \(e\)/t\1\2ose) subcommand replaces the with those if a match for the pattern the exists on the current line. Whether subpatterns are nested or in a series, \Number refers to the occurrence specified by the Number parameter, counting from the left of the delimiting characters, \) (backslash, right parenthesis).

The % (percent sign), when used alone as the Replacement parameter, causes the s subcommand to repeat the previous Replacement parameter. The % does not have this special meaning if it is part of a longer Replacement parameter or if it is preceded by a \ (backslash).

You can split lines by substituting new-line characters into them. In the Replacement parameter. Pressing the \+Enter key sequence quotes the new-line character (not displayed) and moves the cursor to the next line for the remainder of the string. New-line characters cannot be substituted as part of a g subcommand or v subcommand list.

To Substitute Text within the Current Line

  1. Type the following subcommand:
    s/OldString/NewString
    where the OldString parameter is the existing text and the NewString parameter is the text you want to substitute for it.
  2. Type one of the following actions:
    To substitute the NewString parameter for the first instance of the OldString parameter within the current line, type:
    /
    To substitute the NewString parameter for every instance of the OldPattern parameter within the current line, type:
    /g
  3. To display the changed text, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Substitute Text within an Addressed Line or Group of Lines

  1. Type the following subcommand:
    Addresss/OldPattern/NewString
    where the Address parameter is the address of the line or group of lines where you want to substitute text, the OldPattern parameter is the existing text, and the NewString parameter is the text you want to substitute.
  2. Type one of the following actions:
    To substitute the NewString parameter for the first instance of the OldPattern parameter within each line, type:
    /NewString/
    To substitute the NewString parameter for every instance of the OldPattern parameter within each line, type:
    /NewString/g
    To substitute the NewString parameter for the first instance of the NumberOldPattern parameter on each address line, type:
    /NewString/Number
  3. To display the changed text, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Substitute a Specified Pattern within Lines That Contain That Pattern

  1. Type the following subcommand:
    Addressg/Pattern/s//NewString
    where the Address parameter is the address of the group of lines that you want to search for the pattern specified with the Pattern parameter, and the NewString parameter is the text you want to substitute for the Pattern parameter.
  2. Type one of the following actions:
    To substitute the NewString parameter for the first instance of the Pattern parameter within each line, type:
    /
    To substitute the NewString parameter for every instance of the Pattern parameter within each line, type:
    /g
  3. To display the changed text, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Substitute a Pattern within Lines That Contain a Different Pattern

  1. Type the following subcommand:
    Addressg/Pattern/s/OldString/NewString
    where the Address parameter is the address of the group of lines that you want to search for the pattern specified with the Pattern parameter, the OldString parameter is the text you want to replace, and the NewString parameter is the text you want to substitute in place of the OldString parameter.
  2. Type one of the following actions:
    To substitute the NewString parameter for the first instance of the OldString parameter within each line that contains the Pattern parameter, type:
    /
    To substitute the NewString parameter for every instance of the OldString parameter within each line that contains the Pattern parameter, type:
    /g
  3. To display the changed text, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

To Substitute a Pattern within Lines That Do Not Contain a Different Pattern

  1. Type the following subcommand:
    Addressv/Pattern/s/OldString/NewString
    where the Address parameter is the address of the group of lines that you want to search for the pattern specified with the Pattern parameter, the OldString parameter is the text you want to replace, and the NewString parameter is the text you want to substitute in place of the OldString parameter.
  2. Type one of the following actions:

    To substitute the NewString parameter for the first instance of the OldString parameter within each line that does not contain the Pattern parameter, type:

    /
    To substitute the NewString parameter for every instance of the OldString parameter within each line that does not contain the Pattern parameter, type:
    /g
  3. To display the changed text, type one of the following optional subcommands:

    l

    n

    p

  4. Press Enter.

Undoing Text Changes

Item Description
u [l] [n] [p] The u (undo) subcommand restores the buffer to the state it was in before it was last modified by an ed editor subcommand. The u subcommand cannot undo the e, f, and w subcommands.

Type the l (list), n (number), or p (print) subcommand if you want to display the changes. These subcommands are optional.

To Undo Text Changes

Type the following subcommand:
u[l][n][p]

where l, n, and p are optional subcommands that display the changes. All add, change, move, copy, or delete editing functions performed to the text after the last save are undone.

Manipulating Files

You can use ed editor subcommands to manipulate files to perform the following tasks:

Adding Another File to the Current File

Item Description
($)r File The r (read) subcommand reads a file into the buffer after the addressed line. The r subcommand does not delete the previous contents of the buffer. When entered without the File parameter, the r subcommand reads the default file, if any, into the buffer. The r subcommand does not change the default file name.

A 0 address causes the r subcommand to read a file in at the beginning of the buffer. After it reads a file successfully, the r subcommand displays the number of characters read into the buffer and sets the current line to the last line read.

If ! (exclamation point) replaces the File parameter in an r subcommand, the rest of the line is taken as an operating system shell command whose output is to be read. The r subcommand does not store the names of operating system commands as default file names.

To Insert a File after the Current Line

Type the following subcommand:
r File
where the File parameter is the name of the file to be inserted.

The ed editor reads the file specified by the File parameter into the current file after the current line and displays the number of characters read into the current file.

To Insert a File after a Line Specified by Address

Type the following subcommand:

Addressr File

where the Address parameter specifies the line that you want the inserted file to follow, and the File parameter is the name of the file to be inserted.

The ed editor reads the file specified by the File parameter into the current file after the specified line and displays the number of characters read into the current file.

Changing the Default File Name

Item Description
f [File] The f (file name) subcommand changes the default file name (the stored name of the last file used) to the name specified by the File parameter. If a File parameter is not specified, the f subcommand displays the default file name. (The e subcommand stores the default file name.)

To Display the Name of a File

Type the following subcommand:
f

The ed editor displays the name of the file in the edit buffer.

To Name a File

Type the following subcommand:
f File
where the File parameter is the new name for the file in the edit buffer.

The file in the edit buffer is renamed.

Editing Additional Files

Item Description
e File The e (edit) subcommand first deletes any contents from the buffer, sets the current line to the last line of the buffer, and displays the number of characters read into the buffer. If the buffer has been changed since its contents were saved (with the w subcommand), the ed editor displays a ? (question mark) before it clears the buffer.

The e subcommand stores the File parameter as the default file name to be used, if necessary, by subsequent e, r, or w subcommands. (To change the name of the default file name, use the f subcommand.)

When an ! (exclamation point) replaces the File parameter, the e subcommand takes the rest of the line as an operating system shell command and reads the command output. The e subcommand does not store the name of the shell command as a default file name.

E File The E (Edit) subcommand works like the e subcommand with one exception; the E subcommand does not check for changes made to the buffer after the last w subcommand. Any changes you made before re-editing the file are lost.

You can use the e or E subcommands to perform the following tasks:

To Re-Edit the Current File without Saving It

Type the following subcommand:
E 

The ed editor displays the number of characters in the file. Any changes you made before re-editing the file are lost.

To Re-Edit the Current File after Saving It

Type the following subcommand:
e

The ed editor displays the number of characters in the file.

To Edit a File after the Current File Is Saved

Type the following subcommand:
e File

where the File parameter is the name of a new or existing file that you want to edit.

For an existing file, the ed editor displays the number of characters in the file. For a new file, the ed editor displays a ? (question mark) and the name of the file.

To Edit a File without Saving the Current File

Type the following subcommand:
E File
where the File parameter is the name of a new or existing file that you want to edit.

For an existing file, the editor displays the number of characters in the file. For a new file, the ed editor displays a ? (question mark) and the name of the file.

Miscellaneous Functions of the ed Editor Subcommands

You can use ed editor subcommands to perform the following tasks:

Changing the Prompt String

Item Description
P The P (Prompt) subcommand turns on or off the ed editor prompt string, which is represented by an * (asterisk). Initially, the P subcommand is turned off.

To Start or Stop Displaying the Prompt String

Type the following subcommand:
P

The ed editor prompt, an * (asterisk), is displayed or not displayed, depending on its previous setting.

Entering System Commands

Item Description
! Command The ! subcommand allows you to run operating system commands without leaving the ed editor. Anything that follows the ! subcommand on an ed editor subcommand line is interpreted as an operating system command. Within the text of that command string, the ed editor replaces the unescaped % (percent sign) with the current file name, if one exists.

You can repeat the previous operating system command by entering an ! (exclamation point) after the ! ed editor subcommand. If the operating system command interpreter (the sh command) expands the command string, the ed editor echoes the expanded line. The ! subcommand does not change the current line.

To Run One Operating System Command

Type the following subcommand:
!Command
where the Command parameter specifies an operating system command usually entered at the prompt.

The command runs and displays its output. After the command completes, the editor displays an ! (exclamation point).

To Repeat an Operating System Command

Type the following subcommand:
!

The previously run operating system command runs and displays its output. After the command completes, the editor displays an ! (exclamation point).

To Run Several Operating System Commands

  1. Type the following subcommand to display an operating system prompt:
    !sh
  2. Type an operating system command.
  3. Press Enter to run the command and display its output.
  4. Repeat steps 2 and 3 to run more operating system commands.
  5. Press Ctrl+D to return to command mode. The editor displays an ! (exclamation point).

Exiting the ed Editor

Item Description
q The q (quit) subcommand exits the ed editor after checking whether the buffer has been saved to a file after the last changes were entered. If the buffer has not been saved to a file, the q subcommand displays the ? (question mark) message. Enter the q subcommand again to exit the ed editor anyway. The changes to the current file are lost.
Q The Q (Quit) subcommand exits the ed editor without checking whether any changes were made since the buffer was saved to a file. Any changes made to the buffer since the last save are lost.

To Quit after Checking for Edits

  1. Type the following subcommand:
    q
  2. If the ed editor displays a ?, type one of the following subcommands:
    To save changes before quitting, type:
    w
    then press Enter.
    To quit without saving changes, type:
    q
  3. Press Enter.

To Quit and Discard Edits

  1. Type the following subcommand:
    Q
  2. Press Enter. Any changes made to the buffer since the last save are lost.

Requesting Help

Item Description
h The h (help) subcommand provides a brief help message for the most recent ? diagnostic or error message displayed.
H The H (Help) subcommand causes the ed editor to display help messages for all subsequent ? diagnostic messages. The H subcommand also explains the previous ? if one existed. The H subcommand alternately turns this mode on and off; it is initially off.

To Start or Stop Displaying Help Messages

Type the following subcommand:
H

The help messages are displayed or not displayed for ? responses from the ed editor, depending on the previous setting.

To Display the Last Help Message

Type the following subcommand:
h

A help message is displayed for the last ? response from the ed editor.

Character Class Support in the ed Editor

In standard Patterns expression, a range expression matches the set of all characters that fall between two characters in the collation sequence of the current locale. The syntax of the range expression is as follows:
[character-character]

The first character must be lower than or equal to the second character in the collation sequence. For example, [a-c] matches any of the characters a, b, or c in the En_US locale.

The range expression is commonly used to match a character class. For example, [0-9] is used to mean all digits, and [a-z A-Z] is used to mean all letters. This form may produce unexpected results when ranges are interpreted according to the collating sequence in the current locale.

Instead of the preceding form, use a character class expression within [ ] (brackets) to match characters. The system interprets this type of expression according to the character class definition in the current locale. However, you cannot use character class expressions in range expressions.

The syntax of a character class expression is as follows:
[:CharacterClass:]

That is, a left bracket, a colon, the name of the character class, another colon, and then a right bracket.

The following character classes are supported in all locales:

Item Description
[:upper:] Uppercase letters
[:lower:] Lowercase letters
[:alpha:] Uppercase and lowercase letters
[:digit:] Digits
[:alnum:] Alphanumeric characters
[:xdigit:] Hexadecimal digits
[:punct:] Punctuation character (neither a control character nor alphanumeric)
[:space:] Space, tab, carriage return, new-line, vertical tab, or form feed character
[:print:] Printable characters, including space
[:graph:] Printable characters, not including space
[:cntrl:] Control characters
[:blank:] Space and tab characters

The brackets are part of the character class definition. To match any uppercase ASCII letter or ASCII digit, use the following regular expression:

[[:upper:] [:digit:]]

Do not use the expression [A-Z0-9].

A locale may support additional character classes.

The newline character is part of the [:space:] character class but will not be matched by this character class. The newline character may only be matched by the special search characters $ (dollar sign) and ^ (caret).

Exit Status

The ed and red commands return the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.