sed高级应用

sed命令语法

行地址对于任何命令都是可选的。它可以使一个模式,被描述为由斜杠、行号或行寻址符号括住的正则表达式。大多数sed命令能接受由逗号分隔的两个地址,这两个地址,这两个地址用来标识行的范围。这些命令的语法格式为:

[address] command

有一些命令只接受单个行地址:

[line-address] command

可以用大括号进行分组以使其作用于同一个地址:

address {
command1
command2
command3
}

替换

详细的用法:

[address]s/pattern/replacement/flags

这里修饰替换的标志flags 是:

n      1到512之间的一个数字,表示对文本模式中指定模式第n次出现的情况进行替换

g       对模式空间的所有出现的情况进行全局更改。而没有g是通常只有第一次出现的情况被取代。

p       打印模式空间的内容。

w       file  

          将模式空间的内容写到文件file 中
 

和地址不同的是,地址需要一个作为定界符的斜杠(/) ,而正则表达式可以用任意字符来分隔,只有换行符除外。因此,如果模式包含斜杠,那么可以选择另一-个字符作为定界符,例如感叹号。
s !/usr/mail!/usr2/mail !
注意,定界符出现了3次而且在replacement之后是必需的。不管使用哪种定界符,如果它出现在正则表达式中,或者在替换文本中,那么就用反斜杠来转义它

[root@localhost ~]# echo '/usr/local/src' | sed 's/\/usr\/local\/src/\/user\/local\/src/'
/user/local/src
[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src#'
/user/local/src
[root@localhost ~]# echo '/usr/local/src' | sed 's!/usr/local/src!/user/local/src!'
/user/local/src

Replacement是一个字符串,用来替换与正则表达式匹配的内容。在replacement部分,只用下列字符有特殊含义:
&         用正则表达式匹配的内容进行替换。
\n        匹配第n个字串(n是一个数字),这个字串以前在pattern 中用““和““和““指定。
\          当在替换部分包含“与“符号(&),反斜杠(\)和替换命令的定界符时可用\转义它们。另                 外,它用于转义换行符并创建多行replacement字符串

数字表示很少使用,在这种情况下,正则表达式在一行上重复匹配,而只需要对其中某个位置的匹配进行替换。例如,某输入行也许包含tb1输入,也许包含多个制表位。假设每行有3个制表符,并且要用“>“替换第二个制表位,则可以使用下面的替换命令完成该功能:
s /·/>/2
“·”表示一个真正的制表符,而制表符在屏幕上是不可见的。如果输入是一行的文件,如下所示:
Column1·Col umn2·Column3·Col umn4
 

[root@localhost ~]# cat abc
Column1	Column2	Column3	Column4
[root@localhost ~]# sed 's/\t/>/2' abc
Column1	Column2>Column3	Column4     //替换后的结果显示

替换元字符

替换元字符是反斜杠(\)、“与”符号(&)和\n。反斜杠一般用于转义其他的元字符,但是他在替换字符串中也用于包含换行符。
我们可以对前面的示例做一些改动,用换行符取代每行上的第二个制表符。

s/*/\
/2

案例

1.
[root@localhost ~]# cat abc
column1 column2 column3 column4
[root@localhost ~]# sed 's/\t/\n/2' abc
Column1	Column2
Column3	Column4
产生的结果


2.
[root@localhost ~]# echo '.Ah "Major Heading"' > 1
[root@localhost ~]# cat 1
.Ah "Major Heading"
[root@localhost ~]# sed '/^\.Ah/{
> s/\.Ah */\
> \
> @A HEAD = /
> s/"//g
> s/$/\
> /
> }' 1
@A HEAD = Major Heading
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */\n@A HEAD = /;s/"//g};s/$/\n/' 1
@A HEAD = Major Heading



