0

I'm looking for a shell script (Bash) that adds or replaces copyright text at the beginning of the file (for .c and .h files).

It should search and add or replace the copyright text block for all the .c and .h files from the parent directory where I execute my script.

Existing copyright text:

            /* --------------------------------------------------
                Copyright 2014 Author name

                All rights reserved
             ----------------------------------------------------*/

New Copyright text.

             /* Copy right text bla bla bla
              * some license text bla bla bla
              * All rights reserved xyz xyz */

Note

  1. Copyright text should be added at the beginning of the file (.c and .h) only and always the existing copyright text will be in the beginning of the file.

  2. It should search and add or replace for all the .c and .h files within the parent directories and sub directories.

EDIT

This is what I tried:

      for file in `find . -type f -name "*.h"`
       do
       echo $file
       if grep -qRin "                            ----------------------------------------------------------------------\*\/" $file
        then
       echo "Replaced License Text"
         sed '1,/^ ----------------------------------------------------------------------\*\/$/d' < $file | cat licence_file - > $file.new
       mv $file.new $file
      else
    cat licence_file $file >$file.new && mv $file.new $file
     echo "Added License Test"
     fi
      done

Licence_file contains my new license text.

  • 1
    See: stackoverflow.com/questions/14107309/… – muru Dec 5 '14 at 9:01
  • @jasonwryan : Edited the question. It works fine but the problem is it removes all the content from the matched text to beginning of the file.So if there is any code before the license text it will get removed :( – Thushi Dec 15 '14 at 6:22
2

ed is 'the standard text editor'. It's not really ideal to use for day-to-day editing, but it is readily scriptable.

ed file.c <<'EOF'
1,/\*\//d
i
/* Copy right text bla bla bla
 * some license text bla bla bla
 * All rights reserved xyz xyz */
.
w
q
EOF

The first command deletes from the first line, up to and including the first line containing a */ (both the * and the / need to be escaped). The second command is a simple insert -- the i tells ed to enter 'insert' mode, while a . on its own tells ed to exit insert mode. w means 'write', and saves the file; q means 'quit'. The whole thing is within a 'here-document', which begins and ends with an EOF (this is arbitrary; you can use any string you want).

This can easily enough be used in a loop:

for f in ./*.{c,h}; do ed "$f" <<'EOF'
1,/\*\//d
i
/* Copy right text bla bla bla
 * some license text bla bla bla
 * All rights reserved xyz xyz */
.
w
q
EOF
done

If you want recursiveness (i.e. to work in subdirectories), either use shopt -s globstar and use ./**/*.{c,h} in the above command, or use find and a while loop:

find . -name '*.c' -name '*.h' -print0 | while read -d '' f; do ed "$f" <<'EOF'
1,/\*\//d
i
/* Copy right text bla bla bla
 * some license text bla bla bla
 * All rights reserved xyz xyz */
.
w
q
EOF
done

If you might do this many times, it might be worth putting it in a script.

#!/bin/sh
ed "$1" <<'EOF'
1,/\*\//d
i
/* Copy right text bla bla bla
 * some license text bla bla bla
 * All rights reserved xyz xyz */
.
w
q
EOF

If you name this something like copyright.sh, drop it in somewhere in your $PATH, and make it executable, then you can simply:

shopt -s globstar
for f in ./**/*.{c,h}; do copyright.sh "$f"; done
  • The 'r' read command is useful. ed config.inc.php <<'EOF' /mysecrettoken/ .,+1d .-1r token.txt w q EOF – P. Huhn Feb 13 at 13:51
0

A little messy, but this python will work:

#!/usr/bin/python
import sys
filename = sys.argv[1]
fo=open(filename,'r+')
fr=fo.read()
frs=fr.replace('/* --------------------------------------------------\n                Copyright 2014 Author name\n\n                All rights reserved\n             ----------------------------------------------------*/','/* Copy right text bla bla bla\n* some license text bla bla bla\n* All rights reserved xyz xyz */')
fo.seek(0,0)
fo.write(frs)
fo.close()

This works if the original text and replacement text are exactly in the same format(including the hyphen lines) as given in the question. Call this python from the directory where the files exist using:

for i in ./{*.c,*.h} ; do python <scriptname.py> $i ; done

How it works:

Straightforward. It just opens the files, and replaces the lines given between the first couple of single-quotes in the frs=fr.replace.... line with the lines given in the second couple of single-quotes given in the same line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.