|Click this button to go to the index for this section.|
NAMEed, e, red - Edits a file line by line
SYNOPSISed [-p string] [-s] [-x] [-] [file] e [-p string] [-s] [-x] [-] [file] red [-p string] [-s] [-x] [-] [file] The ed command is a line editing program that works on one file at a time by copying it into a temporary edit buffer and making changes to that copy.
STANDARDSInterfaces documented on this reference page conform to industry standards as follows: ed: XPG4, XPG4-UNIX e: XPG4, XPG4-UNIX red: XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags.
OPTIONS-p string Sets the ed prompt to string. The default for string is null (no prompt). -s Suppresses byte counts that the editor displays with the e, E, r, and w subcommands, suppresses diagnostic messages for the e, E, and q subcommands, and suppresses the ! (exclamation point) prompt after a !system_command. This also holds true if you invoke ed with a file name. - Suppresses byte counts that the editor displays with the e, E, r, and w subcommands, suppresses diagnostic messages for the e, E, and q subcommands, and suppresses the ! (exclamation point) prompt after a !system_command. This also holds true if you invoke ed with a file name. This form is obsolescent and may be withdrawn in future releases. If you quit without writing the file, you do not get an error message. (Generally, before ending the program, q checks to determine whether the buffer was written to a file since the last time it was changed. If not, q displays the ? message.) -x [DIGITAL] Prompts for an encryption key and unencrypts the file. If the file specified is not encrypted or an incorrect key is entered, garbled text is displayed.
OPERANDSfile The name of the file to be edited. If this operand is specified, ed reads file into the internal buffer before reading standard input.
DESCRIPTIONThe ed command does not alter the file itself until you use the write subcommand. You can specify on the command line the file you want to edit, or you can use ed subcommands to read a file into the buffer. When ed reads a new file into the buffer, the contents of that file replace the buffer's previous contents, if any. [DIGITAL] There is also a restricted version of ed, the red command, for use with the restricted shell (rsh). With red, you can edit only files that reside in the current directory, or in the /tmp directory, and you cannot use the !system_command subcommand (see Subcommands section). [DIGITAL] A file may be encrypted using the X subcommand. Care should be take to remember the encryption key used as there is not a simple way to unencrypt a file if the key used is lost. Only the first six characters of the key are significant. Modes for ed The ed program operates in one of two modes, command mode and text mode. In command mode, ed recognizes and executes subcommands. In text mode, ed adds text to the file buffer, but does not recognize subcommands. You enter text mode by using the a subcommand, c subcommand, or i subcommand. To leave text mode, enter . (dot) alone at the beginning of a line. [DIGITAL] The e command is a supported alias for the ed command. Syntax for Subcommands An ed subcommand consists of zero, one, or two addresses, followed by a single-character subcommand, possibly followed by arguments to that subcommand. These addresses specify one or more lines in the buffer. Because every subcommand has default addresses, you frequently do not need to specify addresses. Pattern Matching The ed command 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. [DIGITAL] For information about regular expressions (REs), see the grep command. Addressing There are three types of ed addresses: line number addresses, addresses relative to the current line, and pattern addresses. The current line is the point of reference in the buffer and is represented by a . (dot). When you start the ed program, the current line is the last line in the buffer. As you edit a file, the current line changes to the last line affected by a subcommand. The current line is the default address for several ed commands. (See the Subcommands section to find out how each subcommand affects the current line.) Subcommands for determining and changing the current line are described in the Subcommands section. The following are guidelines for constructing addresses: · A . (dot) addresses the current line. · A $ (dollar sign) addresses the last line of the buffer. · The number n addresses the nth line of the buffer. · The construct 'x addresses the line marked with a lowercase letter, x, by the k subcommand. · The /pattern/ construct addresses the next line that contains 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 continues until it either finds a match or returns to the current line. · The ?pattern? construct addresses the previous line that contains a match for the pattern. The ?pattern? construct, like /pattern/, can search the entire buffer, but it does so in the opposite direction. · An address followed by +number or -number specifies an address plus or minus the indicated number of lines. (The + (plus sign) is optional.) · An address that begins with + or - 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 - character addresses the line immediately before the current line. The + character addresses the line immediately after the current line; however, the + character is optional. The + and - characters have a cumulative effect; for example, the address -- addresses the line two lines above 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). Commands that do not accept addresses regard the presence of an address as an error. Commands that do 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, a , (comma) separates addresses (for example 2,8). A ; (semicolon) can also separate addresses. A ; between addresses causes ed 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 based on guidelines 5 and 6). In a pair of addresses, the first must be numerically smaller than the second. Limits for ed The following is a list of ed size limitations: · [DIGITAL] 2048 bytes per line. · [DIGITAL] 256 bytes per global subcommand list. · [DIGITAL] 128 kilobyte buffer size. (The buffer not only contains the original file, but also editing information.) [DIGITAL] The maximum number of lines depends on the amount of memory available to you. 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. Subcommands In most cases, only one ed subcommand can be entered on a line. The exceptions to this rule are the n, p, and l subcommands, which can be added to any subcommand except e, E, f, q, Q, r, w, or !. The e, E, f, r, and w subcommands accept file names as arguments. The ed command stores the last file name used with a subcommand as a default file name. The next e, E, f, r, or w given without a file name uses the default file name. The ed command responds to an error condition with one of two messages: ? (question mark) or ?file. When the ed command receives an INT signal, it displays a ? and returns to command mode. When ed reads a file, it discards NULL characters and all characters after the last newline character. Unless otherwise noted, all subcommands work by default on the current line; an address is optional. If you specify an address, you do not type the brackets. When used as an address, a . (dot) refers to the current line. When a . (dot) is shown in the first position on an otherwise blank line, it terminates text mode and returns to command mode. The text parameter denotes user input in text mode. Note that address need not be a number; it can be a regular expression of the form /RE/,/RE/ or /RE/;/RE/. [address]a text . The a (append) subcommand adds text to the buffer after the addressed line. Enter a . (dot) to return to command mode. The a subcommand sets the current line to the last inserted line, or, if no lines were inserted, to the addressed line. Address 0 (zero) causes the a subcommand to add text to the beginning of the buffer. [address1,address2]c text . The c (change) subcommand deletes the addressed lines, then replaces them with new input. Enter a . (dot) to return to command mode. The c command sets the current line to the last new line of input, or, if there were none, to the first line that was not deleted. [address1,address2]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. e file The e (edit) subcommand first deletes any contents from the buffer, then loads another file into the buffer, sets the current line to the last line of the buffer, and displays the number of bytes read in to the buffer. If the buffer was changed since its contents were last saved (with the w subcommand), e displays ? before it clears the buffer. The e subcommand stores file as the default file name to be used, if necessary, by subsequent e, E, r, or w subcommands. (See the f subcommand.) When the ! (exclamation point) character replaces file, e takes the rest of the line as a shell (sh) 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 e, with one exception: E does not check for changes made to the buffer since the last w subcommand. 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 prints the default file name. [address1,address2]g/pattern/subcommand_list The g (global) subcommand first marks every line that matches the pattern. Then, for each marked line, this subcommand sets the current line to that line and executes subcommand_list. Place a single subcommand, or the first subcommand of a list, on the same line with the g subcommand; place subsequent subcommands on separate lines. Except for the last line, end each of these lines with a \ (backslash). The subcommand_list can include the a, i, and c subcommands and their input. If the last command in subcommand_list is normally the . (dot) that ends input mode, the . (dot) is optional. If there is no subcommand_list, ed displays the current line. The subcommand_list cannot include the g, G, v, V, or ! subcommands. The g subcommand is similar to the v subcommand, which executes subcommand_list for every line that does not contain a match for the pattern. This subcommand defaults to the entire file, not to the current line. [address1,address2]G/pattern/ The interactive G (Global) subcommand first marks every line that matches the pattern, then displays the first marked line, sets the current line to that line, and waits for a subcommand. The G subcommand accepts any but the following ed subcommands: a, c, i, g, G, v, V, and !. After the subcommand finishes, G displays the next marked line, and so on. The G subcommand takes a newline character as a null subcommand. The & subcommand causes G to execute the previous subcommand again, if there is one. Subcommands executed within the G subcommand can address and change any lines in the buffer. The G subcommand can be terminated by pressing the Interrupt key sequence. This subcommand defaults to the entire file, not to the current line. h The h (help) subcommand displays a short message that explains the reason for the most recent ? notification. The current line number is unchanged. H The H (Help) subcommand causes ed to enter a mode in which help messages (see the h command) are displayed for all subsequent ? notifications. The H subcommand toggles this mode, and is initially set to "off". The current line number is unchanged. [address]i ext . The i (insert) subcommand inserts text before the addressed line and sets the current line to the last inserted line. Enter . (dot) to return to command mode. If no lines are inserted, i sets the current line to the addressed line. This subcommand differs from the a subcommand only in the placement of the input text. Address 0 (zero) is not legal for this subcommand. [address1,address1+1]j The j (join) subcommand joins contiguous lines by removing the intervening newline characters. If given only one address, j does nothing. (For splitting lines, see the s subcommand.) Lines that exceed the line length limit cannot be joined. If lines are joined, the current line number is set to the address of the joined line; otherwise, the current line number is unchanged. [address]kx The k (mark) subcommand marks the addressed line with name x, which must be a lowercase letter. The address 'x (single quotation mark before the marking character) then addresses this line. The k subcommand does not change the current line. Marks attached to lines are deleted with the line. [address1,address2]l The l (list) subcommand displays the addressed lines. The l subcommand wraps long lines and, unlike the p subcommand, represents nonprinting characters as 3-digit octal numbers with a \ (backslash) preceding each byte in the character. The following characters, however, are written as escape sequences: \\ Backslash \a Alert \b Backspace \f Formfeed \n Newline \r Carriage return \t Tab \v Vertical tab A $ (dollar sign) character is placed at the end of each line, so that a real (literal) $ at the end of a line cannot be misinterpreted. An l subcommand can be appended to any ed subcommand except: e, E, f, q, Q, r, w, or !. [address1,address2]maddress3 The m (move) subcommand repositions the addressed lines. The first moved line follows the line addressed by address3. Address 0 (zero) for address3 causes m to move the addressed lines to the beginning of the file. The line specified by address3 cannot be one of the lines to be moved. The m subcommand sets the current line to the last moved line. [address1,address2]n The n (number) subcommand displays the addressed lines, each preceded by its line number and a tab character (displayed as spaces); the n subcommand leaves the current line at the last line displayed. An n subcommand can be appended to any ed subcommand except e, E, f, q, Q, r, w, or !. [address1,address2]p The p (print) subcommand displays the addressed lines and sets the current line set to the last line displayed. A p subcommand can be appended to any ed subcommand except: e, E, f, q, Q, r, w, or !. For example, the subcommand dp deletes the current line and displays the new current line. P The P (Prompt) subcommand turns the ed prompt string * or the string specified by the -p option on or off. Initially, P is off. q The q (quit) subcommand exits the ed program. Before ending the program, q checks to determine whether the buffer was written to a file since the last time it was changed. If not, q displays the ? message. You do not get more than one prompt in a row; a second consecutive q quits the ed program without displaying a prompt. Q The Q (Quit) subcommand exits the ed program without checking for changes to the buffer since the last w subcommand (compare with the q subcommand). [address]r file The r (read) subcommand reads a file into the buffer after the addressed line; r does not delete the previous contents of the buffer. When entered without file, r reads the default file, if any, into the buffer (see e and f subcommands). The r subcommand does not change the default file name. Address 0 (zero) causes r to read a file in at the beginning of the buffer. After it reads a file successfully, r, displays the number of bytes read into the buffer and sets the current line to the last line read. If ! (exclamation point) replaces file in a r subcommand, r takes the rest of the line as a shell (sh) command whose output is to be read. The r subcommand does not store the names of shell commands as default file names. [address1,address2]s/pattern/replacement/flags The s (substitute) subcommand searches each addressed line for a string that matches the pattern and then replaces the string with the specified replacement string. Without a number n or the global indicator g, s replaces only the first matching string on each addressed line. With n, s replaces the nth occurrence of pattern on the addressed line. With the g indicator, s replaces every occurrence of the matching string on each addressed line. If s does not find a match for the pattern, it returns the error message ?. Any character except a space or a newline character can separate (delimit) the pattern and replacement. The s subcommand sets the current line to the last line changed. An & (ampersand) in the replacement string is a special symbol that has the same value as the pattern string. So, for example, the subcommand s/out/&ing/ has the same effect as the subcommand s/out/outing/ and replaces out with outing on the current line. A backslash before the ampersand (\!>) removes this special meaning of & in replacement. The % (percent sign), when used by itself as replacement, causes s to use the previous replacement again. The % character does not have this special meaning if it is part of a longer replacement or if it is preceded by a \ (backslash). Lines can be split by substituting newline characters into them. In replacement, the sequence \<Return> quotes the newline character (not displayed) and moves the cursor to the nextline for the remainder of the string. The value of flags can be the following: count Substitutes for the countth occurrence only of the regular expression that is found on each addressed line. g Substitutes globally for all nonoverlapping instances of the regular expression, instead of just substituting for the first instance. l Displays the final line in which a substitution was made in the format specified for the l subcommand. n Displays the final line in which a substitution was made in the format specified for the n subcommand. p Displays the final line in which a substitution was made in the format specified for the p subcommand. [address1,address2]taddress3 The t (transfer) subcommand inserts a copy of the addressed lines after address3. The t subcommand accepts address 0 (for inserting lines at the beginning of the buffer). The t subcommand sets the current line to the last line copied. u The u (undo) subcommand restores the buffer to the state it was in before it was last modified by an ed subcommand. The commands that u can undo are a, c, d, g, G, i, j, m, r, s, t, u, v, and V. All changes made to the buffer by a g, G, v, or V global command are undone as a single change. The current line number is set to the value it had before the command being undone started. [address1,address2]v/pattern/subcommand_list The v subcommand executes the subcommands in subcommand_list for each line that does not contain a match for the pattern. The v subcommand is a complement for the global subcommand g, which executes subcommand_list for every line that does contain a match for the pattern. [address1,address2]V/pattern/ The V subcommand first marks every line that does not match the pattern, then displays the first marked line, sets the current line to that line, and waits for a subcommand. The V subcommand complements the G subcommand, which marks the lines that do match the pattern. [address1,address2]w file The w (write) subcommand copies the addressed lines from the buffer to the file named in file. If the file does not exist, the w subcommand creates it with permission mode 666 (read and write permission for everyone), unless the umask setting specifies another file creation mode. (For information about file permissions, see umask in sh and chmod.) The w subcommand does not change the default file name (unless file is the first file name used since you started ed). If you do not provide a file name, ed uses the default file name, if any (see the e, E, and f subcommands). The w subcommand does not change the current line. If ed successfully writes the file, it displays the number of characters written. When ! (exclamation point) replaces file, ed takes the rest of the line as a shell (sh) command whose output is to be read; w does not save shell command names as default file names (same effect as !). The use of the write subcommand with ! (exclamation point) is not considered the last w command that wrote the entire buffer. Thus, this alone does not prevent the warning to the user if an attempt is made to destroy the editor buffer through the e or q subcommands. The address 0 is not a legal address for the w subcommand. Therefore, it is not possible to create an empty file with ed. X The X subcommand prompts you to enter an encryption key. Only the first six characters of this key are significant. When the ed commands write a file after the encryption key is specified, the output is written in encrypted form. Subsequent edits of the file require the use the -x option to display the file in its unencrypted form. [address]= Without an address, the = (equal sign) subcommand displays the current line number. With the address $, = (equal sign) displays the number of the last line in the buffer. The = subcommand does not change the current line and cannot be included in a g or v subcommand list. !system_command The ! (exclamation point) subcommand allows system commands to be run from within ed. Anything following ! on an ed subcommand line is interpreted as a system command. Within the text of that command string, ed replaces the unescaped character % with the current file name, if there is one. By default, the subcommands are executed by /usr/bin/sh. To support single user mode operation, subcommands can be executed by /sbin/sh. To change the default behavior, modify your $SHELL environmental variable to SHELL=/sbin/sh. When used as the first character of a shell command (after the ! that runs a subshell), ed replaces the ! character with the previous system command; for example, the command !! repeats the previous system command. If the command interpreter (the sh command) expands the command string, ed echoes the expanded line. The ! subcommand does not change the current line. If any replacements of % and/or ! are performed, the modified line is written to standard output before the command is executed. number +number -number The ed editor interprets a number alone on a line as an address and displays the addressed line. Addresses can be absolute (line numbers or $) or relative to the current line (+number or -number). Entering a newline character (a blank line) is equivalent to +1p and is useful for stepping forward through the buffer one line at a time.
EXIT STATUSThe following exit values are returned: 0 Successful completion. >0 An error occurred.
ASYNCHRONOUS EVENTSThe following actions are taken upon receipt of signals: SIGINT The ed command interrupts its current activity, writes the string ?\n to standard output, and returns to command mode. SIGHUP If the buffer is not empty and has changed since the last write, the ed command attempts to write a copy of the buffer in a file. First, the file named ed.hup in the current directory is used; if that fails, the file named ed.hup in the directory named by the HOME environment variable is used. In any case, the ed command exits without returning to command mode.
ENVIRONMENT VARIABLESThe following environment variables affect the execution of ed: LANG Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization variables contain an invalid setting, the utility behaves as if none of the variables had been defined. LC_ALL If set to a non-empty string value, overrides the values of all the other internationalization variables. LC_CTYPE Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments) and the behavior of character classes within regular expressions. LC_MESSAGES Determines the locale for the format and contents of diagnostic messages written to standard error. NLSPATH Determines the location of message catalogues for the processing of LC_MESSAGES.
FILESed.hup Recovery file written if SIGHUP is received during operation.
SEE ALSOCommands: chmod(1), edit(1), ex(1), grep(1), rsh(1), sed(1), Bourne shell sh(1b), POSIX shell sh(1p), stty(1), vi(1) Functions: regexp(3) Files: locale(4) Standards: standards(5) Command and Shell User's Guide