如何使用 sed 替换源文件中的版权/许可证标头?

2023-11-24

我需要用 Apache License 2.0 标头替换所有 Java 源文件中的 LGPL 许可证标头,即

/*
 * 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/>.
 */

需要成为

/*
 * 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.
 */

我认为最简单的方法是使用 sed 查找并替换所有出现的此版权标头。我是一个 Unix 新手,所以我在让命令按照我需要的方式工作时遇到了问题——特别是处理多行字符串。基本上,类似于下面的内容,除了相应的标题代替foo and bar:

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

我知道 sed 一次只能处理一行,所以也许有更好的解决方案?


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命令将行范围更改为指定文本。该范围由包含“Project Foo 是免费软件”的行到包含“与 Project Foo 一起”的行标识。 这-i选项sed表示 GNUsed;因此,我假设你有 GNUfind and xargs也,并且用过-print0 and -0以避免文件名中出现空格等问题。

为此,我可能会想把sed脚本写入文件(sed.script),然后可以与以下命令一起使用:

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

我认为这更简洁,但情人眼里出西施。


只有一个问题:星号的对齐方式有点偏差,是否需要使用某种空白字符来缩进它们?我尝试在替换字符串中添加空格,但这似乎没有效果。

呃……这是我可以不需要的那种刺激(你也是)。似乎“更改”数据行上的前导空白被删除sed。好像是sed而不是bash;我得到了相同的结果ksh并且还使用脚本文件代替-e命令行上的选项。您无法在输出时编辑“更改”数据。

一个可行的技巧——但你可能不喜欢它:

$ 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程序是 Perl 发行版的标准部分,它将sed脚本转换为 Perl 脚本,但它保留替换数据中的前导空格。我对此并不热衷,但我能想到的唯一选择是对每个文件进行两次遍历。替换数据可能是:

$ 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.
$

完成主要文本替换后,您将执行以下操作:

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

