如何使用sed替换我的源文件中的版权/许可证标头? [英] How can I use sed to replace copyright/license headers in my source files?

查看:179
本文介绍了如何使用sed替换我的源文件中的版权/许可证标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将所有Java源文件中的LGPL许可标题替换为Apache License 2.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/>.
 */

需要成为

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

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



我理解sed工作在一行

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

推荐答案

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

c 命令将行的范围更改为指定的文本。范围由包含Project Foo is free software的行标识,直到包含Project Foo的行。
选项 sed 表示GNU sed ;因此,我假设你也有GNU find xargs ,并使用 -print0 -0 以避免文件名中的空白等问题。

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.

为此,我可能会把 sed 脚本放入一个文件( sed.script ),然后用于:

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 {} +


只是一个问题:对齐方式在星号上有点偏差,有没有一些空格字符我需要使用缩进他们?我尝试给替换字符串添加空格,但似乎没有效果。

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 ...这是一种刺激做没有(和你也)。看来,'change'数据行上的前导空格被 sed 删除。它似乎是 sed ,而不是 bash ;我得到了与 ksh 相同的结果,并使用脚本文件,而不是命令行上的 -e 选项。您不能在输出时修改更改数据。

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 {} +
$


b $ b

s2p 程序是Perl发行版的标准部分,它将 sed 脚本转换为Perl脚本,但它保留替代数据中的前导空格。我不喜欢这个,但我唯一的选择,我可以想到的是通过每个文件两次。替换数据可能是:

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/^@\*@/ */' {} +
$

这会跟踪开始 @ * @ 的行,并用 * '(blank-star)。不是那么整洁,但你不会经常这样做,我相信。

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.

这篇关于如何使用sed替换我的源文件中的版权/许可证标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