使用 Bash (sed?) 删除包含特定文本的多行/* ... */样式注释(正则表达式) [英] Use Bash (sed?) to remove a multiline /* ... */ style comment containing specific text (regex)

查看:12
本文介绍了使用 Bash (sed?) 删除包含特定文本的多行/* ... */样式注释(正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种 bash 解决方案来删除包含现在被认为是 Java 类中/* ... */注释中多余的信息的代码片段.

I am looking for a bash solution to remove a code snippet that contains information that is now considered superfluous in /* ... */ comments in java classes.

特别是,以前当我们检入我们的代码时,它会自动在类的顶部附近添加一个检入历史的副本:

Specifically, previously when we checked in our code, it would automatically add near the top of the class a copy of the check in history:

/* Copyright statement */
/*
 * $Log:
 *  3    Project1 02/02/2012 19:05:59    Bob Geldof
 *       Comment 3
 *  2    Project1 01/02/2012 15:25:22    Sir Lancelot
 *       CR-12345
 *  1    Project1 22/10/2011 12:15:31    Thomas the Tank Engine
 *       First implementation of that cool thing
 * $
 */
package com.thing.place.blah
...
...
...

我们现在使用不同的工具来更好地显示这些信息,并且由于时间戳、评论等的差异,在重用代码的项目之间使用代码比较是一种痛苦.

We now use different tools that better display this information, and using code comparisons between projects that reuse code is a pain due to difference in timestamps, comments etc.

目前,如果我们处理一个文件,我们会手动删除注释语句和生成该语句的 $Log: 关键字,但是我想做的是编写一个脚本来删除所有 java 文件中的它们.我可以使用 bash 脚本在每个 java 文件上运行它,但是在正则表达式中有一些技巧,我不知道如何实现.

Currently, if we work on a file, we manually remove the comment statement and the $Log: keyword that generates the statement, however what I would like to do is write a script to remove them accross all java files. I'm fine with the bash script to run it on every java file, however there are a couple of tricks here in the regex that I'm not aware on how to implement.

所以我想工作流程类似于

So I guess the workflow is something like

  1. 查找文件中的第一个 "$Log:"
  2. 在此文本之前寻找最接近的"/*"
  3. 在此文本之后寻找最接近的"*/".
  4. 删除中间的所有文本,包括 "/*""*/"
  5. 在不删除之前/之后的任何注释的情况下执行此操作(大多数文件将具有上述格式类似的版权声明,可能在同一行中包含结束注释和开始注释标记,例如 " ... 版权文本的结尾*//* $Log: ..."

为了做到这一点,我更喜欢与 sed 兼容的正则表达式参数,因为这是我熟悉使用的,尽管我会接受你提供的任何东西!此外,如果有必要进行多次传递,例如在开始标记和 $Log: 之间删除"、在结束标记和 $Log: 之间删除"、删除 $Log:",那就没问题了.这是一个一次性过程,因此速度远不如准确性重要.

In order to do this, I would prefer regex arguments compatible with sed as this is what I'm familiar with using, although I'll take whatever you have to offer! Also if it makes sense to make multiple passes, such as "remove between start tag and $Log:", "remove between end tag and $Log:", "remove $Log:" that would be fine. This is a one time process so speed is much less important than accuracy.

非常感谢您的任何建议.米奇.

Many thanks for any suggestions. Mitch.

推荐答案

以下内容可能适合您:

sed '//*/!b;:a;/*//!{$!{N;ba}};{/$Log:/d}' filename

这篇关于使用 Bash (sed?) 删除包含特定文本的多行/* ... */样式注释(正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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