追加’a’
’ a ’ 追加
[root@localhost ~]# sed '/System/ahello' zbc
on the UNIX Operating System.
hello
插入’i’
’ i ’ 插入
[root@localhost ~]# sed '2iqwe' zbc
on the UNIX Operating System.
qwe
zxc
asd
更改’c’
’ c ’ 更改
[root@localhost ~]# sed '2chello' zbc
on the UNIX Operating System.
hello
asd
[root@localhost ~]# sed '/asd/chello' zbc
on the UNIX Operating System.
zxc
hello
[root@localhost ~]# sed '/asd/c\ hello' zbc
on the UNIX Operating System.
zxc
hello
[root@localhost ~]#
转换’y’
’ y ’ 转换
[root@localhost ~]# cat zbc
on the UNIX Operating System.
zxc
asd
[root@localhost ~]#
[root@localhost ~]# sed 'y/a/e/' zbc
on the UNIX Opereting System.
zxc
esd
打印’p’
’ p ’ 打印输出模式空间的内容
[root@localhost ~]# sed -n '2p' zbc
zxc
[root@localhost ~]#
打印行号 ’ = ’
[root@localhost ~]# sed '=' zbc
1
on the UNIX Operating System.
2
zxc
3
asd
[root@localhost ~]#
下一步’n’
’ n '下一步(next) 命令输出模式空间内容,然后读取输入的下一行
[root@localhost ~]# sed '2n;p' zbc
on the UNIX Operating System.
on the UNIX Operating System.
zxc
asd
asd
[root@localhost ~]# sed '2n' zbc
on the UNIX Operating System.
zxc
asd
[root@localhost ~]#
[root@localhost ~]# cat zbc
.H1 "On Egypt"
Napo|eon,pointing to the Pyramids, said to his troops:
"Soldiers,forty centur ies have their eyes upon you. "
[root@localhost ~]# sed '/^\.H1/{n;/^$/d}' zbc
.H1 "On Egypt"
Napo|eon,pointing to the Pyramids, said to his troops:
"Soldiers,forty centur ies have their eyes upon you. "
[root@localhost ~]#
追加下一行’N’
’ N ’ 追加下一行,多行Next命令通过读取新的输入行,并将它添加到模式空间的现有内容之后来创建多模式空间
[root@localhost ~]# cat zbc
Consult Section 3.1 in the Owner and Operator
Guide for a descr iption of the tape drives
available on your system.
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide /Installation Guide\n/g}' zbc
Consult Section 3.1 in the Installation Guide
for a descr iption of the tape drives
available on your system.
[root@localhost ~]#
多行删除’d’
’ d ’ 多行删除,删除命令(d)删除模式空间的内容并导致读入新的输入行,从而在脚本的顶端重新使用编辑方法
[root@localhost ~]# cat zbc
Consult Section 3.1 in the Owner and Operator
Guide for a descr iption of the tape drives
available on your system.
[root@localhost ~]# sed '/Operator$/{d;p}' zbc
Guide for a descr iption of the tape drives
available on your system.
[root@localhost ~]#
[root@localhost ~]# cat zbc
line 1
line 2
line 3
line4
end
[root@localhost ~]# sed '/^$/{N;/^\n$/d}' zbc
line 1
line 2
line 3
line4
end
[root@localhost ~]#
删除’D’
’ D ’ 删除命令稍微有些不同:它删除模式空间种直到第一个嵌入的换行符的这部分内容。它不会导致读入新的输入行,相反,它返回到脚本的顶端,将这些指令应用于模式空间剩余的内容。
line 1
line 2
line 3
line4
end
[root@localhost ~]#
多行打印’P’
’ P ’ 多行打印(Print)命令与小字母的print命令稍有不同。该命令输出多行模式空间的第一部分,直到第一个嵌入的换行符为止。在执行完脚本的最后一个命令之后,模式空间的内容自动输出(-n 选项或#n抑制这个默认的动作)。因此,当默认的输出被抑制或者脚本中的控制流更改,以至不能到达脚本的底部时,需要使用打印命令(P或p).
[root@localhost ~]# cat zbc
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
0perating System.
[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/s// 0perating &/}' zbc
Here are examples of the UNIX 0perating
System. Where UNIX
System appears, it should be the UNIX
0perating System.
[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/s// 0perating &/;P;D}' zbc
Here are examples of the UNIX 0perating
System. Where UNIX 0perating
System appears, it should be the UNIX
0perating System.
[root@localhost ~]#
包含那一行
模式空间是容纳当前输入行的缓冲区。还有一-个称为保持空间(hold space)的顶留(set-aside)缓冲区。模式空间的内容可以复制到保持空间,而且保持空间的内容也可以复制到模式空间。有一组命令用于在保持空间和模式空间之间移动数据。保持空间用于临时存储。单独的命令不能寻址保持空间或者更改它的内容。
保持空间最常的用途是,当改变模式空间中的原始内容时,用于保留当前输入行的副本。影响模式空间的命令有:
|命令|缩写|功能|
|-------|-----------|
| Hold | h或H | 将模式空间的内容复制或追加到保持空间 |
| Get | g或G | 将保持空间的内容复制或追加到模式空间 |
| Exchange | x | 交换保持空间和模式空间的内容 |
这些命令中的每一条都可以利用一个地址来指定一行或行范围。Hole(h, H)命令将数据移至保持空间、而get (g.G)命令将保持空间的数据移回到模式空间。同一命令的小写字母和大写字母之间的差别是,小字字母命令改写目的缓存区的内容,而大写字母命令追加缓存区的现有内容。
Hold命令用模式空间的内容取代保持空间的内容。get命令用保持空间的内容取代模式空间的内容。
Hole命令在保持空间的内容之后放置一个换行符,且后面跟随模式空间的内容(即使保持空间是空的,换行符也被追加到保持空间中)。Get命令模式空间的内容之后放置一个换行符,且后面跟随保持空间的内容。
交换命令交换两个缓存区的内容,对两个缓存区没有副作用。
[root@localhost ~]# cat zbc
1
2
11
22
111
222
//删除
[root@localhost ~]# sed '/1/{h;d}' zbc
2
22
222
//追加回来
[root@localhost ~]# sed '/1/{h;d};/2/{G}' zbc
2
1
22
11
222
111
[root@localhost ~]#
//不删除的样子
[root@localhost ~]# sed '/1/{h};/2/{G}' zbc
1
2
1
11
22
11
111
222
111
大写转换’y’
’ y ’ 大写转换
[root@localhost ~]# cat zbc
find the Match statement
Consult the Get statement.
Using the Read statement to retrieve data
[root@localhost ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/g}' zbc
Match
Get
Read
[root@localhost ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/g;y/abcdefghijklmnopqrstuvwsyz/ABCDEFGHIJKLMNOPQRSTUVWSYZ/;G}' zbc
MATCH
find the Match statement
GET
Consult the Get statement.
READ
Using the Read statement to retrieve data
[root@localhost ~]#
[root@localhost ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/g;y/abcdefghijklmnopqrstuvwsyz/ABCDEFGHIJKLMNOPQRSTUVWSYZ/;G;s/(.*)\n(.*the ).*( statement.*)/\2\1\3/}' zbc
find the MATCH statement
Consult the GET statement.
Using the READ statement to retrieve data
[root@localhost ~]#