sed

1SED单行脚本快速参考

2,sed工作原理

Sed维护两个数据缓冲区:活动的pattern space 和额外的 hold space。两个初始化的时候都是空的。

Sed按照如下的循环在每一行输入上进行操作:

     首先,sed从输入流上读取一行,移除任何末尾的换行符,接着,把它放到pattern space上。然后执行命令,每一个命令有一个地址和他相关联:地址是一种条件代码,在一个命令被执行前会先验证条件。

当到达脚本的结尾时,除非用了-n选项,pattern space的内容会被打印到输出流,并且加上曾经去掉的末尾换行符。接着下一个循环重新的一行输入开始。

除非使用特殊的命令(像‘D’),在每次循环之间,pattern space都会被删除。另一方面,hold space会保存它在两次循环之间的数据(像命令’hHxgG’都会在两个缓冲区之间移动数据)。

quick note 对pattern space执行以下基本格式的操作:

  address地址 command命令

1,address 行号,正则表达式,范围

2,command  q、d、p、n、s/regexp/replacement/flags、

 How sed Works

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.

sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed.3 Then the next cycle starts for the next input line.

Unless special commands (like ‘D’) are used, the pattern space is deleted between two cycles. The hold space, on the other hand, keeps its data between cycles (see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers).

3,SED多命令执行顺序Multiple commands and order of execution

他们的顺序很简单。每读取一行,多个命令,都有机会按照使用者的排放顺序,去对输入行进行操作。

As we explore more of the commands of sed, the commands will become complex, and the actual sequence can be confusing. It's really quite simple. Each line is read in. Each command, in order specified by the user, has a chance to operate on the input line. After the substitutions are made, the next command has a chance to operate on the same line, which may have been modified by earlier commands. If you ever have a question, the best way to learn what will happen is to create a small example. If a complex command doesn't work, make it simpler. If you are having problems getting a complex script working, break it up into two smaller scripts and pipe the two scripts together.

4,在sed的命令行中引用shell变量时要使用双引号,而不是通常所用的单引号

name='zone\ "localhost"'

sed "/$name/,/};/d" named.conf 

5,速记

1)$                       代表最后一行的地址Match the last line.

2)非!运算         用在地址和命令之间,表示只有当这个地址不匹配的时候才执行这个命令 

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) doesnotmatch.

3)pattern space 和 hold space两个在初始化的时候都是空的

4)hold space     当用于命令hHxgG时候,可以用来保存它在两次循环之间的数据;一般情况,它都是空的。

There is one more "location" to be covered: the hold buffer or hold space. Think of it as a spare pattern buffer. It can be used to "copy" or "remember" the data in the pattern space for later. There are five commands that use the hold buffer.

6,"s" Command: sed命令中的瑞士军刀

基本写法:s/regexp/replacement/flags

replacement部分可以包含\n(n从0到9开始)来引用通过\(和\)匹配到的内容。&可以用来引用所有匹配到的内容。最后可以包含一些特殊的字符进行大小写转换。

The replacement can contain \n (n being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the nth \( and its matching \). Also, the replacementcan contain unescaped & characters which reference the whole matched portion of the pattern space. Finally, as a GNU sed extension, you can include a special sequence made of a backslash and one of the letters L, l, U, u, or E. The meaning is as follows:
\L   Turn the replacement to lowercase until a \U or \E is found,
\l   Turn the next character to lowercase,
\U   Turn the replacement to uppercase until a \L or \E is found,
\u   Turn the next character to uppercase,
\E   Stop case conversion started by \L or \U.

示例:

转换字符串DeepDiscovery_300_lx_sc_hfb9002中除了_lx_sc_之外的字符为大些字符:

echo DeepDiscovery_300_lx_sc_hfb9002|sed 's/\(^.*\)\(_lx_.._\)\(.*$\)/\U\1\E\2\U\3/'

7, Sed Branching Operation

Sed Unconditional Branch Syntax:

$ sed ':labelcommand(s) b label'

:label - specification of label.

commands - Any sed command(s)

label - Any Name for the label

b label – jumps to the label without checking anyconditions. If label is not specified, then jumps to the end of the script.

Sed Conditional Branch Syntax:

$ sed ':labelcommand(s) t label'

:label - specification of label.

commands - Any sed command(s)

label - Any Name for the label

t label – jumps to the label only if the lastsubstitute command modified the pattern space. If label is not specified, thenjumps to the end of the script.

8,Example

     1),  http://www.gnu.org/software/sed/manual/sed.html#fn-3

    2),  http://sed.sourceforge.net/sedfaq4.html

    3), http://www.grymoire.com/Unix/Sed.html

    4) ,http://www.thegeekstuff.com/2009/12/unix-sed-tutorial-6-examples-for-sed-branching-operation/


posted on 2011-10-17 10:49  Tonystz  阅读(205)  评论(0编辑  收藏  举报