再帰の検索と置換

再帰の検索と置換

ディレクトリ内のすべてのファイルを再帰的に変更したいと思います。

交換したい項目:

<?php

そして

 <?php
 /**
  *
  * Copyright (C) MyCompany, Ltd. - All Rights Reserved
  * Unauthorized copying of this file, via any medium is strictly prohibited
  * Proprietary and Confidential
  * Written by Justin E <[email protected]>, March 2014
  *
  */

最良の解決策は何ですか?私はいくつかのsedを知っていますが、専門的に見えるようにギャップと改行を維持したいと思います。

ベストアンサー1

dummy交換したいコンテンツを含むファイルを作成します。その後、dummyファイルは次のようになります。

<?php
 /**
  *
  * Copyright (C) MyCompany, Ltd. - All Rights Reserved
  * Unauthorized copying of this file, via any medium is strictly prohibited
  * Proprietary and Confidential
  * Written by Justin E <[email protected]>, March 2014
  *
  */

その後、次のスクリプトを実行します。

for f in ./*; do
sed -i '/<?php/{
    s/<?php//g
    r dummy
}' $f
done

スペースで置き換え<?php、ファイルの内容で置き換えましたdummy

おすすめ記事