【问题标题】:replace license notice in every file替换每个文件中的许可证通知
【发布时间】:2012-12-04 09:02:12
【问题描述】:

我想在一个相对较大的项目的每个文件的顶部替换多行许可通知(从GNU GPLApache 2.0)。许可通知由几段组成。另一个要求是目标许可证通知中有一个取决于当前文件名的占位符,因此简单的查找和替换是不够的。

我很熟悉这样做:

find . -name "*.java" -exec sed -i 's/find/replace/g' {} \;

但我不知道如何使它适用于这个用例。

更新:

目标 Apache 2.0 许可证的占位符如下所示:

Copyright [yyyy] [name of copyright owner]
[filename.java] <br/><br/>

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<br/><br/>

http://www.apache.org/licenses/LICENSE-2.0<br/><br/>

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.

【问题讨论】:

  • 我希望您不要因此违反许可条款... ;-)
  • 我是版权所有者,我可以对我的项目的新版本做任何我想做的事情;)
  • @GiovanniAzua 更新了我的回复,说明了如何添加占位符...如果占位符已经是旧通知的一部分,请告诉我 - 我认为我们应该差不多完成了。

标签: linux unix sed awk replace


【解决方案1】:

使用以下sed 命令删除从start_pattern 开始并以end_pattern 结束的行:

sed -n '/start_pattern/{:a;N;/end_pattern/!ba;N;s/.*\n//};p' file

例如,要删除 GNU GPL 许可证,您可以使用:

sed -n '/GNU GENERAL PUBLIC LICENSE/{:a;N;/why-not-lgpl.html\>./!ba;N;s/.*\n//};p' file

要在多个文件上运行它,使用 findxargs

find . -name "*.java" -print0 | xargs -0 sed -i -n '/GNU GENERAL PUBLIC LICENSE/{:a;N;/why-not-lgpl.html\>./!ba;N;s/.*\n//};p'

【讨论】:

    【解决方案2】:

    我知道 clicki-buntis 并不总是更好,但对于这种情况,我使用“kfilereplace”。它是为此目的而设计的 kde 工具。它允许您设置正则表达式并运行模拟通行证。这样,您可以首先测试您的设置,然后进行“实时”替换。

    对于占位符:

    • 进行两次更换,分别更换占位符前后的部分。这样你只需要替换两个静态字符串,没有动态的。
    • 使用许多正则表达式替换函数提供的占位符替换策略来接管要替换的文本的动态占位符部分。

    【讨论】:

    • 感谢您的回答!我使用颠覆,所以我可以“空运行”它但是我需要而不会破坏/丢失任何东西。
    • 我当然希望您使用某种代码存储库。关于丢失内容:我学会了总是任何签入操作之前检查每个单个文件的更改 :-) kdesvn 非常适合这个.甚至不需要鼠标,全部由键盘驱动。
    【解决方案3】:

    Perl:

    # First, get the text for the Apache license, stick it in a shell variable:
    export APACHE="$(curl -s http://www.apache.org/licenses/LICENSE-2.0.txt)"
    
    
    # For a single file:
    perl -p -i -e 'BEGIN{undef $/} 
      s#GNU GENERAL PUBLIC LICENSE.*<http://www.gnu.org/philosophy/why-not-lgpl.html>.# Copyright... [$ARGV] <br/> ... $ENV{APACHE}#smg' A.java
    

    注意事项:

    1. 在 Perl 中,$ARGV 包含文件名(当前正在处理的输入文件)

    我猜你可以使用findxargs 递归地执行此操作。

    find . -name "*.java" | xargs -l1 perl ....
    

    【讨论】:

    • 感谢您的回答。占位符如何适合此解决方案?
    • 实际上只是想问你这个问题 - 占位符在哪里? GPL 中的某个地方?还是在外面?他们看起来怎么样?我猜你可以将它放在搜索文本 s#GNU...(your-place-holder-pattern)..#$ENV{APACHE} $1# 中的某个位置 - $1 应该包含你的占位符 - 一旦我得到更好的图片,我会清理我的答案。
    • 占位符是您想在新通知中添加的内容,还是这些已经是当前通知的一部分?
    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2021-07-26
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多