ILi*_*cos 3 regex shell perl sed
我需要catch在包含数千个PHP文件的项目中插入一个调试指令.
我想匹配模式
catch (
所以在每个匹配模式之后,我想插入指令:
Reporter::send_exception($e);
我一直在尝试使用sed来实现这一目标,但我未能成功.
这是我正在使用的sed命令:
sed -e '/catch \(/{:a,n:\ba;i\Reporter::send_exception\(\$e\);\g' -e '}' RandomFile.php
任何写这篇文章的帮助将不胜感激!
我在Stack Overflow中看到了同样问题的其他解决方案,但这些解决方案都没有奏效.
谢谢
编辑
基本上我的文件看起来很像这样:
try {
do_something();
} catch ( AnyKindOfException $e) {
Reporter::send_exception($e); // Here's where I want to insert the line
// throws generic error page
}
Run Code Online (Sandbox Code Playgroud)
这就是为什么我想匹配catch \(*$
并在插入之后
Reporter::send_exception($e)
您可以使用sed \a允许您追加该行的命令来执行此操作.语法是:
sed '/PATTERN/ a\
Line which you want to append' filename
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下它将是:
sed '/catch (/ a\
Reporter::send_exception($e);' filename
Run Code Online (Sandbox Code Playgroud)
$ cat fff
adfadf
afdafd
catch (
dfsdf
sadswd
$ sed '/catch (/ a\
Reporter::send_exception($e);' fff
adfadf
afdafd
catch (
Reporter::send_exception($e);
dfsdf
sadswd
Run Code Online (Sandbox Code Playgroud)