sed应用

sed应用

sed的作用:

流编辑器,过滤和替换文本。

  • 首先sed读取文件中的一行内容,把其保存在一个临时缓存区中(也称为模式空间)
  • 然后根据需求处理临时缓冲区中的行,完成后把该行发送到屏幕上

sed工作原理:

sed 命令将当前处理的行读入模式空间进行处理,处理完把结果输出,并清空模式空 间。然后再将下一行读入模式空间进行处理输出,以此类推,直到最后一行。还有一个空间叫保持 空间,又称暂存空间,可以暂时存放一些处理的数据,但不能直接输出,只能放到模式空间输出。 这两个空间其实就是在内存中初始化的一个内存区域,存放正在处理的数据和临时存放的数据。

命令描述
n N读取/追加下一行输入到模式空间
h H复制/追加模式空间到保持空间
g G复制/追加保持空间到模式空间
x交换模式空间和保持空间内容
p打印当前模式空间
P打印模式空间的第一行
d删除模式空间,开始下一个循环
D删除模式空间的第一行,开始下一个循环
y单字符转换
c所选行替换新文本
a当前行追加文本
i当前行上面插入文本

追加(a)

[root@localhost ~]# cat test
holle world
how are you
[root@localhost ~]# sed '/world/alove' test
holle world
love
how are you

插入(i)

[root@localhost ~]# cat test
holle world
how are you
[root@localhost ~]# sed '2ijiang' test
holle world
jiang
how are you

更改(c)

[root@localhost ~]# cat test
holle world
how are you
[root@localhost ~]# sed '2cfine' test
holle world
fine
[root@localhost ~]# sed '/you/cfine' test
holle world
fine

转换(y)

[root@localhost ~]# cat test
holle world
how are youj
[root@localhost ~]# sed 'y/y/m/' test
holle world
how are mou

打印(p)

[root@localhost ~]# cat test
holle world
how are you
[root@localhost ~]# sed 'p' test
holle world
holle world
how are you
how are you
[root@localhost ~]# sed -n '2p' test
how are you

读取(n)

[root@localhost ~]# cat test
.H1 "On Egypt"

Napoleon,pointing to the Pyramids, said to his troops:
"Soldiers,forty centuries have their eyes upon you."
[root@localhost ~]# sed '/^.H1/{n;/^$/d} ' test
.H1 "On Egypt"
Napoleon,pointing to the Pyramids, said to his troops:
"Soldiers,forty centuries have their eyes upon you."

追加(N)

[root@localhost ~]# cat test
Consult Section 3. 1 in the Owner and Operator
Guide for a describption of the tape drives
available on your system.
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/Installa
Consult Section 3. 1 in the Installation Guide for a describption of the ta
available on your system.
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide /Installation Guide\n/g}' test
Consult Section 3. 1 in the Installation Guide
for a describption of the tape drives
available on your system.

删除(d)

删除的内容到剪切板中

[root@localhost ~]# cat test
Consult Section 3. 1 in the Owner and Operator
Guide for a describption of the tape drives
available on your system.
[root@localhost ~]# sed '/Operator$/{d;p}' test
Guide for a describption of the tape drives
available on your system.

[root@localhost ~]# cat test
line1

line2


line3



line4





end

[root@localhost ~]# sed '/^$/{N;/^\n$/d}' test
line1

line2
line3

line4

end

删除(D)

只删除模式空间的第一行

[root@localhost ~]# cat test
line1

line2


line3



line4





end
[root@localhost ~]# sed '/^$/{N;/^\n$/D}' test
line1

line2

line3

line4

end

多行打印(P)

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

保持空间操作(h 与 H、g 与 G)

h 复制模式空间内容到保持空间(覆盖)。

H 复制模式空间内容追加到保持空间。

g 复制保持空间内容到模式空间(覆盖)。

G 复制保持空间内容追加到模式空间。

x 模式空间与保持空间内容互换

[root@localhost ~]# cat test
1
2
11
22
111
222
[root@localhost ~]# sed '/1/{h;d};/2/{G}' test
2
1
22
11
222
111
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值