Alterar texto em muitos arquivos

2

Eu tenho um projeto em C ++, onde cada arquivo tem no início algumas linhas de licença. Agora que minha licença foi alterada, preciso atualizá-la em cada arquivo. Eu não quero fazer isso manualmente, já que é muito trabalho. Existe alguma maneira de torná-lo mais automático? Eu tentei link , mas é útil apenas quando se trata de textos curtos (ele não substituiu minha licença) ...

Minha licença:

/****************************************************************************
**
** 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."
**
**
****************************************************************************/

Eu simplesmente gostaria de adicionar algumas linhas a este código (substitua pela nova versão) = mas como?

    
por mirx 05.10.2014 / 12:11

2 respostas

1

Outra solução que usa awk .

Crie um novo script changeLicense.awk assim:

{
  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;
}

Este script procura o bloco de linhas dentro de /* e */ e se o bloco de linhas comentadas contiver a sequência "Copyright", substitua o bloco pelo conteúdo do arquivo newLicense, caso contrário, preserve o comentário.

Para alterar a licença em todos os arquivos do seu projeto:

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

Este script executa changeLicense.awk em cada .cpp arquivo encontrado em /path/project , coloca o resultado em .new arquivo e substitui o original .cpp

Testado no lubuntu 12.04

    
por Lety 05.10.2014 / 16:27
0

Se o texto da licença sempre aparecer exatamente no mesmo local em todos os arquivos, você poderá simplesmente usar sed para anexar o novo texto após um determinado número de linha, por exemplo,

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

Você pode combinar isso com find para aplicá-lo a todos os arquivos .c , por exemplo

find -name '*.c' -exec sed -i '34a \
** YOUR ADDITIONAL LICENCE TERMS\
** ADDED HERE
' {} \;
    
por steeldriver 05.10.2014 / 13:37