se

原创 2011年10月17日 10:49:06

SED单行脚本快速参考

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

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命令中的瑞士军刀

基本写法: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/'

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

     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/


JAVA SE 语言编程

-
  • 1970年01月01日 08:00

面试题集——Java之Java SE

最近忙着复习,之前好多东西都忘了,也有一些知道,但到真正要自己说的时候又不一定能流利说出口,所以,在牛客网看到了一位师兄提出的面试可能问题,就试着按自己知道的回答回答,不知道的那就赶紧查查 一、J2...
  • a327369238
  • a327369238
  • 2015-10-07 15:55:22
  • 1833

【Java SE】————环境变量的配置及其原因(Windows)

环境变量:                  先说说环境变量,一般我们在配置的时候电脑中有两个,一个是用户环境变量,另一个是系统环境变量。(如下图)从字面上我们就可以看出来,用户环境变量,单单指的...
  • z15732621736
  • z15732621736
  • 2016-01-31 11:55:07
  • 1979

JAVA SE 学习笔记

J2SE基础学习笔记,温故知新于2016/12/5.
  • conanswp
  • conanswp
  • 2016-02-24 22:07:24
  • 1942

Protel 99 se 注册码

  • 2010年08月03日 15:46
  • 173B
  • 下载

java SE教程05

打印图形: 外层循环控制行, 内层循环控制列1. 先根据行数编写外层循环次数 2. 再根据外层循环的变量(i) , 去分析, 这个变量与内层循环列数的关系 ! (1)* * * * * * *...
  • qq1350048638
  • qq1350048638
  • 2017-10-30 10:08:22
  • 275

JDK和JRE SE、EE和ME

jdk与jre的区别 很多程序员已经干了一段时间java了依然不明白jdk与jre的区别。 JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它...
  • wykgf
  • wykgf
  • 2012-12-21 16:11:51
  • 4830

SE壳破解教程

  • 2013年07月01日 03:53
  • 21.7MB
  • 下载

Java SE 知识点

Java基础 1.什么是Java? Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Jav...
  • Bucklly
  • Bucklly
  • 2012-07-07 23:03:57
  • 3751

Java SE 源码 Java SE 源码

  • 2010年08月02日 00:41
  • 17.27MB
  • 下载
收藏助手
不良信息举报
您举报文章:se
举报原因:
原因补充:

(最多只允许输入30个字)