我可以找到与ant中的表达式匹配的行的行号吗?
基本上我想要做的是将一些额外的代码插入到第三方代码生成器的输出文件中,并且我需要插入到几个位置;导入部分、字段和末尾。使用 / 组合并在最后回显到文件似乎相对容易,但问题出在字段部分,因为我不能保证每次类声明之前的行数相同(如果在任何时候,任何人都会向生成注释部分的代码添加任何内容,代码生成器吐出的代码会更长),并且我不想冒险将我的字段插入到某些内容的中间(任何字段都可以有注释),所以基本上我想要匹配我的字段之一知道将永远在那里,找出该行并在其后插入。
简而言之,有什么方法可以使用 ant 来查找与正则表达式或字符串文字匹配的行号吗? (如果这能让事情变得更容易的话,只会有一个)。哦,解决方案必须是跨平台兼容的,因为还有其他人在 Windows/Linux/Mac 上访问代码。
Basically what I want to do is insert some extra code in to the output files from a 3rd party code generator and I need to insert in a few locations; the import section, fields, and at the end. It seems relatively easy to do using with / combinations and echoing to a file at the end, but the problem is in the field section as I can't guarantee it'll be the same number of lines before the Class declaration each time (if at any point anyone adds anything to the code that's generated the comment section the code generator spits out will be longer) and I don't want to risk inserting my field in the middle of something (any field could have annotations), so basically I'd like to match one of the fields that I know will always be there, find out what line that is on and insert after that.
So in short, is there any way I can use ant to find the line number matching a regular expression or string literal? (There will only be one if that makes things easier). Oh and the solution has to be cross-platform compatible as there are others accessing the code on Windows/Linux/Mac.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用脚本(请参阅 scriptdef 用法)语言
groovy
例如,或groovy
任务来解决你的问题。我已经使用 groovy 脚本准备了示例:
我的输出:
PS:我不是常规程序员,但这个示例正在工作
You can use scripting (see scriptdef usage) language
groovy
for example, orgroovy
task to solve your problem.I've prepared sample using
groovy
script:My output:
PS: I'm not groovy programmer, but this sample is working
sed 可以轻松打印行号。 (通过使用“=”)
例如
sed -n '/yourPattern/=' yourfile
将打印所有与 yourPattern 匹配的行号。
将打印第一个匹配行的行号,
知道这是否是您需要的。
并且,sed 是平台无关的。 :D
sed can print line number easily. (by using "=")
e.g.
sed -n '/yourPattern/=' yourfile
will print all all line numbers, that the lines match yourPattern.
will print the line number of the 1st match line
do know if this is what you need.
AND, sed is platform independent. :D