sed的地址表示

man sed

 

Addresses

       Sed  commands  can be given with no addresses, in which case the command will be executed for all input lines; with one address, in which case the command will only be executed for input lines which match that address; or with two addresses, in which case the command will be executed for  all input  lines  which match the inclusive range of lines starting from the first address and continuing to the second address.  Three things to note about address ranges: the syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the  line  which  addr1  matched  will  always  be accepted, even if addr2 selects an earlier line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched.

       After the address (or address-range), and before the command, a !  may be inserted, which specifies that the command shall only be executed if the address (or address-range) does not match.

       The following address types are supported:

       number Match only the specified line number.

       first~step
              Match every step'th line starting with line first.  For example, ``sed -n 1~2p'' will print all the odd-numbered lines in the input stream,
              and  the  address  2~5  will match every fifth line, starting with the second.  first can be zero; in this case, sed operates as if it were
              equal to step.  (This is an extension.)

       $      Match the last line.

       /regexp/
              Match lines matching the regular expression regexp.

       \cregexpc
              Match lines matching the regular expression regexp.  The c may be any character.

       GNU sed also supports some special 2-address forms:

       0,addr2
              Start out in "matched first address" state, until addr2 is found.  This is similar to 1,addr2, except that if addr2 matches the very  first
              line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range.  This
              works only when addr2 is a regular expression.

       addr1,+N
              Will match addr1 and the N lines following addr1.


删除档内第 10 行到含 "man" 字串的资料行 , 则指令为 10,/man/d


/apple/,/orange/d  其表示删除资料区 , 由档内含有 "apple" 字串至含有 "orange" 字串的资料行 

 

ps. 

sed命令还可以用大括号进行分组,使其作用于同一个地址:
[address]{
    command1
    command2
    command3
}

[address]{command1;command2;command3}

 

awk和sed 格式类似

 

ref:

http://doc.linuxpk.com/351.html

http://blog.csdn.net/ShowMan/archive/2009/07/31/4396142.aspx 

http://hi.baidu.com/stroot/blog/item/1f4f420970f390aa2fddd40f.html 

http://www.blogjava.net/alwayscy/archive/2009/09/01/293409.html 

你可能感兴趣的:(sed)