Howto recursively replace file headers of source files

This scripts can replace file headers from C/C++/JAVA header and source files. Often it’s necessary to replace the license conditions in multiple source files, update the copyright year, or simply make all file headers consistent. This can be a lot of work, so that’s why I wrote this scripts.

How it works: The BASH script replace_header.sh is a simple wrapper that executes the AWK script remove_header.awk which implements a basic state machine for recognizing C/C++/JAVA comments (“// …”, “/* … */). All comments at the beginning of the file – also when spanning over multiple lines – are ignored until the first line without comment is reached. The AWK file ignores all this comments, and prints the rest of the file unchanged. The BASH script creates a new file with your new header.template and adds your code without the old file header to the newly created file. Afterwards it copies the new file over the old one.

Example:

find . -name "*.h" -exec ~/rh/replace_header.sh {} \;

Download replace_header_scripts.tar.gz

Advertisements

13 Responses to “Howto recursively replace file headers of source files”


  1. 1 Thomas June 10, 2009 at 8:34 am

    Thank you, this helped me.

    I just had an error, I had to change the regexp in line #15 of the awk script to /^\/\*[^\/]*\*\/$/

    (escaped the / character inside the brackets)

    • 2 gergap June 10, 2009 at 8:52 am

      Hi Thomas,

      I’ve fixed this issue and uploaded a new version.
      I guess it worked for me because the slash is inside a character class and so it works without escaping it – at least in my awk version. But it’s safer and correct to escape it also inside the character class.
      Thanks for that correction.

      regards,
      gergap

  2. 3 Felix March 9, 2011 at 5:26 pm

    Very useful script. It saved my workday!

  3. 4 Simon September 18, 2012 at 12:17 pm

    Thanks for the script. Works perfectly and saved me a lot of time!

  4. 5 redwarp April 17, 2013 at 9:12 am

    Thanks ! Helped me a lot

  5. 6 ratking82 May 14, 2013 at 7:33 am

    You code just saved me from a whole lot of boring work!

  6. 7 Tapasya September 5, 2013 at 6:28 pm

    Spot on 🙂

  7. 8 Tapasya September 5, 2013 at 6:33 pm

    Reblogged this on Gnosis Hub and commented:
    Handy Script to replace Copyright headers of source files

  8. 10 Anand August 24, 2016 at 4:49 am

    Not able to download

  9. 11 Anand August 24, 2016 at 4:48 am

    Not able to download

  10. 12 gergap August 24, 2016 at 7:33 am

    I added the files to a new git repo.
    Try to clone it from here. The files you need are in the subfolder rh/.
    https://github.com/gergap/bash_scripts

    Cheers,
    Gerhard


  1. 1 Bash Collection - Howto recursively replace file headers of source files - The Linux Counter Blog Trackback on September 10, 2015 at 7:13 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: