Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   ed.1p    ( 1 )

редактировать текст (edit text)

Расширенное описание (Extended description)

The ed utility shall operate on a copy of the file it is editing;
       changes made to the copy shall have no effect on the file until a
       w (write) command is given. The copy of the text is called the
       buffer.

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 very often can be omitted. If the -p option is specified, the prompt string shall be written to standard output before each command is read.

In general, only one command can appear on a line. Certain commands allow text to be input. This text is placed in the appropriate place in the buffer. While ed is accepting text, it is said to be in input mode. In this mode, no commands shall be recognized; all input is merely collected. Input mode is terminated by entering a line consisting of two characters: a <period> ('.') followed by a <newline>. This line is not considered part of the input text.

Regular Expressions in ed The ed utility shall support basic regular expressions, as described in the Base Definitions volume of POSIX.1‐2017, Section 9.3, Basic Regular Expressions. Since regular expressions in ed are always matched against single lines (excluding the terminating <newline> characters), never against any larger section of text, there is no way for a regular expression to match a <newline>.

A null RE shall be equivalent to the last RE encountered.

Regular expressions are used in addresses to specify lines, and in some commands (for example, the s substitute command) to specify portions of a line to be substituted.

Addresses in ed Addressing in ed relates to the current line. Generally, the current line is the last line affected by a command. The current line number is the address of the current line. If the edit buffer is not empty, the initial value for the current line shall be the last line in the edit buffer; otherwise, zero.

Addresses shall be constructed as follows:

1. The <period> character ('.') shall address the current line.

2. The <dollar-sign> character ('$') shall address the last line of the edit buffer.

3. The positive decimal number n shall address the nth line of the edit buffer.

4. The <apostrophe>-x character pair ("'x") shall address the line marked with the mark name character x, which shall be a lowercase letter from the portable character set. It shall be an error if the character has not been set to mark a line or if the line that was marked is not currently present in the edit buffer.

5. A BRE enclosed by <slash> characters ('/') shall address the first line found by searching forwards from the line following the current line toward the end of the edit buffer and stopping at the first line for which the line excluding the terminating <newline> matches the BRE. The BRE consisting of a null BRE delimited by a pair of <slash> characters shall address the next line for which the line excluding the terminating <newline> matches the last BRE encountered. In addition, the second <slash> can be omitted at the end of a command line. Within the BRE, a <backslash>-<slash> pair ("\/") shall represent a literal <slash> instead of the BRE delimiter. If necessary, the search shall wrap around to the beginning of the buffer and continue up to and including the current line, so that the entire buffer is searched.

6. A BRE enclosed by <question-mark> characters ('?') shall address the first line found by searching backwards from the line preceding the current line toward the beginning of the edit buffer and stopping at the first line for which the line excluding the terminating <newline> matches the BRE. The BRE consisting of a null BRE delimited by a pair of <question- mark> characters ("??") shall address the previous line for which the line excluding the terminating <newline> matches the last BRE encountered. In addition, the second <question- mark> can be omitted at the end of a command line. Within the BRE, a <backslash>-<question-mark> pair ("\?") shall represent a literal <question-mark> instead of the BRE delimiter. If necessary, the search shall wrap around to the end of the buffer and continue up to and including the current line, so that the entire buffer is searched.

7. A <plus-sign> ('+') or <hyphen-minus> character ('-') followed by a decimal number shall address the current line plus or minus the number. A <plus-sign> or <hyphen-minus> character not followed by a decimal number shall address the current line plus or minus 1.

Addresses can be followed by zero or more address offsets, optionally <blank>-separated. Address offsets are constructed as follows:

* A <plus-sign> or <hyphen-minus> character followed by a decimal number shall add or subtract, respectively, the indicated number of lines to or from the address. A <plus- sign> or <hyphen-minus> character not followed by a decimal number shall add or subtract 1 to or from the address.

* A decimal number shall add the indicated number of lines to the address.

It shall not be an error for an intermediate address value to be less than zero or greater than the last line in the edit buffer. It shall be an error for the final address value to be less than zero or greater than the last line in the edit buffer. It shall be an error if a search for a BRE fails to find a matching line.

