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 8260525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:07:10+00:00 2026-06-08T03:07:10+00:00

I am trying to update or insert few comments like Copyright headers in to

  • 0

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.

  • 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-08T03:07:11+00:00Added an answer on June 8, 2026 at 3:07 am

    If I understand correctly, you want to:

    • Find files without a Copyright notice in the first 10 lines, and
    • Add a Copyright notice to those files.

    In addition, you want to:

    • Find files WITH a Copyright notice in the first 10 lines, and
    • Update their notice to your standard text.

    It seems to me that these two tasks could be boiled down to a single set:

    • Remove any existing Copyright notice in the first 10 lines, then
    • Insert a new Copyright notice into the file.

    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:

    find . -type f -not -exec grep -q Copyright {} \; -exec sed -i'' '2i/* Copyright */' {} \;
    

    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 find command is getting the following options:

    • -type f tells it to look only at files (not directories or devices).
    • -not inverts 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.

    find . -type f -exec sh -c 'head {} | grep -q Copyright' \; -exec sed -ne '10,$ta;/Copyright/d;:a;p' {} \;
    

    This may be a little redundant, unless you want to traverse subdirectories recursively, which find does 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:

    for file in *;do sed -ne '10,$ta;/Copyright/d;:a;p' "$file"; done
    

    Next, we add new ones back in.

    for file in *;do sed -i'' '2i/* Copyright */' "$file"; done
    

    Or, if you want to do this recursively through subdirectories:

    find . -type f -exec sed -i'' '2i/* Copyright */' {} \;
    

    FINAL UPDATE:

    I can’t spend more time on this one after this.

    find . -type f \
      -exec sh -c 'head {} | grep -q Copyright' \; \
      -exec sed -ne '1h;1!H;${;g;s:/\*.*Copyright.*\*/:/* Copyright 1998-2012 */' {} \;
    

    What?

    The first -exec searches 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 -exec does 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:

    $ printf 'one\n/* Copyright blah blah\n *\n */\ntwo\n' | sed -n '1h;1!H;${;g;s:/\*.*Copyright.*\*/:/* Copyright 1998-2012 */:g;p;}'
    one
    /* Copyright 1998-2012 */
    two
    

    This doesn’t maintain your existing Copyright information, but at least it addresses the multi-line issue.

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

Sidebar

Related Questions

I'm trying to do a single row insert/update on a table but all the
I am trying work out with MERGE statment to Insert / Update Dimension Table
I'm trying to insert status update into my database in table called blabbing, but
I am trying to perform a update/insert into query for MySQL. Should insert, if
I am trying to update a specific row that uses auto_increment as a primary
Trying to do an insert, I have: jdbcTemplate.update(insert into...., new Object[]{foo.getId(), foo.getName()}) foo.getId() returns
I am trying to use FMDB to insert a few queries into the DB.
I have a database table I am trying to do an UPDATE/INSERT to with
I have a bulk insert that works on SQL Server 2000 that I'm trying
I'm trying to update the Insert action while inserting a new element into an

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.