ed [ -p String] [ -s | -] [File]
red [ -pString] [ -s | -] [File]
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:
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 the Enter key 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 a subcommand, 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.
Note: The buffer contains the original file as well as editing information.
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.
-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. |
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.
The following REs match a single
character or a collating element as follows:
The following rules describe how to form patterns from REs:
ab*cd
matches each of the following strings:
acd abcd abbcd abbbcd
abd
If a choice exists, the longest matching leftmost string is chosen. For example, given the following string:
122333444
the pattern .* matches 122333444, the pattern .*3 matches 122333, and the pattern .*2 matches 122.
For example, the following pattern:
\(A\)\(B\)C\2\1
matches the string ABCBA. You can nest subpatterns.
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.
The ^Pattern parameter matches only a string that begins in the first character position on a line.
The Pattern$ parameter matches only a string that ends with the last character (not including the new-line character) on a line.
The ^Pattern$ parameter restricts the pattern to match an entire line.
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:
A . (period) addresses the current line. The . (period) is the default for most ed editor subcommands and does not need to be specified.
To address a specified line of the buffer, enter:
Number$
where the Number parameter represents a line number. For example:
2253$
addresses line number 2253 as the current line.
To address the line before the first line of the buffer, enter:
0
To address the last line of the buffer, enter:
$
To specify an address that is a specified number of lines above the current line, enter:
-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.
To specify an address that is a specified number of lines below the current line, enter:
+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.
To address the first line through the last line, enter:
,
The , (comma) represents the address pair 1,$ (first line through last line). The first line becomes the current line.
To address the current line through the last line, enter:
;
The ; (semicolon) represents the address pair .,$ (current line through last line).
To address a group of lines, enter:
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.
To address the next line that contains a matching string, enter:
/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.
To address the previous line that contains a match for the pattern, enter:
?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.
To address a marked line with the k subcommand, enter:
'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.
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.
You can use the ed editor subcommands to perform the following tasks:
Note: In the following descriptions of ed editor subcommands, default addresses are shown in parentheses. Do not type the parentheses. The address . (period) refers to the current line. A . (period) in the first position of an otherwise empty line is the signal to return to command mode.
(.)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 the Enter key 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 the Enter key 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 the Enter key 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 the Enter key 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. |
You can use different ed editor subcommands to add text in different locations. Use the preceding format to perform the following editing tasks:
a[l][n][p]
where l, n, and p are optional subcommands that display the added text.
i[l][n][p]
where l, n, and p are optional subcommands that display the added text.
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.
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.
[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.
\
\
and press the Enter key. The text you type is added after every line that contains the pattern specified in the command.
[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.
\
\
and press the Enter key. The text you type is added before every line that contains the pattern specified in the command.
[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.
\
\
and press the Enter key. The text you type is added after every line that does not contain the pattern specified in the command.
[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.
\
\
and press the Enter key. The text you type is added before every line that does not contain the pattern specified in the command.
You can change text in several different ways with the ed editor. Use the preceding format to perform the following editing tasks:
c[l][n][p]
where l, n, and p are optional subcommands that display the changed text.
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.
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.
\
\
and press the Enter key.
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.
\
\
and press the Enter key.
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:
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.
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.
Enter 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.
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.
The ed editor provides several ways to delete text. Use the preceding format to perform the following editing tasks:
Enter the following subcommand:
d[l][n][p]
where l, n, and p are optional subcommands that display the deleted line.
Enter 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.
Enter 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.
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.
s/Pattern
where the Pattern parameter is a character string or regular expression that represents the text you want to delete.
//
OR
To delete every instance of the pattern from the line, type:
//g
g
OR
To select the lines not indicated by the Pattern parameter in step 4, type:
v
/Pattern/s
where the Pattern parameter is the text you want to search.
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
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).
Note: The Address parameter is followed by the s subcommand.
Addresss/Pattern
where the Address parameter is the line number, range of line numbers, or symbolic address of the lines you want to delete the pattern from, and the Pattern parameter is a character string or regular expression that represents the text you want to delete.
//
OR
To delete every instance of the pattern from each line, type:
//g
[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.
///
OR
To delete every instance of the pattern from each line that contains it, type:
///g
[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.
/DeletePattern/
/
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]
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).
[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.
/DeletePattern/
/
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]
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).
(.,.)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. |
When you search for lines that contain or do not contain a specified pattern, you can select a range of line numbers to search. You can select and display one line or a group of lines in an ed editor file several different ways. Use the preceding format to perform the following editing tasks:
Enter 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.
Enter 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.
Enter 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.
Enter 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.
Enter 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.
Enter 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.
Enter 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.
Enter 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.
Enter 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.
The ed editor provides several ways to join or split a line. Use the preceding format to perform the following editing tasks:
Enter the following subcommand:
j[l][n][p]
where l, n, and p are optional subcommands that display the joined lines.
Enter 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.
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.
/
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.
/
(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 normally 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 typing the Ctrl-C key sequence. 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. |
Enter 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.
Enter 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 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:
Enter 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.
Enter 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.
Enter 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.
Enter 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.
You can save changes to a file in several ways. Use the preceding format to perform the following actions:
Enter the following subcommand:
w
The current file is saved under its current name, and the ed editor displays the number of characters written.
Enter 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.
Enter 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.
Enter 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.
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), \ (reverse slash), & (ampersand), and % (percent sign).
You can use the ed editor to perform the following text searches:
Enter 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.
Enter 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.
Enter 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.
Enter 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.
(.,.)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 & (ampersand) 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, 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. |
The ed editor provides several ways to substitute text. Use the preceding format to perform the following editing tasks:
s/OldString/NewString
where the OldString parameter is the existing text and the NewString parameter is the text you want to substitute for it.
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
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.
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
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.
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
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.
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
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.
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
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. |
Enter 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.
You can use ed editor subcommands to manipulate files to perform the following tasks:
Enter 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.
Enter 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.
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.) |
Enter the following subcommand:
f
The ed editor displays the name of the file in the edit buffer.
Enter 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.
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:
Enter the following subcommands:
E
The ed editor displays the number of characters in the file. Any changes you made before re-editing the file are lost.
Enter the following subcommands:
e
The ed editor displays the number of characters in the file.
Enter the following subcommands:
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.
Enter the following subcommands:
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.
You can use ed editor subcommands to perform the following tasks:
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. |
Enter the following subcommand:
P
The ed editor prompt, an * (asterisk), is displayed or not displayed, depending on its previous setting.
! 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. |
You can use the ! subcommand to perform the following actions:
Enter 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).
Enter the following subcommand:
!
The previously run operating system command runs and displays its output. After the command completes, the editor displays an ! (exclamation point).
!sh
q
To save changes before quitting, type:
w
then press the Enter key.
To quit without saving changes, type:
q
Q
Enter the following subcommand:
H
The help messages are displayed or not displayed for ? responses from the ed editor, depending on the previous setting.
Enter the following subcommand:
h
A help message is displayed for the last ? response from 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:
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).
The ed and
red commands return the following exit values:
0 | Successful completion. |
>0 | An error occurred. |
The edit command, ex command, grep command, rsh command, sed command, sh command, stty command, vi or vedit command, view command.