[root@localhost ~] # sed [选项] '[动作]' 文件名
选项:
[root@localhost ~]# sed '2p' student.txt
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]# sed -n '2p' student.txt
1 Liming 82 95 86 87.66
[root@localhost ~]#sed '2,4d' student.txt
#删除从第二行到第四行的数据
ID Name PHP Linux MySQL Average
[root@localhost ~]# cat student.txt
#文件本身并没有被修改
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]# sed '2a hello' student.txt
#在第二行后加入hello
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
hello
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]# sed '2i hello > world' student.txt
#在第二行前插入两行数据
ID Name PHP Linux MySQL Average
hello
world
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]# sed -n'2i hello \
#只查看sed命令操作的麵
world' student.txt
hello
world
[root@localhost ~]# cat student.txt | sed '2c No such person'
ID Name PHP Linux MySQL Average
No such person
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]# sed -i'2c No such person' student.txt
[root@localhost ~]# sed's/旧字符串/新字符串/g' 文件名
替换的格式和 Vim 非常类似,例如:
[root@localhost ~]# sed '3s/74/99/g' student.txt
#在第三行中,把74换成99
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
2 Sc 99 96 87 85.66
3 Gao 99 83 93 91.66
[root@localhost ~]#sed '4s/^/#/g' student.txt
#在这里使用正则表达式,"^"代表行首
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
#3 Gao 99 83 93 91.66
[root@localhost ~]# sed -e 's/Liming//g; s/Gao//g' student.txt
#同时把"Liming"和"Gao"替换为空
ID Name PHP Linux MySQL Average
1 82 95 86 87.66
2 Sc 74 96 87 85.66
3 99 83 93 91.66
[root@localhost ~]# sed -e 's/Liming//g
> s/Gao//g' student.txt
ID Name PHP Linux MySQL Average
1 82 95 86 87.66
2 Sc 74 96 87 85.66
3 99 83 93 91.66
本文链接:http://task.lmcjl.com/news/4940.html