3.
[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
[root@localhost ~]# sed -r "s/ORA (.*)/O'Reilly &/g" 1
.Ah "Major Heading"
O'Reilly ORA Associates, Inc.
 
[root@localhost ~]# sed 's/ORA.*/& 123/g' 1
.Ah "Major Heading"
ORA Associates, Inc. 123


4.
[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
[root@localhost ~]# sed 's/UNIX/\\s-2&\\s0/g' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.

删除

重要的是:如果某行匹配这个地址,那么就删除整个行,而不只是删除行中匹配的部分(要删除行的一部分,可以使用替换命令并指定一个空的替换)。上一章展示了删除空行的命令:

/^$/d

删除空行

[root@localhost ~]# cat 123
aaa

aaa

aaa
[root@localhost ~]# sed '/^$/d' 123 
aaa
aaa
aaa

删除指定开头

[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/d' 1   //删除.Ah开头的
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

追加

追加(a)、插入(i)和更改(c)命令提供了通常在交互式编辑器(例如vi)中所选的编辑功能。你会奇怪地发现、可以使用这些相同的命令在非交互编辑器中“输入”文本。这些命令的语法在sed中不常用,因为它们必须在多行上来指定。语法如下:

追加[line-address]a\
text
插入[| ine-address]i\
text
更改[address]c\
text

插入命令将所提供的文本放置在模式空间的当前行之前。追加命令将文本放置在当前行之后。更改命令用所提供的文本取代模式空间的内容。


这些命令中的每一个都要求后面跟一个反斜杠用于转义第一个行尾。text必须从下一行开始。要输入多行文本,每个连续的行都必须用反斜杠结束,最后一行例外。例如,下面的插入命令在匹配“<Larry' s Address>”行的地方插入两行文本:
/<Larry' s Address>/i\

4700 Cross Court\
French Lick,IN
而且,如果文本包含一个字面的反斜杠,要再添加一个反斜杠来转义它
 

#追加a
[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1 a abc' 1    //在第一行后面加abc
.Ah "Major Heading"
abc
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1 a \    abc' 1
.Ah "Major Heading"
    abc
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/a \  hehe' 1
.Ah "Major Heading"
  hehe
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^See/a 123' 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
123
See Section 12.9
123
first:second
one:two
 
 
#插入i
[root@localhost ~]# sed '/^\.Ah/iabc' 1
abc
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
 
 
#更改c
[root@localhost ~]# sed '1 c hehe' 1   //改变第一行
hehe
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/ORA/c \  hh' 1
.Ah "Major Heading"
  hh
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
 
[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/,/on /cabc' 1   //从.Ah到on这一行更改为abc
abc
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/,$cabc' 1   //全部都改了
abc

转换

转换命令是特有的,不仅因为它在所有的sed命令中拥有最小的肋记符。这个命令按位置将字符串abc中的每个字符,都转换成字符串xyz中的等价字符它的语法如下:

[address]y/ abc/xyz/

替换根据字符的位置来进行。因此,它没有“词”的概念。这样,在该行.上的任何地方的“a”都被换成了“x”,而不管它后面是否跟有“b”。这个命令的一个可能的用处是用大写字母替换对应的小写字母

y/ abcdefghi jk Imnopqr stuvwxyz/ ABCDEFGHI JKLMNOPQRSTUVWXYZ/

这个命令影响整个模式空间的所有内容。如果想在输入行上转换单个单词,那么通过使用保持空间可以完成。参见第六章有关如何使用保持空间的详介绍(大致过程是:输出更改单的那一行之前的所有行,删除这些行,将单词后面的行复制到保持空间,转换这个单词,然后将保持空间的内容追加到模式空间)

[root@localhost ~]# cat 1
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1y/ajo/123/' 1
.Ah "M123r He1ding"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

打印

打印命令§ 输出模式空间的内容。它既不清除模式空间也不改变脚本中的控制流。然而,它频繁地用在改变流控制的命令(d,N,b) 之前。除非抑制(-n)默认的输出,否则打印命令将输出行的重复复制。当抑制默认的输出或者当通过程序的流控制来避免到达脚本的底部时,可能会使用它

#实例
[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# sed '/^\.Ah/{p;s/"//g;s/^\.Ah //}' 2
.Ah "Comment"
Comment
.Ah "Substitution"
Substitution
.Ah "Delete"
Delete
.Ah "Append, Insert and Change"
Append, Insert and Change
.Ah "List"
List

打印行号

跟在地址后面的等号(=)打印被匹配的行的行号。除非抑制行的自动输出,行号和行本身将被打印。它的语法如下

[line-address]=

这个命令不能对一个范围内的行进行操作。
程序员也许用该命令打印源文件中的某些行。例如,下面的脚本打印行号和行
本身,这些行都包含后面跟有字符串“if” 的制表符。脚本如下

#n打印具有if 语句的行号和行
/if/{
=
p
}

[root@localhost ~]# cat 2
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
.Ah "Append, Insert and Change"
.Ah "List"
[root@localhost ~]# sed -n '/Comment/{=;p}' 2     //打印出Comment行号
1
.Ah "Comment"

高级sed命令

高级命令分成了3个组:

1.处理了多行模式空间(N、D、P)。

2.采用保持空间来保存模式空间的内容并使它可用于后续的命令(H、h、G、g、x)。

3.编写使用分支和条件指令的脚本来更改控制流( :、b、t) 。

追加下一行

多行Next(N)命令通过读取新的输入行,并将它添加到模式空间的现有内容之后来创建多行模式空间

#原文档
[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.


#改变
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' abc
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.

#拆解
[root@localhost ~]# sed -n '/Operator$/{N;p}' abc  //匹配以Operator结尾的行 N和匹配行的下一行 p打印
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives


[root@localhost ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g ;p}' abc   //将Owner and Operator\nGuide 变成 installation Guide   因为替换包括了\n(转行)  所以打印时会将两条显示为一条
Consult Section 3.1 in the installation Guide for a description of the tape drives  


#原文档
[root@localhost ~]# cat abc
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 inc luding the Owner and
Operator Guide and the User Gui de.
The Owner and Operator Guide is shipped with your system.

#改变
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g}' abc
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 inc luding the installation Guide
and the User Gui de.
The installation Guide is shipped with your system.




#拆解
[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g' abc   //将Owner and Operator Guide替换为installation Guide
Consult Section 3.1 in the Owner and Operator
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 inc luding the Owner and
Operator Guide and the User Gui de.
The installation Guide is shipped with your system.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/p' abc   //匹配带有Owner的行(此时只有两行)
Consult Section 3.1 in the Owner and Operator
Two manuals are provided inc luding the Owner and


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;p}' abc  //将匹配行的下一行追加到模式空间
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;p}' abc  //将 *\n替换为空格 就变成了两行合并为一行
Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and Operator Guide and the User Gui de.

[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g;p}' abc  //将Owner and Operator Guide * 替换为installation Guide\n 改变了Owner and Operator Guide然后后面的换行
Consult Section 3.1 in the installation Guide
for a description of the tape drives
Two manuals are provided inc luding the installation Guide
and the User Gui de.

多行删除

删除命令(d)删除模式空间的内容并导致读入新的输入行,从而在脚本的顶端重新使用编辑方法。删除命令(D)稍微有些不同:它删除模式空间中直到第一个嵌入的换行符的这部分内容。它不会导致读入新的输入行,相反,它返回到脚本的顶端,将这些指令应用于模式空间剩余的内容。

原文档
[root@localhost ~]# cat abc
This line is followed by 1 blank line.

This line is followed by 2 blank lines.


This line is followed by 3 blank lines.



This line is followed by 4 b lank lines.




This is the end.


#改变
[root@localhost ~]# sed  '/^$/{N;/^\n$/d}' abc
This line is followed by 1 blank line.

This line is followed by 2 blank lines.
This line is followed by 3 blank lines.

This line is followed by 4 b lank lines.
This is the end.


#拆解
[root@localhost ~]# sed -n '/^$/{N;p}' abc  //匹配空行将匹配行的下一行追加到模式空间

This line is followed by 2 blank lines.





This line is followed by 4 b lank lines.




[root@localhost ~]# sed -n '/^$/{N;/^\n$/d;p}' abc  //以换行符开头或结尾的删除(空格偶数删除)

This line is followed by 2 blank lines.

This line is followed by 4 b lank lines.



#源文件
[root@localhost ~]# cat abc
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.


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


#拆解
[root@localhost ~]# sed -n '/UNIX$/{N;p}' abc  //匹配UNIX结尾的行 将匹配到行的下一行添加到模式空间
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.


[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{p}}' abc  //  /\nSystem/  在匹配转行后为System的行
Here are examples of the UNIX
System. Where UNIX

[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' abc  //添加Operating
Here are examples of the UNIX Operating 
System. Where UNIX


[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P}}' abc  //P打印模式空间第一行
Here are examples of the UNIX Operating 

[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc  //删除模式空间的第一行
Here are examples of the UNIX Operating 
System. Where UNIX Operating 


包含哪一行

模式空间是容纳当前输入行的缓冲区。还有一个称为保持空间(hold space)的顶留(set-aside)缓冲区。模式空间的内容可以复制到保持空间,而且保持空间的内容也可以复制到模式空间

 

源文件
[root@localhost ~]# cat abc
1
2
11
22
111
222

#变化
[root@localhost ~]# sed -n '/1/{h;d};/2/{G};p' abc
2
1
22
11
222
111

[root@localhost ~]# sed -n '/1/{h;d};p' abc   //将模式空间的1追加到保持空间 并删除模式空间的1
2
22
222



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值