简体   繁体   English

如何用文件中的sed替换多行注释

[英]How to replace multiline comments with sed in files

I have all sort of files php html xml where the copyright notice is included either in top or after 2-3 lines from the top like this 我有各种各样的文件php html xml ,其中copyright声明包含在top或从顶部起2-3行之后,像这样

/**
 * Copyright (c)  2014
 * All rights reserved.
 * bla bla many lines like this
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

Now i want to remove all that text from all the files. 现在我想从所有文件中删除所有文本。

How can i remove with sed. 如何使用sed删除。

I just know basic sed but i don't know how to deal with multilines 我只知道基本的sed但我不知道如何处理多行

$ cat file
1
2
/**
 * Copyright (c)  2014
 * All rights reserved.
 * bla bla many lines like this
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
3
4
/* and here is another comment
 * you presumably want to keep
 */
5
6

$ awk '/^\/\*/{c++} c!=1; /^ \*\//{c++}' file
1
2
3
4
/* and here is another comment
 * you presumably want to keep
 */
5
6

In you can delete a range starting with /* and ending with */ like this: 您可以删除以/*开头并以*/结尾的范围,如下所示:

sed '/\/\*/,/\*\//d inputFile

tends to be a bit hard to read sometimes, so I'll give a more clear example: If you want to delete everything in a range starting with abc and ending with xyz , you'd do 有时会很难读,所以我举一个更清晰的例子:如果要删除以abc开头和以xyz结尾的范围内的所有内容,则可以

sed '/abc/,/def/d' inputFile

The case with /* and */ is the same, but / and * have to be escaped. /**/的情况相同,但是/*必须转义。

A similar approach should work for the and files. 类似的方法应该适用于文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM