一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

linux中Sed 正则替换代码中的日志开关、错误级别值修改、注释代码

时间:2013-11-28 编辑:简简单单 来源:一聚教程网

背景:多个项目进行代码的日志打印级别统一替换时,如果文件庞大,数目多,一个个去做显然力不从心,得通过脚本,这种替换sed最适合不过了。
如题:
I:
我想把:log_threshold =N;
替换为:log_threshold =1;

我想把:show_debug_errors=AnyWord;
替换为:show_debug_errors=false;

II:
error_reporting(E_ALL ^ E_NOTICE);

变:

//error_reporting(E_ALL ^ E_NOTICE);

I):日志开关,错误级别修改:

一、Sed替换方法如下(该处由scottjiang兄弟提供,后面我发挥了一下下:-)):

 sed "s/log_threshold = .*;$/log_threshold = 1;/g;s/show_debug_errors = .*;$/show_debug_errors = false;/g" index.php > index.php.new
mv  index.php.new index.php


二、如果不想备份直接替换:

 
sed -i  "s/log_threshold = .*;$/log_threshold = 1;/g;s/show_debug_errors = .*;$/show_debug_errors = false;/g" index.php


三、多个项目的index.php都要统一用sed替换:

 
 grep -rl "log_threshold =" ./|grep index.php|xargs sed -i  "s/log_threshold = .*;$/log_threshold = 1;/g;s/show_debug_errors = .*;$/show_debug_errors = false;/g"

II):sed实现对PHP代码某特征给注释一行(如:项目上线时不想让其打开错误报告给用户):
如想:
error_reporting(E_ALL ^ E_NOTICE); 变:
//error_reporting(E_ALL ^ E_NOTICE);
sed替换方法:

 
sed 's/error_reporting///error_reporting/g'  ./index.php

而:也可结合上面三实现批量替换实现多个文件包含上面串的该行给予注释掉:

  
grep -rl "error_reporting(E_ALL ^ E_NOTICE);" ./|grep index.php|xargs sed -i 's/error_reporting///error_reporting/g'


四、替换由mysqldump出的自境长字段为0:

情形:Mysqldump出的线下自增长字段,会有默认值,而放到外网则需要从线上开始,此SQL也需要替换的(http://www.111com.net/)
deo` VALUES (1121,NULL,'
用sed替换:
sed -i "s/([0-9]*/(0/g" /tmp/result.sql 
替换后,为0,上线插入时则从线上默认增长,而不是上面的1121:
_video` VALUES (0,NULL,'2'

sed -i 's/([0-9]*,/(0,/g'   //文件名替换(12122, ==>(0,

热门栏目