sed是非交互式文本编辑器,处理行。
sed的三种使用方式:
1.sed [选项] ‘sed命令’ 输入文件
2.sed [选项] -f sed脚本文件 输入文件
3. ./sed脚本文件 输入文件
常用选项:
1.创建文件10.pem
内容
hello
my file
world hello
i am a teacher
打印文件中的第一行(-n),p表示打印,-n表示不打印所有的行
[root@iZ2546h6zurZ test]# sed -n "1p" 10.pem
hello
2.执行多条编辑命令(-e)
[root@iZ2546h6zurZ test]# sed -n -e "1p" -e "/am/p" 10.pem
hello
i am a teacher
3.在行后追加内容(a\),i\在行前面添加内容
[root@iZ2546h6zurZ test]# sed "/teacher/ a\add content" 10.pem
hello
my file
world hello
i am a teacher
add content
4.打印出最后一行($)
[root@iZ2546h6zurZ test]# sed -n ‘$p‘ 10.pem
i am a teacher
5.输出不在2至3行的数据(!)取反
[root@iZ2546h6zurZ test]# sed -n ‘2,3!p‘ 10.pem
hello
i am a teacher
6.修改文本(c\)
[root@iZ2546h6zurZ test]# sed -n ‘/file/ c\modify hello‘ 10.pem
modify hello
7.删除文本(d)
删除第一行
[root@iZ2546h6zurZ test]# sed ‘1d‘ 10.pem
my file
world hello
i am a teacher
删除最后一行
[root@iZ2546h6zurZ test]# sed ‘$d‘ 10.pem
hello
my file
world hello
8.文本替换(s)
将teacher替换为student
[root@iZ2546h6zurZ test]# sed -n ‘s/teacher/student/p‘ 10.pem
i am a student
9.创建文件11.pem
内容
other file
other file
查找10.pem文件中的file,将11.pem的内容添加到file后面
[root@iZ2546h6zurZ test]# sed ‘/file/r 11.pem‘ 10.pem
hello
my file
other file
other file
world hello
i am a teacher
10.多条编辑命令执行
将hello替换为test1,将file替换为test2
[root@iZ2546h6zurZ test]# sed ‘s/hello/test1/g; s/file/test2/g‘ 10.pem
test1
my test2
world test1
i am a teachersed命令
原文:http://blog.csdn.net/liuchangqing123/article/details/44187167