我可以找到与ant中的表达式匹配的行的行号吗?

发布于 2024-12-11 10:48:13 字数 358 浏览 0 评论 0原文

基本上我想要做的是将一些额外的代码插入到第三方代码生成器的输出文件中,并且我需要插入到几个位置;导入部分、字段和末尾。使用 / 组合并在最后回显到文件似乎相对容易,但问题出在字段部分,因为我不能保证每次类声明之前的行数相同(如果在任何时候,任何人都会向生成注释部分的代码添加任何内容,代码生成器吐出的代码会更长),并且我不想冒险将我的字段插入到某些内容的中间(任何字段都可以有注释),所以基本上我想要匹配我的字段之一知道将永远在那里,找出该行并在其后插入。

简而言之,有什么方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-12-18 10:48:13

您可以使用脚本(请参阅 scriptdef 用法)语言 groovy 例如,或 groovy任务来解决你的问题。
我已经使用 groovy 脚本准备了示例:

<target name="checkfile">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="libs"/>

    <fileset id="files_to_search" dir="${folder.to.search}" includes="**/*.*"/>

    <groovy>
        ant.echo "Search for '${properties.str_to_find}' string"
        project.references.files_to_search.each { fileResource ->
          def file = new File(fileResource.toString())
          def lines = 0
          file.eachLine{
            line ->   
            lines++;
            if (line =~ /^${properties['str_to_find']}$/) println "Matches, line no: " + lines + ", file name: " + file;
          }
        }
    </groovy>
</target>

我的输出:

检查文件:
搜索“ddd”字符串
匹配,行号:5,文件名:d:\55\ouuu.txt

PS:我不是常规程序员,但这个示例正在工作

You can use scripting (see scriptdef usage) language groovy for example, or groovy task to solve your problem.
I've prepared sample using groovy script:

<target name="checkfile">
    <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="libs"/>

    <fileset id="files_to_search" dir="${folder.to.search}" includes="**/*.*"/>

    <groovy>
        ant.echo "Search for '${properties.str_to_find}' string"
        project.references.files_to_search.each { fileResource ->
          def file = new File(fileResource.toString())
          def lines = 0
          file.eachLine{
            line ->   
            lines++;
            if (line =~ /^${properties['str_to_find']}$/) println "Matches, line no: " + lines + ", file name: " + file;
          }
        }
    </groovy>
</target>

My output:

checkfile:
Search for 'ddd' string
Matches, line no: 5, file name: d:\55\ouuu.txt

PS: I'm not groovy programmer, but this sample is working

一身仙ぐ女味 2024-12-18 10:48:13

sed 可以轻松打印行号。 (通过使用“=”)

例如 sed -n '/yourPattern/=' yourfile

将打印所有与 yourPattern 匹配的行号。

sed - n '/yourPattern/{=;q}' yourFile

将打印第一个匹配行的行号,

知道这是否是您需要的。

并且,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.

sed - n '/yourPattern/{=;q}' yourFile

will print the line number of the 1st match line

do know if this is what you need.

AND, sed is platform independent. :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文