更改多个文件中的文本

更改多个文件中的文本

我有一个 C++ 项目,每个文件的开头都有几行许可证。现在,我的许可证已经更改,我需要在每个文件中更新它。我不想手动做这件事,因为这需要大量工作。有没有办法让它更自动化?我试过了http://regexxer.sourceforge.net/但它仅在处理短文本时有用(它并没有取代我的许可证)...

我的执照:

/****************************************************************************
**
** Copyright (c) 2014, mirx
** All rights reserved.
**
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of XYZ and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
**
****************************************************************************/

我只是想在这个代码中添加几行(用新版本替换)= 但是怎么做呢?

答案1

另一种解决方案是使用awk

创建一个像这样的新脚本changeLicense.awk

{
  if (match($1,"^/\*.")!=0) {
     i = 0; flag = "false";
     comment[i++] = $0;
     while(getline > 0) {
       if (match($0,"Copyright") != 0) flag = "true"; 
       comment[i++] = $0; 
       if (match($0,"\*/$")!=0) {
         if (flag == "false") {
           for (J=0; J<i; J++) print comment[J];
         }
         else {
          while ((getline line < newLicense) > 0)
               print line;
          close(newLicense); 
         }
         next;
       }
     }
  } 
  print;
}

该脚本查找内部的行块/**/如果注释行块包含“Copyright”字符串,则用 newLicense 文件内容替换块,否则保留注释。

为了更改项目所有文件中的许可证:

 find /path/project -name "*.cpp" -exec bash -c 'awk -f /path/changeLicense.awk -v newLicense=/path/fileWithNewContent $1 > $1.new; mv $1.new $1' _ {} \;

该脚本changeLicense.awk在每个.cpp文件上运行/path/project,将结果放入.new文件中,然后替换原始文件.cpp

在 lubuntu 12.04 上测试

答案2

如果许可证文本在每个文件中总是出现在完全相同的位置,那么您可以简单地使用sed在给定的行号后附加新文本,例如

sed '34a \
** YOUR ADDITIONAL LICENCE TERMS\
** ADDED HERE
' file1.c

例如,您可以将其与find将其应用于每个文件相结合.c

find -name '*.c' -exec sed -i '34a \
** YOUR ADDITIONAL LICENCE TERMS\
** ADDED HERE
' {} \;

相关内容