I need to replace the LGPL license header in all of my Java source files with the Apache License 2.0 header, i.e. this
/*
* Copyright (c) 2012 Tyler Treat
You can use GNU sed to solve this with some regular expression line matches and a read expression. Here are the steps.
First, create a file to hold the replacement portion of your license:
cat << EOF > /tmp/license
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
EOF
Next, run find to collect your file list, and invoke the following sed script to make the changes:
find . -name '*.java' |
xargs sed -i'' '/Copyright.*Tyler Treat/,/\*\// {
/Copyright/n
/\*\//r /tmp/license
d
}'
This solution may or may not work with other versions of sed, but was tested locally and known to work with GNU sed version 4.2.1. If it doesn't work with the version of sed shipped with your edition of OS X, you can install GNU sed via MacPorts or similar.