0

文字列を持つファイルを検索し、コメントされていない場合にのみ行全体を新しい行に置き換える必要があります。実際のシナリオでは、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 などのエラーが発生するか、結果が期待どおりではありません。

前もって感謝します!

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

エスケープ文字を使用した直後に取得しました:

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 に答える