如何打印所有 xml 文件中标题属性的所有属性值

发布于 2024-09-11 03:32:18 字数 317 浏览 1 评论 0原文

不知有人知道吗 我可以使用什么命令或 bash 脚本代码 打印出我所有 xml 文件(当前目录中)中标题属性的所有值。

我正在使用 cygwin 并且文件名包含空格。

( 我一直在谷歌上搜索,有很多关于下载其他实用程序的建议。如果我能避免这种情况,那对我来说是有好处的。例如,我安装了 sgrep,然后收到此错误: sh: m4: 命令未找到 系统(“m4 -s”)返回非零退出状态(32512)。 预处理器返回空文件 )

如果有一个 Xpath 程序可以免费下载到 Windows 并像独立搜索程序一样使用,那就太好了 =)

提前感谢您的帮助 /T

I wonder if anybode knows
what command or bash-script-code I can use
to print out all the values of the title attributes in all my xml files (in current directory).

I'm using cygwin and have file names containing white spaces.

(
I've been googling around and there are a lot of suggestions on downloading other utilities. If I can avoid that it would be good for me. For example I installed sgrep and then got this error: sh: m4: command not found
system("m4 -s") returned non zero exit status (32512).
Preprocessor returned empty file
)

If there is an Xpath program that is free to download to windows and use like a stand alone search program, that would be great too =)

Thanks in advance for helping out
/ T

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

回梦 2024-09-18 03:32:18

如果标签和标题属性都在同一行,但标签的不同实例之间存在换行,则以下内容可能适合您。例如

<mytag someAttr="blah" Title="The Title goes here" ...

,然后您可以执行类似以下操作,以查找包含 Title 属性的感兴趣标签:

grep -ro '<mytag[ \t].*Title="[^"]*"' /path/to/directory/to/search

或者,您应该能够使用 findxargs

find /your/search/path -iname '*.xml' -print0 | \
    xargs -0 -r grep -ro '<mytag[ \t].*Title="[^"]*"'

现在您知道您拥有正确的标签及其相应的 Title 属性,您只需要 Title 属性,因此您可以使用 grep-o 选项仅输出与正则表达式匹配的数据,然后剪切以提取标题的值:

grep -ro '<mytag[ \t].*Title="\([^"]*\)"' /path/to/directory/to/search | \
    grep -o 'Title="[^"]*"' | cut -f2 -d'"'

If the tag and the title attribute are all on the same line, but there are line feeds between the different instances of your tag, the following could work for you. For example

<mytag someAttr="blah" Title="The Title goes here" ...

Then you could do something like the following in order to find the tags of interest that contain a Title attribute:

grep -ro '<mytag[ \t].*Title="[^"]*"' /path/to/directory/to/search

Alternatively, you should be able to use find and xargs:

find /your/search/path -iname '*.xml' -print0 | \
    xargs -0 -r grep -ro '<mytag[ \t].*Title="[^"]*"'

Now that you know you have the correct tag and its corresponding Title attribute, you just want the Title attribute, so you can use grep's -o option to output only the data matching the regular expression followed by cut to extract the value of the Title:

grep -ro '<mytag[ \t].*Title="\([^"]*\)"' /path/to/directory/to/search | \
    grep -o 'Title="[^"]*"' | cut -f2 -d'"'
南薇 2024-09-18 03:32:18

你需要在 cigwin 中安装预处理器“m4”,这将使你的 sgrep 工作

我遇到了同样的问题,安装“m4”解决了我在 cygwin 中的问题

you need to install the preprocessor "m4" in cigwin that will make your sgrep work

I faced same problem installing "m4" resolved my issue in cygwin

情未る 2024-09-18 03:32:18

你安装了 xml_grep 吗?它是免费的,并且是我安装的 centOS 的标准配置。它可以采用 xpath 表达式并打印结果。

Do you have xml_grep installed? It is free and came standard on my install of centOS here. It can take an xpath expression and print the results.

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