1、准备好目标文件
vartest2.sh
#!/bin/bash
var1="ssdww"
var2=`cat /home/test`
var3="ss22233"
var4="qqqqq"
var5="192.168.1.1"
var6="hello world"
test1="aaaa"
test2="bbbbb"
test3='sdddd'
cinfo=`cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c`
ex=$var6
2、查询(指令:p)
#按行查询
#显示第一行
[gaoshou@localhost test]$sed -n '1p' vartest2.sh
#显示最后一行
[gaoshou@localhost test]$sed -n '$p' vartest2.sh
#显示3到最后行
[gaoshou@localhost test]$sed -n '3,$p' vartest2.sh
#模糊查询
#查询包含var的行
[gaoshou@localhost test]$sed -n '/var/p' vartest2.sh
#查询包含$的行
[gaoshou@localhost test]$sed -n '/\$/p' vartest2.sh
#查询并列出 var3 所在行的行数
[gaoshou@localhost test]$sed -n '/var3/=' vartest2.sh
3、增加(当前行后一行增加指令:a)(当前行前一行增加指令:i)
#按行新增
#第一行后增加一行 welcome
[gaoshou@localhost test]$sed -i '1a welcome' vartest2.sh
#第一行到第三行后增加一行 thanks
[gaoshou@localhost test]$sed -i '1,3a thanks' vartest2.sh
#第一行后增加两行 hello 和 thanks
[gaoshou@localhost test]$sed -i '1a hello\nthanks' vartest2.sh
#最后一行后增加hello,这里要对$转义
[gaoshou@localhost test]$sed -i '\$a hello' vartest2.sh
#模糊匹配后新增
#在test3=所在行后增加一行find you
[gaoshou@localhost test]$sed -i '/test3=/a find you' vartest2.sh
#在test1=后追加 come on
[gaoshou@localhost test]$sed -i "s/test1=/&come on/g" vartest2.sh
#在test1=所在行后追加 come on
[gaoshou@localhost test]$sed -i "s/test1=.*/&come on/g" vartest2.sh
注意:这里使用双引号是为了应对被替换内容(come on)使用变量的情形
#插入行(在前插入) i
#在第二行上一行插入 welcome home
[gaoshou@localhost test]$sed -i '2i welcome home' vartest2.sh
4、替换(指令:c)
#按行替换
#第二行替换为var0='2232'
[gaoshou@localhost test]$sed -i "2c var0='2232'" vartest2.sh
#第一行到第三行替换成 thanks
[gaoshou@localhost test]$sed -i "1,2c var0='2232'" vartest2.sh
#替换匹配到的某一行整行
#替换var0所在的整行 ysss="port"
[gaoshou@localhost test]$sed -i "/^var0.*/cysss=\"port\"" vartest2.sh
#替换匹配到的某一行的某一部分,将cinfo所在行的 grep name 替换成 grep -n name
#先找到匹配的行号并列出,方法一
[gaoshou@localhost test]$rowNumber=`grep -n 'cinfo' vartest2.sh|awk -F ':' '{print $1}'`
#先找到匹配的行号并列出,方法二
[gaoshou@localhost test]$rowNumber=`sed -n '/cinfo/=' vartest2.sh`
#对指定行号的内容进行指定内容的替换处理
[gaoshou@localhost test]$sed -i "${rowNumber}s/grep name/grep -n name/g" vartest2.sh
#或者合并成一条指令
[gaoshou@localhost test]$sed -i "`grep -n 'cinfo' vartest2.sh|awk -F ':' '{print $1}'`s/grep name/grep -n name/g" vartest2.sh
扩展:
#列出包含cinfo的行号
sed -n '/cinfo/=' vartest2.sh
#列出以cinfo开头的行号
sed -n '/^cinfo/' vartest2.sh
#列出以cinfo结尾的行号
sed -n '/cinfo$/' vartest2.sh
上述涉及到正则表达式,后续专篇分享。
#替换某一部分 s/待替换字符串/目标字符串/g
#将hello替换成HI
[gaoshou@localhost test]$sed -i 's/hello/HI/g' vartest2.sh
#删除 how do u do
sed -i 's/how do u do//g' vartest2.sh
5、删除(指令:d)
#删除 d
#按行删除
#删除第一行
[gaoshou@localhost test]$sed -i '1d' vartest2.sh
#删除第二行到第三行
[gaoshou@localhost test]$sed -i '2,3d' vartest2.sh
#删除ysss所在行
[gaoshou@localhost test]$sed -n '/ysss/d' vartest2.sh
6、变量的使用
#下列四种格式写法都可以实现带变量执行
[gaoshou@localhost test]$eval sed 's/$var1/$var2/' vartest2.sh
[gaoshou@localhost test]$sed "s/$var1/$var2/" vartest2.sh
[gaoshou@localhost test]$sed 's/'$var1'/'$var2'/' vartest2.sh
[gaoshou@localhost test]$sed s/$var1/$var2/ vartest2.sh
7、变量转义报错【sed:-e 表达式 #1,字符 10:“s”的未知选项】
#!/bin/bash
dir=/home/gaoshou/file
sed -i "s/${dir}.*/& ssdddxx/g" vartest2.sh
执行上述脚本报错:
sed:-e 表达式 #1,字符 10:“s”的未知选项
原因:
这是由于${dir}变量内容中有 / 符号导致的,因此需要进行 / 转义。
#!/bin/bash
dir=/home/gaoshou/file
#转义 /
dir=${dir//\//\\\/}
sed -i "s/${dir}.*/& ssdddxx/g" vartest2.sh
执行ok。
注意:sed是不能对空文件进行修改的!!!!
如果有遇到文件的话,这里建议使用判空的写法,以下提供一个样例吧。
[gaoshou@localhost test]$test -s vartest2.sh && sed -i "\$a byebye" vartest2.sh||echo "byebye" >> vartest2.sh