shell编程(十)sed指令
sed指令介绍
sed指令是shell中常用常见的指令,一般使用sed来进行文本文件的处理。
一般sed指令是以行为单位进行处理文本文件的。
参数说明
- a:新增,在指定行的下方进行新增行
- c:取代,取代n1到n2行之间的内容
- d:删除某些行
- i:插入,在指定行的上行插入一行内容
- p:打印
- s:替换,和vi中的替换语法类似,将内容替换成指定内容
增加操作
下方增加一行,两种方式
第一种方式
sed 4a'#######' testflie
#第四行的下方插入一行
sed 4i'#######' testflie
#第四行的上方插入一行
第二种方式
sed '/This/a ###########' testfile
# 查找This字符串位置,在该位置下方插入一行
删除
sed '1,3d' testfile
#将1到3行进行删除
取代
sed 1c'hello' testflie
#将第一行取代为hello
替换
sed 's/HELLO/hello/g' testfile
#将大写的HELLO替换为hello