I am trying to update or insert few comments like Copyright headers in to all my source files in a directory (Linux). My files are inconsistent, so that a few of them already have headers while others do not have them at all. I tried with sed to look at the first few lines and replace. Replace I mean change the files which are already having Copyright header with latest one.
sed -e '1,10 s/Copyright/*Copyright*/g' file
But, this will not insert if it did not find the pattern. How can I achieve this?
Example I provided in comments or what I am trying to actually replace/insert is a multiline typical copyright header as follows
/*
* Copyright 1234 XXXNAME, XYZPlace
* text text text text ...........
* blah blah blah */
It may contain some special characters also.
If I understand correctly, you want to:
In addition, you want to:
It seems to me that these two tasks could be boiled down to a single set:
If we can safely assume that a shortened version of the sampletext you put in a comment on your question is valid, and should be inserted at, for example, line 2 of each file, then the following should achieve the very first set of requirements if you’re using GNU sed:
If you’re not running GNU sed (i.e. you’re in FreeBSD or OSX or Solaris, etc), let us know, because the sed script will be different.
How does this work?
The
findcommand is getting the following options:-type ftells it to look only at files (not directories or devices).-notinverts the following option.-exec grep -q Copyright {} \;limits the search to anything with Copyright in it (modified by-not)-exec sed -i'' '2i/* Copyright */' {} \;inserts your copyright notice.This solution may run into difficulty if you want your copyright notice to include special characters that would be interpreted by the sed script. But it answers your question. 🙂
If instead, we want to handle the revised requirements, i.e. remove existing copyright notices first, then we can do this with two one-liners:
First, we remove existing copyright notices.
This may be a little redundant, unless you want to traverse subdirectories recursively, which
finddoes by default. The sed script does nothing to files that have no Copyright info in the first 10 lines, so the following should also work instead, if all your files are in one directory:Next, we add new ones back in.
Or, if you want to do this recursively through subdirectories:
FINAL UPDATE:
I can’t spend more time on this one after this.
What?
The first
-execsearches for the word “Copyright” in the first 10 lines of the file. Just like the first example I posted, above. If grep finds anything, this condition returns true.The second
-execdoes the substitution. It reads the entire file into sed’s hold buffer. Then when it gets to the end of the file, it (g) considers the hold buffer, and (s) does a multi-line substitution.Note that this may very well require some tuning, and it may not work at all if you have comments elsewhere in the file. I don’t recall whether GNU sed supports non-greedy stars. You can research that yourself.
Here’s my test:
This doesn’t maintain your existing Copyright information, but at least it addresses the multi-line issue.