递归查找和替换

递归查找和替换

我想递归地修改目录中的每个文件。

我想更换:

<?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

相关内容