0

I need to search for a file having a string and replace whole line with a new line only if it is not commented. Actual scenario is that I need to replace the include of a file with new file only if the include statement is not commented. Example

include("../mytest.php");
// include("../mytest.php");

expected result

require_once("mytestnew.php");
// include("../mytest.php");

I have tried some of the following sed commands found at different places on web:

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

In most of the other cases as well either I am getting unexpected { or s etc error or the result is not as expected.

Thanks in Advance!

4

3 回答 3

2

尝试

sed 's#^[ \t]*include("../mytest.php");#require_once("../mytestnew.php");#' inputfile

如果要保留未注释include行中的前导空格,请尝试:

sed 's#^\([ \t]*\)include("../mytest.php");#\1require_once("../mytestnew.php");#' inputfile
于 2013-06-20T07:52:38.023 回答
0

I got it right after using escape character:

sed -i 's#^[\t]*include.*mytest.php");#require_once("\.\.\/mytestnew.php");#' file.php
于 2013-06-20T08:27:31.360 回答
0

您是否尝试过:

sed 's|^include("../mytest.php");|require_once("mytestnew.php");|g' file.php
于 2013-06-20T07:51:20.390 回答