Ubuntu: Change text in many files


I have a C++ project, where each file has at its beginning some lines of license. Now, that my license have changed, I need to update it in each file. I don't want to do it manually, since its lots of work. Is there any way to make it more automatic? I tried http://regexxer.sourceforge.net/ but its only useful when dealing with short texts (it didnt replace my license) ...

My license:

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

I simply would like to add some lines to this code (replace with new version) = but how?


Another solution using awk.

Create a new script changeLicense.awk like this:

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

This script looks for block of lines inside /* and */ and if commented lines block contains "Copyright" string then replace block with newLicense file content, otherwise preserve comment.

In order to change license in all file of your project:

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

This script runs changeLicense.awk on each .cpp file found in /path/project, put result in .new file and than replace original .cpp

Tested on lubuntu 12.04


If the licence text always appears in exactly the same location in every file, then you could simply use sed to append the new text after a given line number e.g.

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

You could combine that with find to apply it to every .c file for example

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »