sed命令高级用法

sed多行命令

sed 命令的基础功能时, sed 命令都只是针对单行数据执行操作,但是,有时我们需要对多行的数据执行特定操作。如在文本中查找一串字符串"xxxxxxxxxxxx",它很有可能出现在两行中,每行各包含其中一部分。这时,如果用普通的 sed 编辑器命令来处理文本,就不可能发现这种被分开的情况。

sed能查看模式空间的多行(就是允许匹配模式扩展到多行上)。

sed 包含了三个可用来处理多行文本的特殊命令,分别是:

  1. Next 命令(N):将数据流中的下一行加进来创建一个多行组来处理。
  2. Delete(D):删除多行组中的一行。
  3. Print(P):打印多行组中的一行。

N多行命令

下面这个例子演示N命令的功能

[root@localhost ~]# cat test.txt 
Make sure that the heap size is 
set to about half the memory available
on the system and that the owner
of the process is allowed to use this
limit

[root@localhost ~]# sed '/lable/{N;s/\n/ /}' test.txt 
Make sure that the heap size is 
set to about half the memory available on the system and that the owner
of the process is allowed to use this
limit

 上面这个例子中,用sed匹配到lable这行的文本在使用N将匹配到文本和下一行加入到模式空间,然后使用替换命令将换行符替换为空格,结果就是在文件中的两行合并成了一行

 

D 多行删除命令

[root@localhost ~]# cat test.txt 
Make sure that the heap size is 
set to about half the memory available
on the system and that the owner
of the process is allowed to use this
limit.

[root@localhost ~]# sed '/lable/{N;D}' test.txt
Make sure that the heap size is 
on the system and that the owner
of the process is allowed to use this
limit.

 

 文本的第二行被 N 命令加到模式空间,因此 sed 命令第一次匹配就是成功,而 D 命令会将模式空间中,第一个换行符之前(也就是第一行)的数据删除,所以,得到了如上所示的结果

 

多行删除模式

[root@localhost ~]# cat test2.txt 
This is line followed by 1 blank line


This is line followed by 1 blank line


This is line followed by 1 blank line




This is line followed by 1 blank line




This is the end

[root@localhost ~]# sed '/^$/{N;/^\n$/D}' test2.txt 
This is line followed by 1 blank line

This is line followed by 1 blank line

This is line followed by 1 blank line

This is line followed by 1 blank line

This is the end

 匹配空行,文本空行下面的一行回被加入到模式空间,删除空行,第一个空行换行符之前(也就是第一行)的数据删除,所以,得到了如上所示的结果

 

 多行转换

[root@localhost ~]# cat test3.txt 
Here are examples of the UNIX
System. Where UNlX
System appears,it should be the UNIX
Operating System.

[root@localhost ~]# sed '/UNIX$/ {N;/\nSystem/{ s// Operating &/;P;D}}' test3.txt 
Here are examples of the UNIX Operating 
System. Where UNIX Operating 
System appears,it should be the UNIX
Operating System.

 sed匹配UNIX$结尾的行,用N将匹配到的行和下一行加入到模式空间,然后再匹配\nSystem,在里面新增内容System的上一行结尾新增内容,删除模式空间中Here are examples of the UNIX Operating,在次执行前面的内容,在下一个匹配到的\nSystem的上一行结尾增加内容,删除模式空间第二次匹配到的System. Where UNIX Operating,最结果就是上面。

 

模式空间是容纳当前输入行的缓冲区。还有一个称为保持空间(holdspace)的顶留(set-aside) 缓冲区。模式空间的内容可以复制到保持空间,而且保持空间的内容也可以复制到模式空间。有一组命令用于在保持空间和模式空间之间移动数据。保持空间用于临时存储。单独的命令不能寻址保持空间或者更改它的内容。

保持空间最常的用途是,当改变模式空间中的原始内容时,用于保留当前输入行的副本。影响模式空间的命令有:

命令缩写功能
Hold h或H 将模式空间的内容复制或追加到保持空间
Get g或G 将保持空间的内容复制或追加到模式空间
Exchange x 交换保持空间或模式空间的内容

 

反转行

[root@localhost ~]# cat test4.txt 
1
2
11
22
111
222

[root@localhost ~]# sed '/1/{h;d};G' test4.txt 
2
1
22
11
222
111

 

 匹配1,11,111到模式空间然后追加到保存空间,清空模式空间,在将保存空间内容逐行追加到模式空间

 

构建文本块

[root@localhost ~]# cat test.txt 
Make sure that the heap size is 

set to about half the memory available
on the system and that the owner

of the process is allowed to use this

limit.

[root@localhost ~]# sed '/^$/!{H;d};/^$/{x;s/^\n/<p>/;s/$/<\/p>/;G}' test.txt 
<p>Make sure that the heap size is </p>

<p>set to about half the memory available
on the system and that the owner</p>

<p>of the process is allowed to use this</p>

<p>limit.</p>

 匹配非空行的内容然后加入到保存空间,删除模式空间内容,替换模式空间与保存空间内容,然后在匹配以换行符开头的内容替换为<P>,匹配$加上<P>,在将保存空间内容追加到模式空间,出现了上面的结果。

posted @ 2021-03-29 20:26  第七爻  阅读(707)  评论(0编辑  收藏  举报