Commands accept zero, one, or two addresses. If more than the required number of addresses are provided to a command that requires zero addresses, it shall be an error. Otherwise, if more than the required number of addresses are provided to a command, the addresses specified first shall be evaluated and then discarded until the maximum number of valid addresses remain, for the specified command.

Addresses shall be separated from each other by a <comma> (',') or <semicolon> character (';'). In the case of a <semicolon> separator, the current line ('.') shall be set to the first address, and only then will the second address be calculated. This feature can be used to determine the starting line for forwards and backwards searches; see rules 5. and 6.

Addresses can be omitted on either side of the <comma> or <semicolon> separator, in which case the resulting address pairs shall be as follows:

┌──────────┬─────────────┐ │Specified Resulting │ ├──────────┼─────────────┤ │, │ 1 , $ │ │, addr │ 1 , addr │ │addr , │ addr , addr │ │; │ . ; $ │ │; addr │ . ; addr │ │addr ; │ addr ; addr │ └──────────┴─────────────┘ Any <blank> characters included between addresses, address separators, or address offsets shall be ignored.

Commands in ed In the following list of ed commands, the default addresses are shown in parentheses. The number of addresses shown in the default shall be the number expected by the command. The parentheses are not part of the address; they show that the given addresses are the default.

It is generally invalid for more than one command to appear on a line. However, any command (except e, E, f, q, Q, r, w, and !) can be suffixed by the letter l, n, or p; in which case, except for the l, n, and p commands, the command shall be executed and then the new current line shall be written as described below under the l, n, and p commands. When an l, n, or p suffix is used with an l, n, or p command, the command shall write to standard output as described below, but it is unspecified whether the suffix writes the current line again in the requested format or whether the suffix has no effect. For example, the pl command (base p command with an l suffix) shall either write just the current line or write it twice—once as specified for p and once as specified for l. Also, the g, G, v, and V commands shall take a command as a parameter.

Each address component can be preceded by zero or more <blank> characters. The command letter can be preceded by zero or more <blank> characters. If a suffix letter (l, n, or p) is given, the application shall ensure that it immediately follows the command.

The e, E, f, r, and w commands shall take an optional file parameter, separated from the command letter by one or more <blank> characters.

If changes have been made in the buffer since the last w command that wrote the entire buffer, ed shall warn the user if an attempt is made to destroy the editor buffer via the e or q commands. The ed utility shall write the string:

"?\n"

(followed by an explanatory message if help mode has been enabled via the H command) to standard output and shall continue in command mode with the current line number unchanged. If the e or q command is repeated with no intervening command, it shall take effect.

If a terminal disconnect (see the Base Definitions volume of POSIX.1‐2017, Chapter 11, General Terminal Interface, Modem Disconnect and Closing a Device Terminal), is detected:

* If accompanied by a SIGHUP signal, the ed utility shall operate as described in the ASYNCHRONOUS EVENTS section for a SIGHUP signal.

* If not accompanied by a SIGHUP signal, the ed utility shall act as if an end-of-file had been detected on standard input.

If an end-of-file is detected on standard input:

* If the ed utility is in input mode, ed shall terminate input mode and return to command mode. It is unspecified if any partially entered lines (that is, input text without a terminating <newline>) are discarded from the input text.

* If the ed utility is in command mode, it shall act as if a q command had been entered.

If the closing delimiter of an RE or of a replacement string (for example, '/') in a g, G, s, v, or V command would be the last character before a <newline>, that delimiter can be omitted, in which case the addressed line shall be written. For example, the following pairs of commands are equivalent:

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

If an invalid command is entered, ed shall write the string:

"?\n"

(followed by an explanatory message if help mode has been enabled via the H command) to standard output and shall continue in command mode with the current line number unchanged.

Append Command Synopsis:

(.)a <text> .

The a command shall read the given text and append it after the addressed line; the current line number shall become the address of the last inserted line or, if there were none, the addressed line. Address 0 shall be valid for this command; it shall cause the appended text to be placed at the beginning of the buffer.

