📄 sed.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>sed</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_001_014_1973"> </a>NAME</h4><blockquote>sed - stream editor</blockquote><h4><a name = "tag_001_014_1974"> </a>SYNOPSIS</h4><blockquote><pre><code>sed <b>[</b>-n<b>] </b><i>script</i><b>[</b><i>file</i>...<b>]</b>sed <b>[</b>-n<b>][</b>-e <i>script</i><b>]</b>...<b>[</b>-f <i>script_file</i><b>]</b>...<b>[</b><i>file</i>...<b>]</b></code></pre></blockquote><h4><a name = "tag_001_014_1975"> </a>DESCRIPTION</h4><blockquote>The<i>sed</i>utility is a stream editor that reads one or more text files,makes editing changes according toa script of editing commands,and writes the results to standard output.The script is obtained from either the<i>script</i>operand string or a combination of the option-arguments from the<b>-e</b> <i>script</i>and<b>-f</b> <i>script_file</i>options.</blockquote><h4><a name = "tag_001_014_1976"> </a>OPTIONS</h4><blockquote>The<i>sed</i>utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b> </a> ,except that the order of presentation of the<b>-e</b>and<b>-f</b>options is significant.<p>The following options are supported:<dl compact><dt><b>-e </b><i>script</i><dd>Add the editing commands specified by the<i>script</i>option-argument to the end of the script of editing commands.The<i>script</i>option-argument has the same properties as the<i>script</i>operand, described in the OPERANDS section.<dt><b>-f </b><i>script_file</i><dd>Add the editing commands in the file<i>script_file</i>to the end of the script.<dt><b>-n</b><dd>Suppress the default output(in which each line, after it is examinedfor editing, is written to standard output).Only lines explicitly selected for output will be written.</dl><p>Multiple<b>-e</b>and<b>-f</b>options may be specified.All commands areadded to the script in the order specified, regardless of their origin.</blockquote><h4><a name = "tag_001_014_1977"> </a>OPERANDS</h4><blockquote>The following operands are supported:<dl compact><dt><i>file</i><dd>A pathname of a file whose contents will be read and edited.If multiple<i>file</i>operands are specified, the named files will be read in theorder specified and the concatenation will be edited.If no<i>file</i>operands are specified,the standard input will be used.<dt><i>script</i><dd>A string to be used as the script of editing commands.The application must not present a<i>script</i>that violates the restrictions of a text fileexcept that the final character need not be anewline character.</dl></blockquote><h4><a name = "tag_001_014_1978"> </a>STDIN</h4><blockquote>The standard input will be used only if no<i>file</i>operands are specified.See the INPUT FILES section.</blockquote><h4><a name = "tag_001_014_1979"> </a>INPUT FILES</h4><blockquote>The input files must be text files.The<i>script_file</i>snamed by the<b>-f</b>option will consist of editing commands, one per line.</blockquote><h4><a name = "tag_001_014_1980"> </a>ENVIRONMENT VARIABLES</h4><blockquote>The following environment variables affect the execution of<i>sed</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_COLLATE</i><dd>Determine the locale for thebehaviour of ranges, equivalence classesand multi-character collating elementswithin regular expressions.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text data ascharacters (for example, single-versus multi-byte characters in arguments and input files),and the behaviour of character classes within regular expressions.<dt><i>LC_MESSAGES</i><dd>Determine the locale that should be used to affectthe format and contents of diagnosticmessages written to standard error.<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i></dl></blockquote><h4><a name = "tag_001_014_1981"> </a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_1982"> </a>STDOUT</h4><blockquote>The input files are written to standard output,with the editing commands specified inthe script applied.If the<b>-n</b>option is specified, only thoseinput lines selected by the scriptwill be written to standard output.</blockquote><h4><a name = "tag_001_014_1983"> </a>STDERR</h4><blockquote>Used only for diagnostic messages.</blockquote><h4><a name = "tag_001_014_1984"> </a>OUTPUT FILES</h4><blockquote>The output files are text files whose formats aredependent on the editing commands given.</blockquote><h4><a name = "tag_001_014_1985"> </a>EXTENDED DESCRIPTION</h4><blockquote>The<i>script</i>consists of editing commands, one per line,of the following form:<pre><dl compact><dt> <dd><b>[</b>address<b>[</b>,address<b>]]</b>command<b>[</b>arguments<b>]</b></dl></pre><p>Zero or moreblank charactersare accepted before the first address and before<i>command.</i><p>In default operation,<i>sed</i>cyclically copies a line of input,less its terminatingnewline character,into a<i>pattern space</i>(unless there is something left after a<b>D</b>command),applies in sequence all commands whose addressesselect that pattern space,and at the end of the script copies the pattern spaceto standard output (except when<b>-n</b>is specified) and deletes the pattern space.Whenever the pattern space is written to standard outputor a named file,<i>sed</i>will immediately follow it with anewline character.<p>Some of the commands use a<i>hold space</i>to save all or part of the<i>pattern space</i>for subsequent retrieval.The<i>pattern</i>and<i>hold</i><i>spaces</i>will each be able to hold at least 8192 bytes.<h5><a name = "tag_001_014_1985_001"> </a>Addresses in sed</h5>An addressis either empty, a decimal number that countsinput lines cumulatively across files, a "$"character that addresses the last line of input,or a context address(which consists of a regular expression as described in<xref href=sedre><a href="#tag_001_014_1985_002">Regular Expressions in sed</a></xref>,preceded and followed by a delimiter, usually a slash).<p>A command line with no addresses selects every pattern space.<p>A command line withone address selects each pattern space that matches the address.<p>A command line withtwo addresses selects the inclusive range from the firstpattern space that matches the first address tothe next pattern space that matches the second.(If the second address is a number less than or equalto the line number first selected, only one line will be selected.)Starting at the first line following the selected range,<i>sed</i>looks again for the first address.Thereafter the process is repeated.<p>Editing commands can be applied only to non-selected patternspaces by use of the negation command"!"(see<xref href=sedcmds><a href="#tag_001_014_1985_003">Editing Commands in sed</a></xref>).<h5><a name = "tag_001_014_1985_002"> </a>Regular Expressions in sed</h5><xref type="5" name="sedre"></xref>The<i>sed</i>utility supports the basic regular expressions described in the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003"><b>Basic Regular Expressions</b> </a> ,with the following additions:<ul><p><li>In a context address, the construction\cREcwhere<i>c</i>is any character other than a backslash or newline character,is identical to/RE/If the character designated by<i>c</i>appears following a backslash,then it is considered to be that literalcharacter, which does not terminate the RE.For example, in the context address\xabc\xdefx,the secondxstands for itself, so that theregular expression isabcxdef.<p><li>The escape sequence\nmatches anewline characterembedded in the pattern space.A literalnewlinecharacter must not be used in the regular expressionof a context address or in the substitute command.<p></ul><h5><a name = "tag_001_014_1985_003"> </a>Editing Commands in sed</h5><xref type="5" name="sedcmds"></xref>In the following list of commands, themaximum number of permissible addressesfor each command is indicated by<b>[</b><i>0addr</i><b>]</b>,<b>[</b><i>1addr</i><b>]</b>or<b>[</b><i>2addr</i><b>]</b>,representing zero, one or two addresses.<p>The argument<i>text</i>consists of one or more lines.Each embeddednewline characterin the text must be preceded by a backslash.Other backslashes in textare removed and the followingcharacter is treated literally.<p>The<b>r</b>and<b>w</b>commands take an optional<i>rfile</i>(or<i>wfile</i>)parameter, separated from the command letter byone or moreblank characters;implementations may allow zero separation as an extension.<p>The argument<i>rfile</i>or the argument<i>wfile</i>terminates the command line.Each<i>wfile</i>will be created before processing begins.Implementations support at leastten<i>wfile</i>arguments in the script;the actual number(greater than or equal to 10)that will be supportedby the implementation is unspecified.The use of the<i>wfile</i>parameter causes that file to be initiallycreated, if it does not exist, orwill replace the contents of an existing file.<p>The<b>b</b>,<b>r</b>,<b>s</b>,<b>t</b>,<b>w</b>,<b>y</b>,<b>!</b>and<b>:</b>commands accept additional arguments.The following synopses indicate which argumentsmust be separated from the commands by a singlespace character.<p>Two of the commands take a<i>command-list</i>,which is a list of<i>sed</i>commands separated bynewline characters,as follows:<pre><code>{ <i>command</i><i>command</i>. . .}</code></pre><p>The"{"can be preceded withblank characters
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -