基础sed命令

基础sed命令

简介

sed是非交互式的编辑器。它不会修改文件,除非使用shell重定向来保存结果。默认情况下,所有的输出行都被打印到屏幕上。
sed编辑器逐行处理文件(或输入),并将结果发送到屏幕。具体过程如下:首先sed把当前正在处理的行保存在一个临时缓存区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。sed每处理完一行就将其从临时缓冲区删除,然后将下一行读入,进行处理和显示。处理完输入文件的最后一行后,sed便结束运行。sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。

sed命令

sed命令

命令功能
a在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行
c用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用""续行
i在当前行之前插入文本。多行时除最后一行外,每行末尾需用""续行
d删除行
y将字符替换为另一字符(不能对正则表达式使用y命令
p打印行
s用一个字符串替换另一个
g在行内进行全局替换
n读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理
N读取匹配到的行的下一行追加至模式空间
P打印模式空间开端至\n内容,并追加到默认输出之前
D删除多行模式空间中的所有行
w将所选的行写入文件
x交换保持区与模式空间的内容
h把模式空间里的内容复制到保持空间
H把模式空间里的内容追加到保持空间
g把保持里的内容复制到模式空间,覆盖原有的内容
G把保持的内容追加到模式空间里,追加在原有内容的后面

sed命令使用示例

单行模式空间

a:追加

[root@master ~]# cat test2
See Section 1.4
See Section 12.9

//向test2文件中追加See Section 7.7
[root@master ~]# sed  '/1.4/aSee Section 7.7' test2     
See Section 1.4
See Section 7.7
See Section 12.9

i:插入

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//在See Section 7.7前面插入See Section 8.8
[root@master ~]# sed  '/See Section 7.7/iSee Section 8.8' test2    
See Section 1.4
See Section 8.8
See Section 7.7
See Section 12.9

c:更改

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//将第二行更改为See Section 7.8
[root@master ~]# sed '2cSee Section 7.8' test2      
See Section 1.4
See Section 7.8
See Section 12.9

d:删除

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//删除See Section 7.7[root@master ~]# sed  '/See Section 7.7/d' test2    
See Section 1.4
See Section 12.9

y:替换

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9
[root@master ~]# sed 'y/7/8/' test2    //将test2中的7替换为8
See Section 1.4
See Section 8.8
See Section 12.9

p:打印

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//默认情况下,sed把所有输入行都打印在标准输出上。如果某行匹配模式See Section 7.7,p命令将把该行另外打印一遍。
[root@master ~]# sed '/See Section 7.7/p' test2 
See Section 1.4
See Section 7.7
See Section 7.7
See Section 12.9

//加上sed -n相当于,打印模式空间的文本
[root@master ~]# sed -n '/See Section 7.7/p' test2  
See Section 7.7

s:替换

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//将每行的第一个See参数替换为Sys
[root@master ~]# sed 's/See/Sys/' test2
Sys Section 1.4
Sys Section 7.7
Sys Section 12.9

g:在行内进行全局替换

[root@master ~]# cat test2
See Section 1.4
See Section 7.7
See Section 12.9

//将S全局替换为Z
[root@master ~]# sed 's/S/Z/g' test2
Zee Zection 1.4
Zee Zection 7.7
Zee Zection 12.9

n:下一步

[root@master ~]# cat test3
.H1 "on Egypt"

Napoleon, pointing to the Pyramids, said to his troops:
"Soldiers, forty centuries have their eyes upon you." 

//匹配任何以字符串‘.H1’开始的行,然后打印那一行并读入下一行。如果那一行为空,则删除它”
[root@master ~]# sed '/^.H1/{n;/^$/d}' test3
.H1 "on Egypt"
Napoleon, pointing to the Pyramids, said to his troops:
"Soldiers, forty centuries have their eyes upon you."
多行模式空间

N:读取匹配到的行的下一行追加至模式空间

[root@master ~]# cat test4
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system

//寻找行结尾处的“Operator”,读取下一个输入行,然后进行替换。
[root@master ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/Installlation Guide/}' test4
Consult Section 3.1 in the Installlation Guide for a description of the tape drives
available on your system

P:打印模式空间开端至\n内容,并追加到默认输出之前
D:删除多行模式空间中的所有行

[root@master ~]# cat test6
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.

//替换命令匹配“\nSystem”,并且用“Operating \nSystem”取代它
[root@master ~]# sed '/UNIX$/{N;/\nSystem/s// Operating &/;P;D}' test6 
Here are examples of the UNIX Operating 
System. Where UNIX Operating 
System appears,it should be the UNIX
Operating System.

h:把模式空间里的内容复制到保持空间
G:把保持的内容追加到模式空间里,追加在原有内容的后面

[root@master ~]# cat test7
1
2
11
22
111
222


//我们将第一行复制到保持空间(它一直在那),这时清除模式空间。然后sed将第二行读入模式空间,并且将保持空间的行追加到模式空间的结尾
[root@master ~]# sed '/1/{h;d};/2/{G}' test7
2
1
22
11
222
111
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值