Change Command Synopsis:

(.,.)c <text> .

The c command shall delete the addressed lines, then accept input text that replaces these lines; the current line shall be set to the address of the last line input; or, if there were none, at the line after the last line deleted; if the lines deleted were originally at the end of the buffer, the current line number shall be set to the address of the new last line; if no lines remain in the buffer, the current line number shall be set to zero. Address 0 shall be valid for this command; it shall be interpreted as if address 1 were specified.

Delete Command Synopsis:

(.,.)d

The d command shall delete the addressed lines from the buffer. The address of the line after the last line deleted shall become the current line number; if the lines deleted were originally at the end of the buffer, the current line number shall be set to the address of the new last line; if no lines remain in the buffer, the current line number shall be set to zero.

Edit Command Synopsis:

e [file]

The e command shall delete the entire contents of the buffer and then read in the file named by the pathname file. The current line number shall be set to the address of the last line of the buffer. If no pathname is given, the currently remembered pathname, if any, shall be used (see the f command). The number of bytes read shall be written to standard output, unless the -s option was specified, in the following format:

"%d\n", <number of bytes read>

The name file shall be remembered for possible use as a default pathname in subsequent e, E, r, and w commands. If file is replaced by '!', the rest of the line shall be taken to be a shell command line whose output is to be read. Such a shell command line shall not be remembered as the current file. All marks shall be discarded upon the completion of a successful e command. If the buffer has changed since the last time the entire buffer was written, the user shall be warned, as described previously.

Edit Without Checking Command Synopsis:

E [file]

The E command shall possess all properties and restrictions of the e command except that the editor shall not check to see whether any changes have been made to the buffer since the last w command.

Filename Command Synopsis:

f [file]

If file is given, the f command shall change the currently remembered pathname to file; whether the name is changed or not, it shall then write the (possibly new) currently remembered pathname to the standard output in the following format:

"%s\n", <pathname>

The current line number shall be unchanged.

Global Command Synopsis:

(1,$)g/RE/command list

In the g command, the first step shall be to mark every line for which the line excluding the terminating <newline> matches the given RE. Then, going sequentially from the beginning of the file to the end of the file, the given command list shall be executed for each marked line, with the current line number set to the address of that line. Any line modified by the command list shall be unmarked. When the g command completes, the current line number shall have the value assigned by the last command in the command list. If there were no matching lines, the current line number shall not be changed. A single command or the first of a list of commands shall appear on the same line as the global command. All lines of a multi-line list except the last line shall be ended with a <backslash> preceding the terminating <newline>; the a, i, and c commands and associated input are permitted. The '.' terminating input mode can be omitted if it would be the last line of the command list. An empty command list shall be equivalent to the p command. The use of the g, G, v, V, and ! commands in the command list produces undefined results. Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>.

Interactive Global Command Synopsis:

(1,$)G/RE/

In the G command, the first step shall be to mark every line for which the line excluding the terminating <newline> matches the given RE. Then, for every such line, that line shall be written, the current line number shall be set to the address of that line, and any one command (other than one of the a, c, i, g, G, v, and V commands) shall be read and executed. A <newline> shall act as a null command (causing no action to be taken on the current line); an '&' shall cause the re-execution of the most recent non-null command executed within the current invocation of G. Note that the commands input as part of the execution of the G command can address and affect any lines in the buffer. Any line modified by the command shall be unmarked. The final value of the current line number shall be the value set by the last command successfully executed. (Note that the last command successfully executed shall be the G command itself if a command fails or the null command is specified.) If there were no matching lines, the current line number shall not be changed. The G command can be terminated by a SIGINT signal. Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE and the replacement. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>.

Help Command Synopsis:

h

The h command shall write a short message to standard output that explains the reason for the most recent '?' notification. The current line number shall be unchanged.

Help-Mode Command Synopsis:

H

The H command shall cause ed to enter a mode in which help messages (see the h command) shall be written to standard output for all subsequent '?' notifications. The H command alternately shall turn this mode on and off; it is initially off. If the help-mode is being turned on, the H command also explains the previous '?' notification, if there was one. The current line number shall be unchanged.

