递归多行替换:更改版权标题 [英] Recursive multi-line replace: changing copyright headers
问题描述
我正尝试用新版本替换项目中的所有版权标头(超过100个文件).目前,每个文件的开头都有这样的内容:
I'm retrying to replace all of the copyright headers in my project (100+ files) with a new version. Currently I have something like this at the start of each file:
<?php
/**
* Project name
*
* @copyright Apache 2.0
* @author FooBar
*/
我希望我的所有文件都这样开始:
And I want all my files to start like this:
<?php
/**
* Copyright 2014 FooBar
*
* 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.
*/
我已经看过了:
this thread, which I can't get working. It does a partial replacement, keeping certain lines of the original text in the new text. I want a complete replacement.
此脚本,同样不适用于我的用例.它将每个文件的开头替换为新的标头,这将导致现有内容(<?php/** */
)附加到新注释中,从而导致解析错误./p>
this script, which similarly doesn't work for my use case. It replaces the very start of each file with a new header, which causes the existing content (<?php /** */
) to be appended to the new comment, thereby causing parse errors.
有人知道我可以如何替换递归多行文件吗?我需要使用sed/awk吗?
Does anybody know how I can do a recursive multi-line file replace? Do I need to use sed/awk?
解决方案:
我只需要执行以下bash脚本即可:
I just need to execute this bash script:
INPUT=../path
find $INPUT -name "*.php" -exec sed -i -e '2,/\*\//d; 1r copyright.txt' {} \;
推荐答案
假定所有文件均以
<?php
/**
如果是这样,您可以使用
If so, you can use
sed '2,/\*\//d; 1r newSig.txt' input.txt
第一个 sed 的问题命令从以下位置删除签名第2行直到签名结束.您可以使用动态范围,但也会删除文件中的其他多行签名.第二条命令读取具有新签名的文件 newSig.txt
,并将其附加在第1行之后.
The first sed command deletes the signature from line 2 til the end of the signature. You could use a dynamic range but it would also delete other multi-line signatures in the file. The second command reads the file newSig.txt
, which has your new signature, and appends it after line 1.
这篇关于递归多行替换:更改版权标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!