使用sed,grep 批量修改文件内容

使用sed命令可以进行字符串的批量替换操作,以节省大量的时间及人力;

使用的格式如下:

sed -i "s/oldstring/newstring/g" `grep oldstring -rl path`

其中,oldstring是待被替换的字符串,newstring是待替换oldstring的新字符串,grep操作主要是按照所给的路径查找oldstring,path是所替换文件的路径;

-i选项是直接在文件中替换,不在终端输出;

-r选项是所给的path中的目录递归查找;

-l选项是输出所有匹配到oldstring的文件;

在使用sed,然后用变量替换指定的字符串,一直出现这个错误;

把分隔符/替换成#就可以:

sed -i "s#oldstring#newstring#g" `grep oldstring -rl path`

参考网址:

https://www.cnblogs.com/coffy/p/5607913.html

https://www.cnblogs.com/lemon-le/p/6020695.html

原文地址:https://www.cnblogs.com/sien6/p/8194028.html