# se

### sed工作原理

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

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

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

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

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

### 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.

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

name='zone\ "localhost"'

sed "/$name/,/};/d" named.conf ### 速记符号 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.

### 强大的"s" Command: sed命令中的瑞士军刀

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.

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

### 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.

### Sed命令嵌套

Following an address or address range, sed accepts curly braces '{...}' so several commands may be applied to that line or to the lines matched by the address range. On the command line, semicolons ';' separate each instruction and must precede the closing brace.

sed '/Owner:/{s/yours/mine/g;s/your/my/g;s/you/me/g;}' file

### More Example

• 本文已收录于以下专栏：

举报原因： 您举报文章：se 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)