适合文件中只有一个指定字符串的行
 
#!/bin/bash
 
#查找的字符串
str1="string1"
 
#新字符串
newstr="string2"
 
#被替换的文件名
filename="filename"
 
#获取指定字符串的行号
line=`sed -n '/$str1/=' $filename`
 
#删除这行
sed -i "$line d" $filename
 
#在删除的行插入新字符串
sed -i "$line i$newstr" $filename