Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:23:45+00:00 2026-06-16T15:23:45+00:00

I need to replace the LGPL license header in all of my Java source

  • 0

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
 * 
 * This file is part of Project Foo.
 *
 * Project Foo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Project Foo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Project Foo.  If not, see <http://www.gnu.org/licenses/>.
 */

needs to become

/*
 * Copyright (c) 2012 Tyler Treat
 * 
 * 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.
 */

I figured the easiest way would be to use sed to do a find and replace on all occurrences of this copyright header. I’m a bit of a Unix novice, so I was having problems getting the command working the way I needed it to — specifically, dealing with the multiline strings. Basically, something like below, except the respective headers in place of foo and bar:

find . -name "*.java" -print | xargs sed -i 's/foo/bar/g'

I understand that sed works on one line at a time, so maybe there is a better solution altogether?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:23:47+00:00Added an answer on June 16, 2026 at 3:23 pm
    find . -name "*.java" -print0 | xargs -0 \
    sed -i -e '/Project Foo is free software/,/along with Project Foo/c\
     * 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.'
    

    The c command changes the range of lines to the specified text. The range is identified by the line containing ‘Project Foo is free software’ up to the line containing ‘along with Project Foo’.
    The -i option to sed indicates GNU sed; therefore, I’m assuming that you’ve GNU find and xargs too, and used -print0 and -0 to avoid issues with blanks in file names etc.

    For this, I might be tempted to put the sed script into a file (sed.script), which could then be used with:

    find . -name "*.java" -exec sed -i -f sed.script {} +
    

    This is neater, I think, but beauty is in the eye of the beholder.


    Just one question: the alignment is a little off on the asterisks, is there some sort of whitespace character I need to use to indent them? I tried adding spaces to the replacement string but that seemed to have no effect.

    Grrr…that’s the sort of irritation I could do without (and you too). It seems that leading blanks on the ‘change’ data lines are dropped by sed. It seems to be sed rather than bash; I got the same result with ksh and also using a script file instead of the -e option on the command line. You can’t edit the ‘change’ data as it is output.

    One trick that would work — but you may not be keen on it:

    $ cat sed.script
    /Project Foo is free software/,/along with Project Foo/c\
     * 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.
    $ s2p -f sed.script > perl.script
    $ find . -name "*.java" -exec perl -f perl.script -i.bak {} +
    $
    

    The s2p program is a standard part of the Perl distribution which converts sed scripts into Perl scripts, but it preserves the leading spaces in the substitute data. I’m not keen on this, but the only alternative I can think of is making two passes through each file. The replacement data might be:

    $ cat sed.script
    /Project Foo is free software/,/along with Project Foo/c\
    @*@ 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.
    $
    

    After doing the main text replacement, you’d then do:

    $ find . -name "*.java" -exec sed -i 's/^@\*@/ */' {} +
    $
    

    This tracks down the lines starting @*@ and replaces that text with ‘*‘ (blank-star). Not as neat and tidy, but you aren’t going to be doing this all that often, I trust.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace all & with with &amp; in a string like this:
I have a string. In this string i need replace all special characters (0-31
I need to replace for ' in all csv files in one directory. I
Need to replace a domain name on all the links on the page that
I need to replace all System.Environment.Newline(s) in the string returned by my function with
need to replace <wiki>this page</wiki> to <a href='wiki/this_page'>this page</a> using callback function: text =
i need to replace Sprite name = new Sprite(number1, number2, this.mScritteRegion.get(number3) { stuff_here };
I need to replace a bit of text in multiple css files, but ony
I need to replace one of the pre-defined window classes all across Windows. For
I need to replace all double quotes to single quotes using mysql query. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.