我需要搜索一个有字符串的文件,只有在没有注释的情况下才用新行替换整行。实际情况是,只有在include语句没有注释的情况下,我才需要用新文件替换文件的include。
的例子:
include("../mytest.php");
// include("../mytest.php");
预期结果:
require_once("mytestnew.php");
// include("../mytest.php");
我尝试了以下在web上不同地方找到的一些sed命令:
sed -i '/^['t]*'/'/.*/!/include.*mytest.php/c'require_once("mytestnew.php");' file.php
sed -i '/^['t]*'/'/.*/!{include.*mytest.php/c'require_once("mytestnew.php");}' file.php
sed -i '/^['t]*'/'/.*/b;include.*mytest.php/c'require_once("mytestnew.php");' file.php
sed -i '/^['t]*'/'/.*/!s/include.*mytest.php/c'require_once("mytestnew.php");/g' file.php
在大多数其他情况下,要么我得到意想不到的{或s等错误,要么结果不是预期的
Try
sed 's#^[ 't]*include("../mytest.php");#require_once("../mytestnew.php");#' inputfile
如果您想保留未注释的include
行中的前导空格,请尝试:
sed 's#^'([ 't]*')include("../mytest.php");#'1require_once("../mytestnew.php");#' inputfile
你试过吗?
sed 's|^include("../mytest.php");|require_once("mytestnew.php");|g' file.php
我在使用转义字符后得到了它:
sed -i 's#^['t]*include.*mytest.php");#require_once("'.'.'/mytestnew.php");#' file.php