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
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
Very useful script. It saved my workday!
Thanks for the script. Works perfectly and saved me a lot of time!
Thanks ! Helped me a lot
You code just saved me from a whole lot of boring work!
Spot on 🙂
Reblogged this on Gnosis Hub and commented:
Handy Script to replace Copyright headers of source files
Thanks, nice tool
Not able to download
Not able to download
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