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