[root@VM-12-14-centos ~]# cat 3.txt
This
is
Test
file
!!!!
# 第二行后追加 Do you know?anything is possible
[root@VM-12-14-centos ~]# sed '2a Do you know?anything is possible' 3.txt
This
is
Do you know?anything is possible
Test
file
!!!!
# 将文本文件中的所有!替换成HappyEveryDay
[root@VM-12-14-centos ~]# sed 's/!/HappyEveryDay/g' 3.txt
This
is
Test
file
HappyEveryDayHappyEveryDayHappyEveryDayHappyEveryDay
# 删除第三行和第四行
[root@VM-12-14-centos ~]# sed '3,4d' 3.txt
This
is
!!!!
# 匹配以Test开始的行,并且删除
[root@VM-12-14-centos ~]# sed '/^Test/d' 3.txt
This
is
file
!!!!
# 打印文件的奇数行
[root@VM-12-14-centos ~]# cat 3.txt
This
is
Test
file
!!!!
[root@VM-12-14-centos ~]# sed -n '1~2p' 3.txt
This
Test
!!!!
# 将文件中每行的第二个<body>替换为</body>
[root@VM-12-14-centos ~]# cat index.html
<html>
<title>Web Page</title>
<body>Hello EveryBody!<body>
<body>Hello EveryBody!!<body>
<body>Hello EveryBody!!!<body>
<body>Hello EveryBody!!!!<body>
</html>
[root@VM-12-14-centos ~]# cat sed.sh
/body/{
s//\/body/2
}
[root@VM-12-14-centos ~]# sed -f sed.sh index.html
<html>
<title>Web Page</title>
<body>Hello EveryBody!</body>
<body>Hello EveryBody!!</body>
<body>Hello EveryBody!!!</body>
<body>Hello EveryBody!!!!</body>
</html>
# 将所有第一个出现的h1..h5,第二个出现的h1..h5添加<>,</>
[root@VM-12-14-centos ~]# cat index001.html
<html>
<title>Test Html file</title>
<body>
h1Helloh1
h2Thish2
h3ish3
h4Testh4
h5fileh5
</body>
</html>
[root@VM-12-14-centos ~]# cat sed001.sh
/h[0-9]/{
s//\<&\>/1
s//\<\/&\>/2
}
[root@VM-12-14-centos ~]# sed -f sed001.sh index001.html
<html>
<title>Test Html file</title>
<body>
<h1>Hello</h1>
<h2>This</h2>
<h3>is</h3>
<h4>Test</h4>
<h5>file</h5>
</body>
</html>