Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > E

ed(1)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

NAME

ed, red — line-oriented text editor

SYNOPSIS

ed [-p string] [-s|-] [-x] [file]

red [-p string] [-s|-] [-x] [file]

DESCRIPTION

The ed command executes a line-oriented text editor. It is most commonly used in scripts and noninteractive editing applications because, even though it can be used interactively, other editors such as vi and ex are typically easier to use in an interactive environment.

If file is specified, ed performs an e command (see below) on the named file; that is to say, the file is read into ed's buffer so that it can be edited.

Options

The following options are recognized:

-p string

Use string as the prompt string when in command mode. By default, there is no prompt string.

-s|-

Suppress printing of byte counts by e, E, r, and w commands, and suppress the ! prompt after a ! command. The - option is obsolescent and will be removed in a future release.

-x

Perform an X command first to handle an encrypted file.

File Handling

ed operates on a copy of the file it is editing; changes made to the copy have no effect on the original file until a w (write) command is given. The copy of the text being edited resides in a temporary file called the buffer. There is only one buffer.

red is a restricted version of ed that only allows editing of files in the current directory and prohibits executing shell commands via !shell-command. Attempts to bypass these restrictions result in the error message restricted shell.

Both ed and red support the fspec(4) formatting capability. After including a format specification as the first line of file and invoking ed with the controlling terminal in stty -tabs or stty tab3 mode (see stty(1)), the specified tab stops are automatically used when scanning file. For example, if the first line of a file contained

<:t5,10,15 s72:>

the tab stops would be set at columns 5, 10, and 15, and a maximum line length of 72 would be imposed.

Note: When you input text, ed expands tab characters as they are typed to every eighth column as a default.

Editor Commands Structure

Commands to ed have a simple and regular structure: zero, one, or two addresses followed by a single-character command, possibly followed by parameters to that command. These addresses specify one or more lines in the buffer. Every command that requires addresses has default addresses, so that the addresses can very often be omitted.

In general, only one command is allowed on a line. Append, change, and insert commands accept text input which is then placed in the buffer as appropriate. While ed is accepting text following an append, change, or insert command, it is said to be in input mode. While in input mode, no editor commands are recognized; all input is merely collected. To terminate input mode, type a period (.) alone at the beginning of a line.

Regular Expressions