这会追踪开始的行@*@并将该文本替换为 '*'(空白星号)。虽然没有那么整洁,但我相信你不会经常这样做。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 sed 替换源文件中的版权/许可证标头? 的相关文章

  • 为什么 NSToolbarItem 自动被禁用?

    我的 Mac 幻灯片应用程序工具栏中的按钮有点问题 我希望在没有活动幻灯片时禁用这些按钮 在有活动幻灯片时启用这些按钮 为了实现这一点 我设置了按钮isEnabled财产给false在开始时 我已经尝试过 Interface Builder
  • 如何在 mac os 10.6 上安装brew或homebrew

    我已经尝试过多次了 谁能告诉我具体的详细步骤吗 我的Mac操作系统是10 6 8 它抱怨一些丢失的包裹 使用它在 Mac OSX 10 6 8 上安装 ruby e curl fsSL https raw githubusercontent
  • R 代码中的命令行

    首先我要说的是我是编程新手 我希望在 R 脚本中从命令行运行 python 脚本 我正在运行 Windows XP 但也有一台运行 Windows 7 的机器 我可以运行以下代码 在 dos 提示符下不会出现错误 cd C Document
  • sed 替换为多行变量[重复]

    这个问题在这里已经有答案了 我正在尝试用跨多行的文本替换单词 我知道我可以简单地使用换行符 n 来解决这个问题 但我想保持字符串 干净 不包含任何不需要的格式 下面的例子显然是行不通的 read r d TEST lt
  • 如何访问命令行参数? [复制]

    这个问题在这里已经有答案了 我使用 python 创建项目设置设置 但我需要帮助获取命令行参数 我在终端上尝试过 python myfile py var1 var2 var3 在我的 Python 文件中 我想使用输入的所有变量 Pyth
  • 有没有办法在 OSX 中安装 gcc 而无需安装 Xcode?

    我用谷歌搜索了一下 似乎没有办法在 OS X 上安装 gcc 而不安装 Xcode 至少需要 1 5GB 的空间 我需要的只是 gcc 而不是 Xcode 附带的其他垃圾 此时 我将采用任何其他类型的 C 编译器 我知道我可以简单地安装 X
  • 基于 Unix ASCII 的命令行图表/绘图工具

    有没有好的命令行 UNIX 图表 绘图 绘图工具 我正在寻找能够在 ASCII 图表上绘制 xy 点的东西 澄清一下 我正在寻找能够以 ASCII 格式输出图形 如 ascii art 风格 的东西 这样我就可以在交互式 shell 会话中
  • 从 Process.StandardOutput 重定向二进制数据会导致数据损坏

    On top of this https stackoverflow com questions 8978390 passing command line arguments from c sharp to a external exe 8
  • 如何在 OS X 上安装 Intel TBB?

    如何在 OS X 10 6 上正确安装开源版本的英特尔线程构建模块 TBB 开源版本似乎没有正确的安装脚本 http www threadingbuildingblocks org ver php fid 154 http www thre
  • SwiftUI:获取动态背景颜色(深色模式或浅色模式)

    有没有一种方法可以系统地访问 SwiftUI 视图的标准动态背景颜色 无论用户处于浅色模式还是深色模式 例如 我知道以下内容可用于获取主要 例如文本 颜色 let textColor Color primary 但我没有看到任何类似的背景颜
  • “代码 。”无法在 OS X/Mac 上的 Visual Studio Code 命令行上工作

    命令code 不起作用本手册 https code visualstudio com docs nodejs 之前的所有其他步骤都有效 如何在 OS X 终端中调用 Visual Studio Code pwd 用户 mona nodejs
  • 类似 jq 中的 sql join

    我有以下 json id 1 type folder title folder 1 id 2 type folder title folder 2 id 3 type item title item 1 folder 1 id 4 type
  • 如何在 Linux 上通过 FTP 递归下载文件夹 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 Locked 这个问题及其答案是locked help locked posts因为这个问题是题外话 但却具有历史意义 目前不接受新的答案
  • 安装python启动文件

    我如何安装pythonstartup文件 以便它在命令上运行 例如python myfile py 我尝试将其安装到我的 home myuserUbuntu的目录 但它说我没有足够的权限 此外 不同的地方交替说它应该全部大写或全部小写 前面
  • NSSharingService 共享子菜单

    如何在 Mac 应用程序中添加共享子菜单 例如 Safari gt 文件 gt 共享 我戳了戳Apple 共享服务示例代码 http developer apple com library mac samplecode SharingSer
  • 从命令行运行 R 代码 (Windows)

    我在名为 analysis r 的文件中有一些 R 代码 我希望能够从命令行 CMD 运行该文件中的代码 而无需通过 R 终端 并且我还希望能够传递参数并在我的代码中使用这些参数 例如就像下面的伪代码 C gt execute r scri
  • 卸载我安装的所有 Python 包及其依赖项,而不用破坏我的 Mac

    有很多问题 但正确的方法似乎是手动卸载不需要的所有内容 pip 在卸载时不会卸载包的依赖项 https stackoverflow com questions 7915998 does uninstalling a package with
  • Perl 和 Unix 如何以相同的顺序对 Unicode 字符串进行排序?

    我正在尝试获取 Perl 和 GNU Linuxsort 1 程序就如何对 Unicode 字符串进行排序达成一致 我在跑sort with LANG en US UTF 8 在Perl程序中我尝试了以下方法 use Unicode Col
  • 在 OS X 上创建和使用静态库

    好的 我正在尝试创建一个 Cocoa 库 静态 并使用 但我不断收到错误 我创建了一个超基本的静态库 TSXLib 其中仅包含一个额外的类 import
  • 减少 CoreData 的调试输出?

    我正在开发一个使用 CoreData 的 iOS macOS 项目 它工作正常 但它会向控制台输出大量调试信息 这使得控制台无法使用 因为我的打印语句隐藏在所有与 CoreData 相关的内容中 我有一个非常简单的 CoreData 设置

随机推荐