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

~ by gergap on June 3, 2009.

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

  1. 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)

    • 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

Leave a Reply