跳过导航链接 | |
退出打印视图 | |
手册页第 1 部分:用户命令 Oracle Solaris 11.1 Information Library (简体中文) |
- stream editor
/usr/bin/sed [-n] script [file]...
/usr/bin/sed [-n] [-e script]... [-f script_file]... [file]...
/usr/xpg4/bin/sed [-n] script [file]...
/usr/xpg4/bin/sed [-n] [-e script]... [-f script_file]... [file]...
The sed utility is a stream editor that reads one or more text files, makes editing changes according to a script of editing commands, and writes the results to standard output. The script is obtained from either the script operand string, or a combination of the option-arguments from the -e script and -f script_file options.
The sed utility is a text editor. It cannot edit binary files or files containing ASCII NUL (\0) characters or very long lines.
The following options are supported:
script is an edit command for sed. See USAGE below for more information on the format of script. If there is just one -e option and no -f options, the flag -e may be omitted.
Takes the script from script_file. script_file consists of editing commands, one per line.
Suppresses the default output.
Multiple -e and -f options may be specified. All commands are added to the script in the order specified, regardless of their origin.
The following operands are supported:
A path name of a file whose contents will be read and edited. If multiple file operands are specified, the named files will be read in the order specified and the concatenation will be edited. If no file operands are specified, the standard input will be used.
A string to be used as the script of editing commands. The application must not present a script that violates the restrictions of a text file except that the final character need not be a NEWLINE character.
A script consists of editing commands, one per line, of the following form:
[ address [ , address ] ] command [ arguments ]
Zero or more blank characters are accepted before the first address and before command. Any number of semicolons are accepted before the first address.
In normal operation, sed cyclically copies a line of input (less its terminating NEWLINE character) into a pattern space (unless there is something left after a D command), applies in sequence all commands whose addresses select that pattern space, and copies the resulting pattern space to the standard output (except under -n) and deletes the pattern space. Whenever the pattern space is written to standard output or a named file, sed will immediately follow it with a NEWLINE character.
Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval. The pattern and hold spaces will each be able to hold at least 8192 bytes.
An address is either empty, a decimal number that counts input lines cumulatively across files, a $ that addresses the last line of input, or a context address, which consists of a /regular expression/ as described on the regexp(5) manual page.
A command line with no addresses selects every pattern space.
A command line with one address selects each pattern space that matches the address.
A command line with two addresses selects the inclusive range from the first pattern space that matches the first address through the next pattern space that matches the second address. Thereafter the process is repeated, looking again for the first address. (If the second address is a number less than or equal to the line number selected by the first address, only the line corresponding to the first address is selected.)
Typically, address are separated from each other by a comma (,). They may also be separated by a semicolon (;).
sed supports the basic regular expressions described on the regexp(5) manual page, with the following additions:
In a context address, the construction \cREc, where c is any character other than a backslash or NEWLINE character, is identical to /RE/. If the character designated by c appears following a backslash, then it is considered to be that literal character, which does not terminate the RE. For example, in the context address \xabc\xdefx, the second x stands for itself, so that the regular expression is abcxdef.
The escape sequence \n matches a NEWLINE character embedded in the pattern space. A literal NEWLINE character must not be used in the regular expression of a context address or in the substitute command.
Editing commands can be applied only to non-selected pattern spaces by use of the negation command ! (described below).
In the following list of functions the maximum number of permissible addresses for each function is indicated.
The r and w commands take an optional rfile (or wfile) parameter, separated from the command letter by one or more blank characters.
Multiple commands can be specified by separating them with a semicolon (;) on the same command line.
The text argument consists of one or more lines, all but the last of which end with \ to hide the NEWLINE. Each embedded NEWLINE character in the text must be preceded by a backslash. Other backslashes in text are removed and the following character is treated literally. Backslashes in text are treated like backslashes in the replacement string of an s command, and may be used to protect initial blanks and tabs against the stripping that is done on every script line. The rfile or wfile argument must terminate the command line and must be preceded by exactly one blank. The use of the wfile parameter causes that file to be initially created, if it does not exist, or will replace the contents of an existing file. There can be at most 10 distinct wfile arguments.
Regular expressions match entire strings, not just individual lines, but a NEWLINE character is matched by \n in a sed RE. A NEWLINE character is not allowed in an RE. Also notice that \n cannot be used to match a NEWLINE character at the end of an input line; NEWLINE characters appear in the pattern space as a result of the N editing command.
Two of the commands take a command-list, which is a list of sed commands separated by NEWLINE characters, as follows:
{ command command }
The { can be preceded with blank characters and can be followed with white space. The commands can be preceded by white space. The terminating } must be preceded by a NEWLINE character and can be preceded or followed by <blank>s. The braces may be preceded or followed by <blank>s. The command may be preceded by <blank>s, but may not be followed by <blank>s.
The following table lists the functions, with the maximum number of permissible addresses.
|
|
See largefile(5) for the description of the behavior of sed when encountering files greater than or equal to 2 Gbyte ( 231 bytes).
示例 1 An example sed script
This sed script simulates the BSD cat -s command, squeezing excess blank lines from standard input.
sed -n ' # Write non-empty lines. /./ { p d } # Write a single empty line, then look for more empty lines. /^$/ p # Get next line, discard the held <newline> (empty line), # and look for more empty lines. :Empty /^$/ { N s/.// b Empty } # Write the non-empty line before going back to search # for the first in a set of empty lines. p '
See environ(5) for descriptions of the following environment variables that affect the execution of sed: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.
The following exit values are returned:
Successful completion.
An error occurred.
See attributes(5) for descriptions of the following attributes:
|
|
awk(1), ed(1), grep(1), attributes(5), environ(5), largefile(5), regexp(5), standards(5)