shell-script – 查找和替换递归

我想以递归方式修改目录中的每个文件.

我想替换:

<?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 <justin@domain.tld>, March 2014
  *
  */

什么是最好的解决方案?我知道一些sed,但我希望保持间距和换行符,以便它看起来很专业.

最佳答案
我会创建一个虚拟文件,其中包含您要替换的内容.所以虚拟文件如下所示.

<?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 <justin@domain.tld>, March 2014
  *
  */

之后,我将执行以下脚本.

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

我用空格替换<?php并将其替换为虚拟文件中的内容.

转载注明原文:shell-script – 查找和替换递归 - 代码日志