knobbled has asked for the wisdom of the Perl Monks concerning the following question:

I have a large number of .java scripts that need their copyright/legal headers updated (16k+), few are up todate so what is best way to search and replace/change the copyright, preferably using a template (copyright changes freq) like:
/* * $Workfile: AbstractVanillaPeriod.java $ * * Copyright (c) 1998-2003 Blah All Rights Reserved. * * This software is the confidential and proprietary * accordance with the terms of the license agreement * with blah.plc. * * blah.plc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS */

Replies are listed 'Best First'.
Re: Search and replace copyright to .java scripts
by castaway (Parson) on Feb 03, 2003 at 11:37 UTC
    I guess the best way would be:
    1. Read in the new copyright file.
    2. List the directory where the .java files are.
    2a. Open each one
    2b. Write the contents to a new file until you find a chunk of text starting with /* and containing the 'Copyright (c) .. ' text
    2c. Replace the old bits with the new bits from the copyright file read in earlier.
    2d. Write the rest of the file. Close it.
    3. Optionally, rename new file to old filename.

    Thats Psuedocode for it. If you need help on specific bits, then please post what you have tried so we can see where it's going wrong.

    C.

      I use the following code to searches for the .java scripts and print the date line (1999 - 2002) to see how many scrpts out of date. What I would like is to adapt this to change the copyright.
      my $root = 'f:/'; my %hash = (); # # main # { my @files = getFileList(); # find files within given vobs my $n = @files ; print STDERR "Searching $n files\n" ; my $n = 0 ; foreach my $f (@files) # open found files { $n++ ; print STDERR "$n\r" ; unless (open( SRC, $f )) { print STDERR "Unable to open $f: $!\n" ; next ; } while (<SRC>) { last if (/^\s*package/); #next unless (/\$Workfile/m); # get filename next unless (/copyright.*application/i); # get copyright chomp ; # remove newline s/^\s*\*//; # remove comments s/\s+/ /; # multiple whitespace into single whitespac +e s/^\s+// ; # remove any leading whitespace s/\s+$// ; # remove any trailing whitespace $hash{ $_ }++; last ; } close (SRC); } foreach my $k (sort keys %hash) { my $v = $hash{ $k }; #print STDERR "$v: $k\n" ; print STDERR "$_\n" ; } } my @theFiles = (); sub getFileList() { my @files = (); foreach my $k ("dir1", "dir2") { @theFiles = (); find( \&wanted, "$view/$k/src" ); push @files, @theFiles ; } return @files ; } sub wanted { return unless (-f $_ ); return unless ($_ =~ /\.java$/); push @theFiles, $File::Find::name ; }
        # perl -pi -e 's/oldcopyright/newcopyright/ig' `find . -type f -name *.java -print`

        (Note the backticks.)