1.删除首字母问空的行
2.删除第二到第八的所有行。
3.编写sed脚本,替换与行匹配相同的内容,即将boby替换为/boby,但仅替换第二个boby为/boby.
[
root@localhost tmp]# cat test1.txt
<html>
<title>First Web</title>
<boby>wqeqwe<boby>
</html>
[
root@localhost tmp]# cat sed.sh
/boby/{
s//\/body/2
}
[
root@localhost tmp]# sed -f sed.sh test1.txt
<html>
<title>First Web</title>
<boby>wqeqwe</body>
</html>
4.编写sed脚本,第一条s替换指令仅替换每行的第一个h1,h2...,第二条s替换指令用来替换第二个h1,h2...
[
root@localhost tmp]# vi sed.sh
[
root@localhost tmp]# cat sed.sh
/h[0-9]/{
s//\<&\>/1
s//\<\/&\>/2
}
[
root@localhost tmp]# vi sed.sh
[
root@localhost tmp]# sed -f sed.sh test1.txt
<html>
<title>First Web</title>
<body>
<h1>wqeqwe</h1>
<h2>wqeqwe</h2>
<h3>qweqwe</h3>
</body>
</html>
5.删除文件中的空白行
[
root@localhost tmp]# cat test.txt
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
[
root@localhost tmp]# cat sed.sh
/.*/{
/^$/d
}
[
root@localhost tmp]# sed -f sed.sh test.txt
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=255.255.255.0
GATEWAY=192.168.0.254
6.添加一行内容
sed '/static/a IPADDR=192.168.0.1' test.txt
7.在匹配NETMASK的行前面插入内容IPADDR=192.168.0.1
sed '/static/i IPADDR=192.168.0.1' test.txt
8.将包含ONBOOT行的内容更改为ONBOOT=not.
9.显示第一,第二行的内容。