我有一个特定的PHP代码评论,出现在几个用户的约75个文件。 这个命令succsessfully定位它。 (我没有费心寻找双斜线,因为我不确定是否逃跑了,他们真的没关系。)
grep -l -r "calls the myfunc() function" /home/*/public_html
find之后,我需要添加换行符和一些更多的PHP代码
if($my_return_var===false){echo "<b>THERE WAS AN ERROR</b>";}
我也需要做类似的事情
grep -l -r "include('path/to/my/file.php')" /home/*/public_html
然后插入它之前
"$my_return_var="
search谷歌,我发现greppipe道sed可能是要走的路,但我不确定的语法。
谁能帮忙? 顺便说一句,这个Q / A可能很接近,但还是有点过头了。 如何在Linux中replace多个文件中的文本string
sed可以在看到一个模式时插入和追加:
grep -l -r "calls the myfunc() function" /home/*/public_html | xargs sed -i '/calls the myfunc() function/a\ if($my_return_var===false){echo "<b>THERE WAS AN ERROR</b>";} ' grep -l -r "include('path/to/my/file.php')" /home/*/public_html | xargs sed -i "/include('path\\/to\\/my\\/file.php')"'/i\ "$my_return_var=" '
第一件事: 做一个备份!
第二件事: 做一个备份!
因为我知道perl比python好多了,所以我会使用perl解决scheme。
请记住,“\ n”是perl中的换行符,所以如下所示:
grep -l -r "calls the myfunc() function" /home/*/public_html |xargs perl -pi -e 's/(calls the myfunc() function;)/$1\n if($my_return_var===false){echo "<b>THERE WAS AN ERROR</b>";}/'
我显然没有testing这个,这是一个powershell的方式来做到这一点(并假设一堆东西,例如,grep不会返回文件名和空格等等)。 如果你的search/replace模式包含“/”,你可以使用另一个分隔符到你的模式,例如s?(calls the myfunc() function;)?$1\n if($my_return_var===false){echo "<b>THERE WAS AN ERROR</b>";?'