ed supports the Basic Regular Expression (RE) syntax (see regexp(5)), with the following additions:

  • The null RE (for example, //) is equivalent to the last RE encountered.

  • If the closing delimiter of an RE or of a replacement string (for example, /) would be the last character before a newline, that delimiter can be omitted, in which case the addressed line is printed. The following pairs of commands are equivalent:

    s/s1/s2 g/s1 ?s1 s/s1/s2/p g/s1/p ?s1?

Line Addresses

To understand line addressing, remember that ed maintains a pointer to the current line. Generally speaking, the current line is the last line affected by a command. The exact effect of a given command on the current line is discussed under the description of each command. Addresses are interpreted according to the following rules:

1.

The character . refers to the current line.

2.

The character $ refers to the last line of the buffer.

3.

A decimal number n refers to the nth line of the buffer.

4.

A 'x refers to the line marked with the mark name character x, which must be a lower-case letter. Lines are marked with the k command described below.

5.

An RE enclosed by slashes (/RE/ ) refers to the first line found by searching forward from the line following the current line toward the end of the buffer and stopping at the first line containing a string matching the RE. If necessary, the search wraps around to the beginning of the buffer and continues up to and including the current line, so that the entire buffer is searched. (Also see WARNINGS below.)

6.

An RE enclosed by question marks (?RE? ) addresses the first line found by searching backward from the line preceding the current line toward the beginning of the buffer and stopping at the first line containing a string matching the RE. If necessary, the search wraps around to the end of the buffer and continues up to and including the current line. (Also see WARNINGS below.)

7.

An address followed by a plus (+) or minus (-) sign followed by a decimal number specifies that address plus or minus the indicated number of lines. The plus sign can be omitted.

8.

If an address begins with + or -, the addition or subtraction is calculated with respect to the current line. For example, -5 is interpreted as .-5.

9.

If an address ends with + or -, 1 is added to or subtracted from the address, respectively. As a consequence of this and rule 8 above, the address - refers to the line preceding the current line. (To maintain compatibility with earlier versions of the editor, the circumflex (^) and - characters are interpreted identically when encountered in addresses.) Moreover, multiple trailing + and - characters have a cumulative effect, so -- refers to the second line preceding the current line.

10.

For convenience, a comma (,) represents the address pair 1,$, while a semicolon (;) represents the pair .,$. Additionally, (,addr) represents the address pair 1,addr, while (;addr) represents the pair .,addr.

Commands require zero, one, or two addresses. Commands that do not use addresses treat the presence of an address as an error. Commands that accept one or two addresses assume default addresses when the number of addresses specified is insufficient. If more addresses are specified than a given command requires, the last one or two are used as appropriate.

Addresses are usually separated from each other by a comma (,). They can also be separated by a semicolon (;), in which case the current line (.) is set to the first address, after which the second address is calculated. This feature can be used to determine the starting line for forward and backward searches (see rules 5 and 6 above). The second address of any two-address sequence must correspond to a line in the buffer that follows the line corresponding to the first address.

Editor Commands

In the following list of ed commands, the default addresses are shown in parentheses (parentheses are not part of the address and should not be placed in an actual command except for other purposes).

It is generally illegal for more than one command to appear on a line. However, any command (except e, f, r, or w) can be suffixed by l, n, or p in which case the current line is respectively either listed, numbered, or printed, as discussed below under the l, n, and p commands.

(.)a

text

.

The a (append) command reads text and appends it after the addressed line. Upon completion, the new current line is the last inserted line, or, if no text was added, at the addressed line. Address 0 is legal for this command, causing the appended text to be placed at the beginning of the buffer.

(.,.)c

text

.

The c (change) command deletes the addressed lines then accepts input text to replace the deleted lines. Upon completion, the new current line is the last line in text or, if no text was provided, at the first line after the deleted line or lines. Address 0 would be interpreted as address 1.

(.,.)d

The d (delete) command deletes the addressed lines from the buffer. Upon completion, the new current line is the first line following the deleted text, or the last line in the file if the deleted line or lines were at the end of the buffer.

e file

The e (edit) command deletes the entire contents of the buffer, then reads in the named file. Upon completion, the new current line is the last line in the buffer. If no file name is given, the remembered file name, if any, is used (see the f command). The number of characters read is displayed, and file is remembered for possible use as a default file name in subsequent e, r, or w commands.

If the file name starts with !, the rest of the line is interpreted as a shell command whose standard output is to be read. Such a shell command is not remembered as the current file name.

Also see DIAGNOSTICS below.

E file

The E (forced edit) command is identical to e except that no check is made to ensure that the current buffer has not been altered since the last w command.

f file

If file is specified, the f (file name) command changes the remembered file name to file. Otherwise, it prints the remembered file name.

(1,$)g/RE/command-list

The g (global) command first marks every line that matches the given RE. Then, for every such line, the given command-list is executed with the current line initially set to that line. A single command or the first of a list of commands appears on the same line as the global command. All lines of a multiple-line list except the last line must end with a backslash (\). a, i, and c commands and associated input are permitted. The . that normally terminates input mode can be omitted if it would be the last line of the command-list. An empty command-list is equivalent to the p command. The g, G, v, and V commands are not permitted in the command-list. (Also see WARNINGS below.)

(1,$)G/RE

The interactive G (Global) command first marks every line that matches the given RE. Then, for every such line, the line is printed, then the current line is changed to that line and one command (other than a, c, i, g, G, v, or V) can be input and executed. After executing that command, the next marked line is printed, and so on. A newline character acts as a null command, and an & causes the re-execution of the most recent command executed within the current invocation of G. Note that the commands input as part of the execution of the G command may address and affect any lines in the buffer. The G command can be terminated by an interrupt signal (ASCII DEL or BREAK).

h

The h (help) command gives a short error message explaining the reason for the most recent ? diagnostic.

H

The H (Help) command causes ed to enter a mode in which error messages are printed for all subsequent ? diagnostics. It also explains the previous ? if there was one. The H command alternately turns this mode on and off. Initially, it is off.

(.)i

text

.

The i (insert) command inserts the given text before the addressed line. Upon completion, the current line is the last inserted line, or, if there were none, the addressed line. This command differs from the a command only in the placement of the input text. Address 0 is not legal for this command.

Under the UNIX 2003 environment (see standards(5)), the i (insert) command interprets address 0 as address 1.

(.,.+1)j

The j (join) command joins contiguous lines by removing the appropriate newline characters. If exactly one address is given, this command does nothing.

(.)kx

The k (mark) command marks the addressed line with the name x, which must be a lower-case letter. The address 'x then addresses this line. Upon completion, the new current line remains unchanged from before.

(.,.)l

The l (list) command writes the addressed lines to standard output in a visually unambiguous form. Characters listed in the following table are written as the corresponding escape sequence. Nonprintable characters not in the table are written as a three-digit octal number (with a preceding backslash character) for each byte in the character (most significant byte first).

Long lines are folded with the point of folding indicated by writing a backslash character followed by a newline. The end of each line is marked with a $. An l (ell) command can be appended to any command other than e, E, f, q, Q, r, w, or !. The current line number is set to the address of the last line written.

Escape ASCIIEscape ASCII
SequenceRepresentsNameSequenceRepresentsName
\\backslash\\rcarriage returnCR
\aalertBEL\thorizontal tabHT
\bbackspaceBS\vvertical tabVT
\fformfeedFF   

(.,.)ma

The m (move) command repositions the addressed lines after the line addressed by a. Address 0 is legal for a, causing the addressed lines to be moved to the beginning of the file. It is an error if address a falls within the range of moved lines; Upon completion, the new current line is the last line moved.

(.,.)n

The n (number) command prints the addressed lines, preceding each line by its line number and a tab character. Upon completion, the new current line is the last line printed. The n command can be appended to any command other than e, f, r, or w.

(.,.)p

The p (print) command prints the addressed lines. Upon completion, the new current line is the last line printed. The p command may be appended to any other command other than e, E, f, q, Q, r, w, or !. For example, dp deletes the current line and prints the new current line.

P

The P (prompt) command causes ed to prompt with an asterisk (*) (or with string if the -p option was specified in the command line) for all subsequent commands. The P command alternately turns this mode on and off. It is initially on if the -p option was specified; otherwise, off. The current line number is unchanged.

q

The q (quit) command causes ed to exit. No automatic write of a file is done (but see DIAGNOSTICS below).

Q

The editor exits unconditionally without checking for changes in the buffer since the last w command.

($)r file

The r (read) command reads the specified file into the buffer after the addressed line. If no file name is given, the remembered file name, if any, is used (see the e and f commands). The remembered file name is not changed unless file is the very first file name mentioned since ed was invoked. Address 0 is legal for r and places the contents of file at the beginning of the buffer. If the read is successful, the number of characters read is displayed. Upon completion, the new current line is the last line read into the buffer. If the file name starts with !, the rest of the line is interpreted as a shell command whose standard output is to be read. For example, $r !ls appends a listing of files in the current directory to the end of the file being edited. A shell command is not remembered as the current file name.

(.,.)s/RE/replacement/flags

The s (substitute) command searches each addressed line for an occurrence of the specified RE. In each line in which a match is found, all (nonoverlapped) matched strings are replaced by replacement if the global replacement indicator g appears after the command. If the global indicator does not appear, only the first occurrence of the matched string is replaced. If a number n appears after the command, only the nth occurrence of the matched string on each addressed line is replaced. It is an error for the substitution to fail on all addressed lines. Any character other than space or newline can be used instead of / to delimit the RE and replacement. Upon completion, the new current line is the last line on which a substitution occurred. (Also see WARNINGS below.)

If an ampersand (&) appears in replacement, it is replaced by the string matching the RE on the current line. The special meaning of & in this context can be suppressed by preceding it with \.

As a more general feature, the characters \n, where n is a digit, are replaced by the text matched by the nth regular subexpression of the specified RE enclosed between \( and \). When nested parenthesized subexpressions are present, n is determined by counting occurrences of \(, starting from the left.

When the character % is the only character in replacement, the replacement used in the most recent substitute command is used as the replacement in the current substitute command. The % loses its special meaning when it is in a replacement string containing more than one character or when preceded by a \.

A line can be split by substituting a newline character into it. The newline in replacement must be escaped by preceding it by \. Such substitution cannot be done as part of a g or v command list.

The value of flags is zero or more of:

n

Substitute for the nth occurrence only of the RE found on each addressed line.

g

Substitute for all nonoverlapped occurrences of the RE on each addressed line.

l

Write to standard output the final line in which a substitution was made. The line is written in the format specified for the l command.

n

Write to standard output the final line in which a substitution was made. The line is written in the format specified for the n command.

p

Write to standard output the final line in which a substitution was made. The line is written in the format specified for the p command.

(.,.)ta

Same as m command, except that a copy of the addressed lines is placed after address a (which can be 0). Upon completion, the new current line is the last line of the copy.

u

The u (undo) command nullifies the effect of the most recent command that modified anything in the buffer, that is, the most recent a, c, d, g, G, i, j, m, r, s, t, v, or V command. All changes made to the buffer by a g, G, v, or V global command are "undone" as a single change; if no changes were made by the global command (such as with g/RE/p ), the u command has no effect. The current line number is set to the value it had immediately before the command started.

(1,$)v/RE/command-list

The complement of the global command g in that the lines marked during the first step are those that do not match the RE.

(1,$)V/RE/

The complement of the interactive global command G in that the lines marked during the first step are those that do not match the RE.

(1,$)w file

The w (write) command writes the addressed lines into the named file. If the file does not exist, it is created with mode 666 (readable and writable by everyone), unless the current umask setting dictates otherwise (see umask(1). The remembered file name is not changed unless file is the very first file name encountered since ed was invoked. If no file name is given, the remembered file name, if any, is used (see the e and f commands). Upon completion, the current line address is unchanged. If the command is successful, the number of characters written is displayed.

If the file name starts with !, the rest of the line is interpreted as a shell command whose standard input is the addressed lines. Such a shell command is not remembered as the current file name.

X

A key string is demanded from the standard input. Subsequent e, r, and w commands will encrypt and decrypt the text with this key, using the algorithm of crypt(1). An explicitly empty key turns off encryption.

($)=

The line number of the addressed line is displayed. The current line address is unchanged by this command.

!shell-command

The remainder of the line after the ! is sent to the shell to be interpreted and executed as a command. Within the text of that command, the unescaped character % is replaced with the remembered file name. If a ! appears as the first character of the shell command, it is replaced with the text of the previous shell command. Thus, !! repeats the last shell command. If any expansion is performed, the expanded line is echoed. Upon completion, the current line address is unchanged.

(.+1) newline

An address alone on a line causes the addressed line to be printed. A newline alone is equivalent to .+1p. This technique is useful for stepping forward through the buffer.

If an interrupt signal (ASCII DEL or BREAK) is sent, ed prints a ? and returns to its command level.

The following size limitations apply: 256 characters per global command list, 64 characters per file name, and 32 MB characters in the buffer. The limit on the number of lines depends on the amount of user memory: each line takes 1 word.

EXTERNAL INFLUENCES

For information about the UNIX standard environment, see standards(5).

Environment Variables

SHELL determines the preferred command-line interpreter for use in all !-style commands. If this variable is null or not set, the POSIX shell, /usr/bin/sh, is used (see sh-posix(1)).

When set, TMPDIR specifies a directory to be used for temporary files, overriding the default directory, /tmp.

LANG provides a default value for internationalization variables that are unset or null. If LANG is unset or null, the default value is "C" (see lang(5)). If any internationalization variable contains an invalid setting, all internationalization variables default to "C". See environ(5).

If LC_ALL is set to a nonempty string value, it overrides the values of all the other internationalization variables, including LANG.

LC_CTYPE determines the interpretation of text as single- and/or multibyte characters, the classification of characters as printable, and the characters matched by character class expressions in regular expressions.

LC_MESSAGES determines the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output.

NLSPATH determines the location of message catalogues for the processing of LC_MESSAGES.

International Code Set Support

Single- and multibyte character code sets are supported.

DIAGNOSTICS

?

Command error. Use h or H to get a detailed explanation.

?file

Inaccessible file. Use h or H to get a detailed explanation.

If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns you if you attempt to destroy the buffer with an e or q command. ed displays ? or warning: expecting `w', then continues normal editing unless you enter a second e or q command, in which case the second command is executed. The -s or - command-line option inhibits this feature.

EXAMPLES

Make a simple substitution in file-1 from a shell script, changing the first occurrence of abc in any line to xyz, and save the changes in file-2.

cat - << EOF | ed -s file-1 1,$ s/abc/xyz/ w file-2 q EOF

Note that, if a command fails, the editor exits immediately.

WARNINGS

ed(1) allows a Maximum Line Length of 4096 characters. Attempting to create lines longer than the allowable limit causes ed(1) to produce a Line too long error message.

If a file contains lines longer than the specified limit (eg., 4096 characters), the longer lines will be truncated to the stated maximum length. Saving the file will write the truncated version over the original file, thus overwriting the original lines completely.

A ! command cannot be subject to a g or a v command.

The ! command and the ! escape from the e, r, and w commands cannot be used if the the editor is invoked from a restricted shell (see sh(1)).

The sequence \n in a regular expression does not match a newline character.

The l command does not handle DEL correctly.

Files encrypted directly with the crypt command with the null key cannot be edited (see crypt(1)).

If the editor input is coming from a command file (e.g., ed file < ed-cmd-file), the editor exits at the first failure of a command in the command file.

When reading a file, ed discards ASCII NUL characters and all characters after the last newline. This can cause unexpected behavior when using regular expressions to search for character sequences containing NUL characters or text near end-of-file.

AUTHOR

ed was developed by HP and OSF.

FILES

/tmp/ep

Temporary buffer file where p is the process number.

ed.hup

Work is saved here if the terminal is hung up.

SEE ALSO

awk(1), csh(1), crypt(1), ex(1), grep(1), ksh(1), sed(1), sh(1), sh-posix(1), stty(1), vi(1), fspec(4), environ(5), lang(5), regexp(5), standards(5).

The ed section in Text Processing: User's Guide.

STANDARDS CONFORMANCE

ed: SVID2, SVID3, XPG2, XPG3, XPG4, POSIX.2

red: SVID2, SVID3, XPG2, XPG3

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.