简体   繁体   English

用另一个文本替换多行Java注释的Sed

[英]Sed to replace multiline Java comment with another text

I have some java code: 我有一些java代码:

/**
 * Some comment.
 */
void someFunction();

/**
 * Some comment.
 */
void anotherFunction();

How to write a single line sed to convert the above into 如何编写单行sed将上述内容转换为

@Override
void someFunction();

@Override
void anotherFunction();

The best I could do is to just delete the comments with 我能做的最好的事情就是删除评论

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

and manually insert @Override . 并手动插入@Override

EDIT: The method signature is not always void(). 编辑:方法签名并不总是void()。

If you can also work with awk , this can make it: 如果你也可以使用awk ,这可以做到:

$ awk '/\/\*/ {p=0; next} /\*\// {p=1; print "@Override"; next}p' file
@Override
void someFunction();

@Override
void anotherFunction();

@Override
char Bla();

For a given file 对于给定的文件

/**
 * Some comment.
 */
void someFunction();

/**
 * Some comment.
 */
void anotherFunction();

/**
 * Some comment.
 */
char Bla();

You can use sed to insert a new line after '*/' 您可以使用sed在'* /'之后插入一个新行

sed '/\*\// a @Override' file

and then run your command - 然后运行你的命令 -

sed '/\/\*\*/,/\*\// d' file

This way you can add @override for each method even if it doesn't start with void. 这样,即使不以void开头,也可以为每个方法添加@override。 But There should be comment section for each method. 但每个方法都应该有评论部分。

Here is how you can do this with a single pass of sed: 以下是单步传递sed的方法:

sed '/\/\*\*/,/\*\// {s/.*\*\/.*/@Override/p; d}'

The commands within the braces will only run for the lines that are a part of the comment, and the s/.*\\*\\/.*/@Override/p will only match the end of the comment and then print @Override as desired. 大括号内的命令将只对于那些注释的一部分的线路运行,并且s/.*\\*\\/.*/@Override/p将只匹配注释的末尾,然后打印@Override作为期望。

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

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