手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

sed 高级用法

[日期:2014-09-20] 来源:Linux社区  作者:Linux [字体: ]

首先,应该明白模式空间的定义。模式空间就是读入行所在的缓存,sed对文本行进行的处理都是在这个缓存中进行的。这对接下来的学习是有帮助的。

在正常情况下,sed将待处理的行读入模式空间,脚本中的命令就一条接着一条的对该行进行处理,直到脚本执行完毕,然后该行被输出,模式空间请空;然后重复刚才的动作,文件中的新的一行被读入,直到文件处理完备。

但是,各种各样的原因,比如用户希望在某个条件下脚本中的某个命令被执行,或者希望模式空间得到保留以便下一次的处理,都有可能使得sed在处理文件的时候不按照正常的流程来进行。这个时候,sed设置了一些高级命令来满足用户的要求。

在sed命令中引入shell变量 http://www.linuxidc.com/Linux/2014-03/97896.htm

Linux下Shell编程——sed命令基本用法 http://www.linuxidc.com/Linux/2013-06/85526.htm

Unix文本处理工具之sed  http://www.linuxidc.com/Linux/2013-08/89315.htm

总的来说,这些命令可以划分为以下三类:
1. N、D、P:处理多行模式空间的问题;
2. H、h、G、g、x:将模式空间的内容放入存储空间以便接下来的编辑;
3. :、b、t:在脚本中实现分支与条件结构。
 多行模式空间的处理:
 由于正则表达式是面向行的,因此,如若某个词组一不分位于某行的结尾,另外一部分又在下一行的开始,这个时候用grep等命令来处理就相当的困难。然而,借助于sed的多行命令N、D、P,却可以轻易地完成这个任务。
 多行Next(N)命令是相对于next(n)命令的,后者将模式空间中的内容输出,然后把下一行读入模式空间,但是脚本并不会转移到开始而是从当前的n 命令之后开始执行;而前者则保存原来模式空间中的内容,再把新的一行读入,两者之间依靠一个换行符"\n"来分隔。在N命令执行后,控制流将继续用N命令以后的命令对模式空间进行处理。
 值得注意的是,在多行模式中,特殊字符"^"和"$"匹配的是模式空间的最开始与最末尾,而不是内嵌"\n"的开始与末尾。
 例1:
$ cat expl.1
 Consult Section 3.1 in the Owner and Operator
 Guide for a description of the tape drives
 available on your system.
现在要将"Owner and Operator Guide"替换为"Installation Guide":
$ sed '/Operator$/{
 > N
 > s/Owner and Operator\nGuide/Installation Guide\
 > /
 > }' expl.1
在上面的例子中要注意的是,行与行之间存在内嵌的换行符;另外在用于替代的内容中要插入换行符的话,要用如上的"\"的转义。
 再看一个例子:
 例2:
$ cat expl.2
 Consult Section 3.1 in the Owner and Operator
 Guide for a description of the tape drives
 available on your system.

 Look in the Owner and Operator Guide shipped with your system.

 Two manuals are provided including the Owner and
 Operator Guide and the User Guide.

 The Owner and Operator Guide is shipped with your system.
 $ sed 's/Owner and Operator Guide/Installation Guide/
 > /Owner/{
 > N
 > s/ *\n/ /
 > s/Owner and Operator Guide */Installation Guide\
 > /
 }' expl.2
结果得到:
Consult Section 3.1 in the Installation Guide
 for a description of the tape drives
 available on your system.

 Look in the Installation Guide shipped with your system.

 Two manuals are provided including the Installation Guide
 and the User Guide.

 The Installation Guide is shipped with your system.
看上去sed命令中作了两次替换是多余的。实际上,如果去掉第一次替换,再运行脚本,就会发现输出存在两个问题。一个是结果中最后一行不会被替换(在某些版本的sed中甚至不会被输出)。这是因为最后一行匹配了"Owner",执行N命令,但是已经到了文件末尾,某些版本就会直接打印这行再退出,而另外一些版本则是不作出打印立即退出。对于这个问题可以通过命令"$!N"来解决。这表示N命令对最后一行不起作用。另外一个问题是"look manuals"一段被拆为两行,而且与下一段的空行被删除了。这是因为内嵌的换行符被替换的结果。因此,sed中做两次替换一点也不是多余的。
 例3:
$ cat expl.3
 <para>

 This is a test paragraph in Interleaf style ASCII. Another line
 in a paragraph. Yet another.

 <Figure Begin>

 v.1111111111111111111111100000000000000000001111111111111000000
 100001000100100010001000001000000000000000000000000000000000000
 000000

 <Figure End>

 <para>

 More lines of text to be found after the figure.
 These lines should print.
我们的sed命令是这样的:
$ sed '/<para>{
 > N
 > c\
 > .LP
 > }
 > /<Figure Begin>/,/<Figure End>/{
 > w fig.interleaf
 > /<Figure End>/i\
 > .FG\
 > <insert figure here>\
 > .FE
 > d
 > }
 > /^$/d' expl.3
运行后得到的结果是:
.LP
 This is a test paragraph in Interleaf style ASCII. Another line
 in a paragraph. Yet another.
 .FG
 <insert figure here>
 .FE
 .LP
 More lines of text to e found after the figure.
 These lines should print.
而<Figure Begin>与<Figure End>之间的内容则写入文件"fig.interleaf"。值得注意的是命令"d"并不会影响命令i插入的内容。
 命令"d"作用是删除模式空间的内容,然后读入新的行,sed脚本从头再次开始执行。而命令"D"的不同之处在于它删除的是直到第一个内嵌换行符为止的模式空间的一部分,但是不会读入新的行,脚本将回到开始对剩下内容进行处理。
 例4:
$ cat expl.4
 This line is followed by 1 blank line.

 This line is followed by 2 blank line.


 This line is followed by 3 blank line.

 

 This line is followed by 4 blank line.

 This is the end.

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2014-09/106961p2.htm

linux
相关资讯       sed  sed 高级用法 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款