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