Insert Command Synopsis:

(.)i <text> .

The i command shall insert the given text before the addressed line; the current line is set to the last inserted line or, if there was none, to the addressed line. This command differs from the a command only in the placement of the input text. Address 0 shall be valid for this command; it shall be interpreted as if address 1 were specified.

Join Command Synopsis:

(.,.+1)j

The j command shall join contiguous lines by removing the appropriate <newline> characters. If exactly one address is given, this command shall do nothing. If lines are joined, the current line number shall be set to the address of the joined line; otherwise, the current line number shall be unchanged.

Mark Command Synopsis:

(.)kx

The k command shall mark the addressed line with name x, which the application shall ensure is a lowercase letter from the portable character set. The address "'x" shall then refer to this line; the current line number shall be unchanged.

List Command Synopsis:

(.,.)l

The l command shall write to standard output the addressed lines in a visually unambiguous form. The characters listed in the Base Definitions volume of POSIX.1‐2017, Table 5-1, Escape Sequences and Associated Actions ('\\', '\a', '\b', '\f', '\r', '\t', '\v') shall be written as the corresponding escape sequence; the '\n' in that table is not applicable. Non-printable characters not in the table shall be written as one three-digit octal number (with a preceding <backslash> character) for each byte in the character (most significant byte first).

Long lines shall be folded, with the point of folding indicated by <newline> preceded by a <backslash>; the length at which folding occurs is unspecified, but should be appropriate for the output device. The end of each line shall be marked with a '$', and '$' characters within the text shall be written with a preceding <backslash>. An l command can be appended to any other command other than e, E, f, q, Q, r, w, or !. The current line number shall be set to the address of the last line written.

Move Command Synopsis:

(.,.)maddress

The m command shall reposition the addressed lines after the line addressed by address. Address 0 shall be valid for address and cause the addressed lines to be moved to the beginning of the buffer. It shall be an error if address address falls within the range of moved lines. The current line number shall be set to the address of the last line moved.

Number Command Synopsis:

(.,.)n

The n command shall write to standard output the addressed lines, preceding each line by its line number and a <tab>; the current line number shall be set to the address of the last line written. The n command can be appended to any command other than e, E, f, q, Q, r, w, or !.

Print Command Synopsis:

(.,.)p

The p command shall write to standard output the addressed lines; the current line number shall be set to the address of the last line written. The p command can be appended to any command other than e, E, f, q, Q, r, w, or !.

Prompt Command Synopsis:

P

The P command shall cause ed to prompt with an <asterisk> ('*') (or string, if -p is specified) for all subsequent commands. The P command alternatively shall turn this mode on and off; it shall be initially on if the -p option is specified; otherwise, off. The current line number shall be unchanged.

Quit Command Synopsis:

q

The q command shall cause ed to exit. If the buffer has changed since the last time the entire buffer was written, the user shall be warned, as described previously.

Quit Without Checking Command Synopsis:

Q

The Q command shall cause ed to exit without checking whether changes have been made in the buffer since the last w command.

Read Command Synopsis:

($)r [file]

The r command shall read in the file named by the pathname file and append it after the addressed line. If no file argument is given, the currently remembered pathname, if any, shall be used (see the e and f commands). The currently remembered pathname shall not be changed unless there is no remembered pathname. Address 0 shall be valid for r and shall cause the file to be read at the beginning of the buffer. If the read is successful, and -s was not specified, the number of bytes read shall be written to standard output in the following format:

"%d\n", <number of bytes read>

The current line number shall be set to the address of the last line read in. If file is replaced by '!', the rest of the line shall be taken to be a shell command line whose output is to be read. Such a shell command line shall not be remembered as the current pathname.

Substitute Command Synopsis:

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

The s command shall search each addressed line for an occurrence of the specified RE and replace either the first or all (non- overlapped) matched strings with the replacement; see the following description of the g suffix. It is an error if the substitution fails on every addressed line. Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE and the replacement. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>. The current line shall be set to the address of the last line on which a substitution occurred.

