?? sed
字號:
appear in the output..SHExample:.LPThe list of editing commands:.LP.in +.5i.nfna\eXXXXd.in -.5i.fi.LPapplied to our standard input, produces:.LP.in +.5i.nfIn Xanadu did Kubhla KhanXXXXWhere Alph, the sacred river, ranXXXXDown to a sunless sea..in -.5i.fi.LPIn this particular case,the same effect would be produced by eitherof the two following command lists:.LP.in +.5i.nfn ni\e c\eXXXX XXXXd.in -.5i.fi.LP.in 0.SH3.2. Substitute Function.LPOne very important function changes parts of lines selected bya context search within the line..if t .sp .5.in +1i.ti -.5i(2)s<pattern><replacement><flags> -- substitute.if t .sp .5The.ulsfunction replaces.ulpartof a line (selected by <pattern>) with <replacement>.It can best be read:.if t .sp .5.ti +1iSubstitute for <pattern>, <replacement>.if t .sp .5The <pattern> argument contains a pattern,exactly like the patterns in addresses (see 2.2 above).The only difference between <pattern> and a context address isthat the context address must be delimited by slash (`/') characters;<pattern> may be delimited by any character other than space ornewline..if t .sp .5By default, only the first string matched by <pattern> is replaced,but see the.ulgflag below..if t .sp .5The<replacement> argument begins immediately after thesecond delimiting character of <pattern>, and must be followedimmediately by another instance of the delimiting character.(Thus there are exactly .ulthreeinstances of the delimiting character.).if t .sp .5The <replacement> is not a pattern,and the characters which are special in patternsdo not have special meaning in <replacement>.Instead, other characters are special:.if t .sp .5.in +1i.ti -.5i& is replaced by the string matched by <pattern>.if t .sp .5.ti -.5i.ul\ed(where.uldis a single digit) is replaced by the \fId\fRth substringmatched by parts of <pattern> enclosed in `\e(' and `\e)'.If nested substrings occur in <pattern>, the \fId\fRthis determined by counting opening delimiters (`\e(')..if t .sp .5As in patterns, special characters may be madeliteral by preceding them with backslash (`\e')..if t .sp .5.in -1iThe <flags> argument may contain the following flags:.if t .sp .5.in +1i.ti -.5ig -- substitute <replacement> for all (non-overlapping)instances of <pattern> in the line.After a successful substitution, the scan for the nextinstance of <pattern> begins just after the end of theinserted characters; characters put into the line from<replacement> are not rescanned..if t .sp .5.ti -.5ip -- print the line if a successful replacement was done.The.ulpflag causes the line to be written to the output if and onlyif a substitution was actually made by the.ulsfunction.Notice that if several.ulsfunctions, each followed by a.ulpflag, successfully substitute in the same input line,multiple copies of the line will be written to theoutput: one for each successful substitution..if t .sp .5.ti -.5iw <filename> -- write the line to a file if a successfulreplacement was done.The.ulwflag causes lines which are actually substituted by the.ulsfunction to be written to a file named by <filename>.If <filename> exists before.ulsedis run, it is overwritten;if not, it is created..if t .sp .5A single space must separate.ulwand <filename>..if t .sp .5The possibilities of multiple, somewhat different copies ofone input line being written are the same as for .ulp..if t .sp .5A maximum of 10 different file names may be mentioned after.ulwflags and.ulwfunctions (see below), combined..in 0.SHExamples:.LPThe following command, applied to our standard input,.LP.in +.5is/to/by/w changes.in -.5i.LPproduces, on the standard output:.LP.in +.5i.nfIn Xanadu did Kubhla KhanA stately pleasure dome decree:Where Alph, the sacred river, ranThrough caverns measureless by manDown by a sunless sea..fi.in -.5i.LPand, on the file `changes':.LP.in +.5i.nfThrough caverns measureless by manDown by a sunless sea..fi.in -.5i.LPIf the nocopy option is in effect, the command:.LP.in +.5i.nfs/[.,;?:]/*P&*/gp.fi.in -.5i.LPproduces:.LP.in +.5i.nfA stately pleasure dome decree*P:*Where Alph*P,* the sacred river*P,* ranDown to a sunless sea*P.*.LP.in -.5i.fiFinally, to illustrate the effect of the.ulgflag,the command:.LP.in +.5i.nf/X/s/an/AN/p.in -.5i.fi.LPproduces (assuming nocopy mode):.in +.5i.LP.nfIn XANadu did Kubhla Khan.fi.in -.5i.LPand the command:.LP.in +.5i.nf/X/s/an/AN/gp.in -.5i.fi.LPproduces:.LP.in +.5i.nfIn XANadu did Kubhla KhAN.in -.5i.fi.LP.in 0.SH3.3. Input-output Functions.LP.in +1i.ti -.5i(2)p -- print.if t .sp .5The print function writes the addressed lines to the standard output file.They are written at the time the.ulpfunction is encountered, regardless of what succeedingediting commands may do to the lines..if t .sp .5.ti -.5i(2)w <filename> -- write on <filename>.if t .sp .5The write function writes the addressed lines to the file namedby <filename>.If the file previously existed, it is overwritten; if not, it is created.The lines are written exactly as they exist when the write functionis encountered for each line, regardless of what subsequentediting commands may do to them..if t .sp .5Exactly one space must separate the.ulwand <filename>..if t .sp .5A maximum of ten different files may be mentioned in writefunctions and.ulwflags after.ulsfunctions, combined..if t .sp .5.ti -.5i(1)r <filename> -- read the contents of a file.if t .sp .5The read function reads the contents of <filename>, and appendsthem after the line matched by the address.The file is read and appended regardless of what subsequentediting commands do to the line which matched its address.If.ulrand.ulafunctions are executed on the same line,the text from the .ulafunctions and the.ulrfunctions is written to the output in the order thatthe functions are executed..if t .sp .5Exactly one space must separate the.ulrand <filename>.If a file mentioned by a.ulrfunction cannot be opened, it is considered a null file,not an error, and no diagnostic is given..if t .sp .5.in -1iNOTE:Since there is a limit to the number of files that can be openedsimultaneously, care should be taken that no more than tenfiles be mentioned in.ulwfunctions or flags; that number is reduced by one if any.ulrfunctions are present.(Only one read file is open at one time.).in 0.SHExamples.LPAssume that the file `note1'has the following contents:.LP.in +1iNote: Kubla Khan (more properly Kublai Khan; 1216-1294)was the grandson and most eminent successor of Genghiz(Chingiz) Khan, and founder of the Mongol dynasty in China..LP.in 0Then the following command:.LP.nf.in +.5i/Kubla/r note1.in -.5i.fi.LPproduces:.LP.nf.in +.5iIn Xanadu did Kubla Khan.in +.5i.fiNote: Kubla Khan (more properly Kublai Khan; 1216-1294)was the grandson and most eminent successor of Genghiz(Chingiz) Khan, and founder of the Mongol dynasty in China..in -.5i.nfA stately pleasure dome decree:Where Alph, the sacred river, ranThrough caverns measureless to manDown to a sunless sea..in -.5i.fi.LP.in 0.SH3.4.Multiple Input-line Functions.LPThree functions, all spelled with capital letters, dealspecially with pattern spaces containing imbedded newlines;they are intended principally to provide pattern matches acrosslines in the input..if t .sp .5.in +1i.ti -.5i(2)N -- Next line.if t .sp .5The next input line is appended to the current line in thepattern space; the two input lines are separated by an imbeddednewline.Pattern matches may extend across the imbedded newline(s)..if t .sp .5.ti -.5i(2)D -- Delete first part of the pattern space.if t .sp .5Delete up to and including the first newline characterin the current pattern space.If the pattern space becomes empty (the only newlinewas the terminal newline),read another line from the input.In any case, begin the list of editing commands againfrom its beginning..if t .sp .5.ti -.5i(2)P -- Print first part of the pattern space.if t .sp .5Print up to and including the first newline in the pattern space..if t .sp .5.in 0The .ulPand.ulDfunctions are equivalent to their lower-case counterpartsif there are no imbedded newlines in the pattern space..in 0.SH3.5. Hold and Get Functions.LPFour functions save and retrieve part of the input for possible lateruse..if t .sp .5.in 1i.ti -.5i(2)h -- hold pattern space.if t .sp .5The.ulhfunctions copies the contents of the pattern spaceinto a hold area (destroying the previous contents of thehold area)..if t .sp .5.ti -.5i(2)H -- Hold pattern space.if t .sp .5The.ulHfunction appends the contents of the pattern spaceto the contents of the hold area; the former and new contentsare separated by a newline..if t .sp .5.ti -.5i(2)g -- get contents of hold area.if t .sp .5The.ulgfunction copies the contents of the hold area intothe pattern space (destroying the previous contents of thepattern space)..if t .sp .5.ti -.5i(2)G -- Get contents of hold area.if t .sp .5The.ulGfunction appends the contents of the hold area to thecontents of the pattern space; the former and new contents are separated bya newline..if t .sp .5.ti -.5i(2)x -- exchange.if t .sp .5The exchange command interchanges the contentsof the pattern space and the hold area..in 0.SHExample.LPThe commands.nf.if t .sp .5 1h 1s/ did.*// 1x G s/\en/ :/.if t .sp .5.fiapplied to our standard example, produce:.nf.if t .sp .5 In Xanadu did Kubla Khan :In Xanadu A stately pleasure dome decree: :In Xanadu Where Alph, the sacred river, ran :In Xanadu Through caverns measureless to man :In Xanadu Down to a sunless sea. :In Xanadu.if t .sp .5.fi.SH3.6. Flow-of-Control Functions.LPThese functions do no editing on the inputlines, but control the application of functionsto the lines selected by the address part..if t .sp .5.in +1i.ti -.5i(2)! -- Don't.if t .sp .5The.ulDon'tcommand causes the next command(written on the same line), to be applied to all and only those input lines.ulnotselected by the adress part..if t .sp .5.ti -.5i(2){ -- Grouping.if t .sp .5The grouping command `{' causes thenext set of commands to be applied(or not applied) as a block to theinput lines selected by the addressesof the grouping command.The first of the commands under control of the groupingmay appear on the same line as the`{' or on the next line..LPThe group of commands is terminated by amatching `}' standing on a line by itself..LPGroups can be nested..ti -.5i.if t .sp .5(0):<label> -- place a label.if t .sp .5The label function marks a place in the listof editing commands which may be referred to by.ulband.ultfunctions.The <label> may be any sequence of eight or fewer characters;if two different colon functions have identical labels,a compile time diagnostic will be generated, andno execution attempted..if t .sp .5.ti -.5i(2)b<label> -- branch to label.if t .sp .5The branch function causes the sequence of editing commands beingapplied to the current input line to be restarted immediatelyafter the place where a colon function with the same <label>was encountered.If no colon function with the same label can be found afterall the editing commands have been compiled, a compile time diagnosticis produced, and no execution is attempted..if t .sp .5A.ulbfunction with no <label> is taken to be a branch to the end of thelist of editing commands;whatever should be done with the current input line is done, andanother input line is read; the list of editing commands is restarted from thebeginning on the new line..if t .sp .5.ti -.5i(2)t<label> -- test substitutions.if t .sp .5The.ultfunction tests whether .ulanysuccessful substitutions have been made on the current inputline;if so, it branches to <label>;if not, it does nothing.The flag which indicates that a successful substitution hasbeen executed is reset by:.if t .sp .5.in +1i1) reading a new input line, or.br2) executing a.ultfunction..if t .sp .5.in 0.SH3.7. Miscellaneous Functions.LP.in +1i.ti -.5i(1)= -- equals.if t .sp .5The = function writes to the standard output the line number of theline matched by its address..if t .sp .5.ti -.5i(1)q -- quit.if t .sp .5The.ulqfunction causes the current line to be written to theoutput (if it should be), any appended or read text to be written, andexecution to be terminated..in 0.SH.SHReference.IP [1]Ken Thompson and Dennis M. Ritchie,.ulThe UNIX Programmer's Manual.Bell Laboratories, 1978.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -