ed or red Command Purpose Edits text by line. Syntax ed [ -p ] [ -s ] [ - ] [ File ] red [ -p ] [ -s ] [ - ] [ File ] Description The ed command starts a line editing program, the ed editor, that 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 replaces the buffer's previous contents. There is also a restricted version of the ed command, the red command, for use with the restricted shell (rsh). With the red command, you can edit only files that reside in the current directory or in the /tmp directory, and you cannot use the !AIX-Command subcommand. An ed editor subcommand consists of zero, one, or two addresses, followed by a single-character subcommand, possibly followed by parameters to that subcommand. The addresses specify one or more lines in the buffer. Because every subcommand has default ad- dresses, you frequently do not need to specify addresses. The ed editor allows you to edit only the current line unless you address another line in the buffer. 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. You can also enter command mode by entering a . (period) alone at the beginning of a line. text input mode In text input mode, the ed editor allows you to enter text into the file buffer but does not recognize subcom- mands. You enter text mode by using the a subcommand, c sub- command, or i subcommand. You leave text mode by entering a . (period) alone at the beginning of a line. The following lists the ed editor's size limitations: * 64 characters per file name * 512 characters per line (although there is currently a system- imposed limit of 255 characters per line entered from the key- board) * 256 characters per global subcommand list * 128,000 character buffer size 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 physi- cal data storage (disk or tape drive) available or on the maximum number of lines permitted in user memory. Pattern Matching The ed editor supports a limited form of special pattern-matching characters that you can use as regular expressions (REs) to con- struct pattern strings. You can use these patterns in addresses to specify lines and in some subcommands to specify portions of a line. Regular Expressions (REs) The following REs match a single character or a collating element as follows: Character An ordinary character (other than one of the special pattern-matching symbols) matches itself. . A . (period) matches any single character except for the new- line character. [String] A String enclosed in [] (brackets) matches any one char- acter in the string. Certain pattern-matching characters have special meanings within brackets as follows: \^ If the first character of String is a \^ (circumflex), the RE ([\^String]) matches any character except the characters in String and the new-line character. A \^ (circumflex) has this special meaning only if it occurs first in the string. - You can use a - (minus sign) to indicate a range of consecutive ASCII characters according to the current collating sequence. For example, [a-f] might be equivalent to [abcdef] or [aAbBcCdDeEfF] or [aáàbcdeéèf]. A collating sequence may define equivalence classes for characters. For example, if three char- acters: e, é, and è are equivalent, the following expressions identify the same sequence of characters: [a-e] [a-è] The minus sign loses its special meaning if it occurs first ([-String]), if it immediately follows an initial circumflex ([\^-String]), or if it appears last ([String-]) in the string. ] When the ] (right bracket) is the first character in the string ([]String]) or when it immediately follows an initial circumflex ([\^]String]), it is treated as a part of the string rather than as the string terminator. Forming Patterns The following rules describe how to form patterns from Regular Expressions (REs): * An RE that consists of a single, ordinary character matches that same character in a string. * An RE followed by an * (asterisk) matches zero or more oc- currences of the character that the RE matches. For example, the following pattern: ab*cd matches each of the following strings: acd abcd abbcd abbbcd but not the following string: abd If there is any choice, 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. * An RE followed by: \{m\} Matches exactly m occurrences of the character matched by the RE. \{m,\} Matches at least m occurrences of the character matched by the RE. \{m,n\} Matches any number of occurrences of the character matched by the RE from m to n inclusive. The numbers m and n must be integers from 0 to 255, inclusive. Whenever a choice exists, this pattern matches as many oc- currences as possible. * You can combine REs into patterns that match strings containing that same sequence of characters. For example, AB\*CD matches the string AB*CD, and [A-Za-z]*[0-9]* matches any string that contains any combination of alphabetic characters (including none), followed by any combination of numerals (including none). * The character sequence \(Pattern\) marks a subpattern that matches the same string it would match if it were not enclosed. * The characters \Number match the same string of characters that a subpattern matched earlier in the pattern (see the preced- ing discussion). Number is a digit. The pattern \Number matches the string matched by the occurrence of the subpattern specified by Number, counting from left to right. For example, the following pattern: \(A\)\(B\)C\2\1 matches the string ABCBA. You can nest subpatterns. Restricting What Patterns Match A pattern can be restricted to match only the first segment of a line, the final segment, or both: * A \^ (circumflex) at the beginning of a pattern causes the pat- tern to match only a string that begins in the first character position on a line. * A $ (dollar sign) at the end of a pattern causes that pattern to match only a string that ends with the last character (not in- cluding the new-line character) on a line. * The \^Pattern$ construction restricts the pattern to matching only an entire line. In addition, the null pattern (that is //) duplicates the previ- ous pattern. Addressing Lines There are three types of addresses in the ed editor: 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. The following are guidelines for constructing addresses: * A . (period) addresses the current line. This is the default for most ed subcommands and does not need to be specified. * A $ (dollar sign) addresses the last line of the buffer. * Number addresses the specified line number of the buffer. * 'x addresses the line marked with a lowercase ASCII letter, represented by x, by the k subcommand. * /Pattern/ (a pattern enclosed in slashes) addresses the next line containing a matching string. 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 contin- ues until it either finds a match or returns to the current line. * ?Pattern? (a pattern enclosed in question marks) addresses the previous line that contains a match for the pattern. The ?Pattern? construction, like /Pattern/, can search the entire buffer, but it searches in the opposite direction. * An address followed by +Number or -Number (a plus sign or a minus sign followed by a decimal number) specifies an address plus or minus the indicated number of lines. The + (plus) sign is optional. * An address that begins with a + (plus sign) or - (minus sign) specifies a line relative to the current line. For example, -5 is the equivalent of .-5 (five lines above the current line). * An address that ends with - or + specifies the line immediately before (-) or immediately after (+) the addressed line. Used alone, the + addresses the line immediately after the current line; however, the + is optional. The + and - have a cumulative effect; for example, the address - - (minus minus) addresses the line two lines before the current line. * For convenience, a , (comma) stands for the address pair 1,$ (first line through last line) and a ; (semicolon) stands for the pair .,$ (current line through last line). 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 must be numerically smaller than the second. Subcommands In most cases, only one ed editor subcommand can be entered on a line. The exceptions to this rule are the p and l subcommands, which can be added to any subcommand except e, f, r, or w. 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, f, r, or w given without a file name uses the default file name. The ed editor responds to an error condition with one of two mes- sages: ? (question mark) or ?File. When it 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 char- acter. Editing a File with the ed Editor You can use the ed editor subcommands to perform the following tasks: * Adding text. * Changing lines. * Changing the current line. * Copying lines. * Deleting lines. * Displaying text and finding out the current line. * Joining lines. * Locating text in a file. * Making global changes. * Making substitutions. * Marking lines. * Moving text. * Saving text. * Undoing a change. ed Subcommands for Editing a File Note: In the following lists of ed editor subcommands, default addresses are shown in parentheses. Do not type the parentheses. The address . (period) refers to the current line. When a . (period) is shown in the first position of an otherwise empty line, it is the signal to return to command mode. Adding Text (.)a Text . The a (append) subcommand adds text to the buffer after the ad- dressed line. The a subcommand sets the current line to the last inserted line, or, if no lines were inserted, to the addressed line. Address 0 causes the a subcommand to add text to the be- ginning of the buffer. See "Addressing Lines" for more informa- tion about addressing. 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. However, 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. When you have entered all your text, enter a . (period) alone at the start of a new line. (.)i 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, i sets the current line to the addressed line. You cannot use a 0 address for this subcommand. See "Ad- dressing Lines" for more information about addressing. 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. However, 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. When you have entered all your text, enter a period alone at the start of a new line. The i subcommand differs from the a subcommand only in the placement of the text. Changing Lines (.,.)c Text . The c (change) subcommand deletes the addressed lines, then re- places them with new input. The c subcommand sets the current line to the last new line of input, or, if there was no input, to the first line that was not deleted. See "Addressing Lines" for more information about addressing. The c subcommand first deletes the line(s) you want to replace and then lets you enter the new lines. Type in the text you want, pressing the Enter key at the end of each line. When you have entered all of the new text, enter a . (period) on a line by itself. Copying Lines (.,.)tAddress The t (transfer) subcommand inserts a copy of the addressed lines after Address. The t subcommand accepts address 0 (for inserting lines at the beginning of the buffer). See "Addressing Lines" for more information about addressing. The t subcommand sets the current line to the last line copied. Deleting Lines (.,.)d 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. See "Addressing Lines" for more information about addressing. Displaying Text and Finding Out the Current Line (.,.)l The l (list) subcommand displays the addressed lines. The l subcommand wraps long lines and represents nonprinting charac- ters either with mnemonic overstrikes or in hexadecimal notation. An l subcommand may be appended to any ed editor subcommand ex- cept e, f, r, or w. For example, the dl subcommand deletes the current line and displays the new current line. (.,.)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. An n subcommand may be appended to any ed editor sub- command 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 line(s) and sets the current line set to the last line displayed. A p subcommand may be appended 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. With the $ address, the = subcommand displays the number of the last line in the buffer. The = sub- command does not change the current line and cannot be included in a g subcommand or v subcommand list. Joining Lines (.,.+1)j The j (join) subcommand joins contiguous lines by remov- ing the intervening new-line characters. If given only one ad- dress, the j subcommand does nothing. See "Addressing Lines" for more information about addressing. (For splitting lines, see the s subcommand.) Making Global Changes (1,$)g/Pattern/SubcommandList The g (global) subcommand first marks every line that matches Pattern. 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 SubcommandList. Enter a single subcommand or the first subcommand of a list on the same line with the g subcom- mand; enter subsequent subcommands on separate lines. Except for the last line, each of the lines should end with a \ (backslash). The SubcommandList can include the a, i, and c subcommands and their input. If the last command in SubcommandList would normal- ly be the . (period) that ends input mode, the . (period) is op- tional. If there is no SubcommandList, the current line is displayed. The SubcommandList cannot include the g , G, v, or V subcommands. Note: The g subcommand is similar to the v subcommand, which runs SubcommandList for every line that does not contain a match for the pattern. (1,$)G/Pattern/ The interactive G (Global) subcommand marks every line that matches the Pattern, then displays the first marked line, sets the current line to that line, and waits for a subcom- mand. A pattern can be a fixed character string or a regular expression.The G subcommand accepts any but the following ed edi- tor subcommands: a, i, c, g, G, v, and V. After the sub- command 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, if there was one. The G sub- command can be stopped by pressing Interrupt (the Ctrl-C key se- quence). (1,$)v/Pattern/SubcommandList The v subcommand runs the subcom- mands in SubcommandList for each line that does not contain a match for Pattern. A pattern can be a fixed character string or a regular expression. The v subcommand accepts any but the fol- lowing ed editor subcommands: a, i, c, g, G, and V. Note: The v subcommand is a complement for the global subcommand, the g subcommand, which runs SubcommandList for every line that does contain a match for the pattern. (1,$)V/Pattern/ The V subcommand marks every line that does not match Pattern, then displays the first marked line, sets the current line to that line, and waits for a subcommand. A pattern can be a fixed character string or a regular expression. The V subcommand accepts any but the following ed editor subcommands: a, i, c, g, G, and v. Note: The V subcommand complements the G subcommand, which marks the lines that do match the pattern. Making Substitutions (.,.)s/Pattern/Replacement/ (.,.)s/Pattern/Replacement/ng The s (substitute) subcommand searches each addressed line for a string that matches the pat- tern and replaces the string with the specified Replacement string. A pattern can be a fixed character string or a regular expression. See "Addressing Lines" for more information about addressing. Without the global indicator ( g), the s subcommand replaces only the first matching string on each addressed line. With the g indicator, the s subcommand replaces every occurrence of the matching string on each addressed line. If the s subcom- mand does not find a match for the pattern, it returns the error message ? (question mark). Any character except a space or a new-line character can separate (delimit) the pattern and Replacement. The s subcommand sets the current line to the last line changed. If Number (an integer) is specified, then the first Number match- ing strings in each addressed line are replaced. An & (ampersand) in the Replacement string is a special symbol that has the same value as the Pattern string. 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 this special meaning of the & (ampersand) in Replacement. A subpattern is part of a pattern enclosed by the strings \( and \); the pattern works as if the enclosing characters were not present. In Replacement, the characters \Number refer to strings that match subpatterns; Number, a decimal number, refers to the occurrence of a subpattern specified by Number, counting from the left. (For example, s/\(t\)\(h\) \(e\)/t\1\2ose) replaces the with those if there is a match for the pattern the on the current line.) Whether subpatterns are nested or in a series, \Number refers to the occurrence speci- fied by Number, counting from the left of the delimiting charac- ters, \) (backslash right parenthesis). The % (percent sign), when used by itself as Replacement, causes the s subcommand to use the previous Replacement again. The % does not have this special meaning if it is part of a longer Replacement or if it is preceded by a \ (backslash). Lines may be split by substituting new-line characters into them. In Replacement, the sequence \Enter quotes the new-line charac- ter (not displayed) and moves the cursor to the next line for the remainder of the string. New-line characters cannot be substi- tuted as part of a g subcommand or v subcommand list. Marking Lines (.)kx The k (mark) subcommand marks the addressed line with the name specified by x, which must be a lowercase ASCII letter. See "Addressing Lines" for more information about addressing. The address 'x (single quotation mark before the marking character) then addresses this line. The k subcommand does not change the current line. Moving Text (.,.)mA The m (move) subcommand repositions the addressed line(s). The first moved line follows the line addressed by A. Address 0 for A causes m to move the addressed line(s) to the be- ginning of the file. Address A cannot be one of the lines to be moved. See "Addressing Lines" for more information about ad- dressing. The m subcommand sets the current line to the last moved line. Saving Text (1,$)w File The w (write) subcommand copies the addressed lines from the buffer to the file specified by the File variable. If the file does not exist, the w subcommand creates it with permis- sion 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 File 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, if any (see the e subcommand and the f subcommand). The w subcommand does not change the current line. If the ed editor successfully writes the file, it displays the number of characters written. If you specify !AIX-Command in- stead of a file name, the w subcommand reads the output of the AIX command specified. The w subcommand does not save the name of the AIX command you specified as a default file name. Note: Zero is not a legal address for the w subcommand. There- fore, it is not possible to create an empty file with ed. Undoing a Change u The u (undo) subcommand restores the buffer to the state it was in before it was last modified by an ed editor subcommand. The subcommands that the u subcommand cannot undo are e, f, and w. ed Subcommands for Manipulating Files You can use the subcommands for manipulating files to do the fol- lowing tasks: * Editing Additional Files. * Changing the Default File Name. * Adding Another File to the Current File. Editing Additional Files 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 last saved (with the w subcommand), the ed editor displays a ? (question mark) before it clears the buffer. The e subcommand stores File 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 File, the e subcommand takes the rest of the line as an AIX 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 since the last w subcommand. Changing the Default File Name f [File] The f (file name) subcommand changes the default file name (the stored name of the last file used) to File, if File is given. If File is not given, the f subcommand displays the de- fault file name. (The e subcommand stores the default file name.) Adding Another File to the Current File ($)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 (see the e subcommand and the f subcommand). The r subcommand does not change the default file name. Address 0 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 File in an r subcommand, the rest of the line is taken as an AIX shell command whose output is to be read. The r subcommand does not store the names of AIX commands as default file names. Other Subcommands with the ed Editor You can use other subcommands with the ed editor to do the fol- lowing tasks: * Changing the Prompt String for the ed Editor. * Requesting Help with the ed Editor. * Entering System Commands from the ed Editor. * Ending and Exiting the ed Editor. Changing the Prompt String for the ed Editor P The P (Prompt) subcommand turns on or off the ed editor prompt string * (asterisk). Initially, the P subcommand is off. Requesting Help with the ed Editor h The h (help) subcommand gives a short explanation (help mes- sage) for the most recent ? (question mark) diagnostic or error message displayed. H The H (Help) subcommand causes the ed editor to display the help messages for all subsequent ? (question mark) diagnostics. The H subcommand also explains the previous ? (question mark) if there was one. The H subcommand alternately turns this mode on and off; it is initially off. Entering System Commands from the ed Editor !AIX-Command The !AIX-Command allows AIX commands to be run from the ed editor. Anything following the ! (exclamation point) on an ed editor subcommand line is interpreted as an AIX command. Within the text of that command string, the ed editor replaces the unescaped % (percent sign) with the current file name, if there is one. When used as the first character of a shell command (after the ! that runs a subshell), the ed editor replaces the ! character with the previous AIX command; for example, the !! (double excla- mation point) command repeats the previous AIX command. If the AIX command interpreter (the sh command) expands the command string, the ed editor echoes the expanded line. The ! subcommand does not change the current line. Ending and Exiting the ed Editor q The q (quit) subcommand exits the ed editor. Before ending the editor, the q subcommand checks to determine whether the buffer has been written to a file since the last time it was changed. If not, the q subcommand displays the ? (question mark) message. (See the w subcommand for information about writing text to a file.) Q The Q (Quit) subcommand exits the ed editor without checking for changes to the buffer since the last w subcommand (compare with the q subcommand). Flags -p String Sets the editor prompt to String. The default for String is null (no prompt). -s Same as the - (minus sign) flag. - The - (minus sign) suppresses character counts that the editor displays with the e subcommand, r subcommand, and w subcom- mand. It also suppresses diagnostic messages for the e subcom- mand and the q subcommand, and suppresses the ! (exclamation point) prompt after an ! AIX-Command. Character Class Support in the ed Editor In regular 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 or equal to the second charac- ter in the collation sequence. For example, [a-c] matches any of the characters a, b, or c in the En_US locale. A common use of the range expression is 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 unex- pected results when ranges are interpreted according to the col- lating 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 defini- tion in the current locale. However, you cannot use character class expressions in range expressions. The following is the syntax of a character class expression: [:CharacterClass:] That is, in a series, 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: [: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 follow- ing regular expression: [[:upper:] [:digit:]] Do not use the expression [A-Z0-9]. A locale may support character classes additional to those previ- ously listed. Exit Status The following exit values are returned: 0 Successful completion. >0 An error occurred. Implementation Specifics This command is part of Base Operating System (BOS) Runtime. Related Information The ex command, edit command, grep command, sed command, sh command, rsh command, stty command, vi or vedit command, view command. Editors Overview in AIX Version 3.2 Editing Concepts and Pro- cedures introduces general concepts about editors and describes the main AIX editors. Understanding Modes and Subcommands in the ed Editor in AIX Ver- sion 3.2 Editing Concepts and Procedures describes concepts and tasks specific to the ed editor. Operating the ed Editor in AIX Version 3.2 Editing Concepts and Procedures discusses procedures used with the ed editor.