An <ampersand> ('&') appearing in the replacement shall be replaced by the string matching the RE on the current line. The special meaning of '&' in this context can be suppressed by preceding it by <backslash>. As a more general feature, the characters '\n', where n is a digit, shall be replaced by the text matched by the corresponding back-reference expression. If the corresponding back-reference expression does not match, then the characters '\n' shall be replaced by the empty string. When the character '%' is the only character in the replacement, the replacement used in the most recent substitute command shall be used as the replacement in the current substitute command; if there was no previous substitute command, the use of '%' in this manner shall be an error. The '%' shall lose its special meaning when it is in a replacement string of more than one character or is preceded by a <backslash>. For each <backslash> encountered in scanning replacement from beginning to end, the following character shall lose its special meaning (if any). It is unspecified what special meaning is given to any character other than <backslash>, '&', '%', or digits.

A line can be split by substituting a <newline> into it. The application shall ensure it escapes the <newline> in the replacement by preceding it by <backslash>. Such substitution cannot be done as part of a g or v command list. The current line number shall be set to the address of the last line on which a substitution is performed. If no substitution is performed, the current line number shall be unchanged. If a line is split, a substitution shall be considered to have been performed on each of the new lines for the purpose of determining the new current line number. A substitution shall be considered to have been performed even if the replacement string is identical to the string that it replaces.

The application shall ensure that the value of flags is zero or more of:

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

g Globally substitute for all non-overlapping instances of the RE rather than just the first one. If both g and count are specified, the results are unspecified.

l Write to standard output the final line in which a substitution was made. The line shall be 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 shall be 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 shall be written in the format specified for the p command.

Copy Command Synopsis:

(.,.)taddress

The t command shall be equivalent to the m command, except that a copy of the addressed lines shall be placed after address address (which can be 0); the current line number shall be set to the address of the last line added.

Undo Command Synopsis:

u

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

Global Non-Matched Command Synopsis:

(1,$)v/RE/command list

This command shall be equivalent to the global command g except that the lines that are marked during the first step shall be those for which the line excluding the terminating <newline> does not match the RE.

Interactive Global Not-Matched Command Synopsis:

(1,$)V/RE/

This command shall be equivalent to the interactive global command G except that the lines that are marked during the first step shall be those for which the line excluding the terminating <newline> does not match the RE.

Write Command Synopsis:

(1,$)w [file]

The w command shall write the addressed lines into the file named by the pathname file. The command shall create the file, if it does not exist, or shall replace the contents of the existing file. The currently remembered pathname shall not be changed unless there is no remembered pathname. If no pathname is given, the currently remembered pathname, if any, shall be used (see the e and f commands); the current line number shall be unchanged. If the command is successful, the number of bytes written shall be written to standard output, unless the -s option was specified, in the following format:

"%d\n", <number of bytes written>

If file begins with '!', the rest of the line shall be taken to be a shell command line whose standard input shall be the addressed lines. Such a shell command line shall not be remembered as the current pathname. This usage of the write command with '!' shall not be considered as a ``last w command that wrote the entire buffer'', as described previously; thus, this alone shall not prevent the warning to the user if an attempt is made to destroy the editor buffer via the e or q commands.

Line Number Command Synopsis:

($)=

The line number of the addressed line shall be written to standard output in the following format:

"%d\n", <line number>

The current line number shall be unchanged by this command.

Shell Escape Command Synopsis:

!command

The remainder of the line after the '!' shall be sent to the command interpreter to be interpreted as a shell command line. Within the text of that shell command line, the unescaped character '%' shall be replaced with the remembered pathname; if a '!' appears as the first character of the command, it shall be replaced with the text of the previous shell command executed via '!'. Thus, "!!" shall repeat the previous !command. If any replacements of '%' or '!' are performed, the modified line shall be written to the standard output before command is executed. The ! command shall write:

"!\n"

to standard output upon completion, unless the -s option is specified. The current line number shall be unchanged.

Null Command Synopsis:

(.+1)

An address alone on a line shall cause the addressed line to be written. A <newline> alone shall be equivalent to "+1p". The current line number shall be set to the address of the written line.