在shell脚本中提取与另一个标签处于同一级别的标签
假设我有以下 XML:
<app-deployment>
<name>gr1</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>stage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr2</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>nostage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr3</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
</app-deployment>
如何提取 staging-mode 标记的值,例如名为 gr2 的应用程序部署?
Say i have the following XML:
<app-deployment>
<name>gr1</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>stage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr2</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>nostage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr3</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
</app-deployment>
how can i extract the value of the staging-mode tag, say for the app-deployment named gr2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
许多人(包括我自己)会告诉您,使用基于 reg-ex 的工具解析 xml 是一件愚蠢的事情,您应该使用专为 xml 解析而设计的工具。 Xpath 应该适用于此,并且 xmlstarlet 将是一个可以快速安装和使用的包。
也就是说,假设您的数据总是格式良好,那么制作一个 awk 脚本来搜索 1 个模式、设置一个标志、查找另一个模式、设置一个标志等是相当容易的。当您发现最终目标,清理该行以仅提取您想要的数据。
输出
set -- gr3
和{ cat ... } |
是一个测试工具,您可以将其包装为 shell 脚本 ie并调用,如
This 2nd part is untested 。如果您遇到问题,请告诉我。
我希望这有帮助
Many people (including myself), will tell you that it is a fools errand to parse xml with reg-ex based tools, and that you should use tools designed for xml parsing. Xpath should work for this, and xmlstarlet would be a package you could install and use quickly.
That said, given that you assume your data will always be well formed, it is pretty easy to make an awk script to search for 1 pattern, set a flag, look for another pattern, set a flag, etc. And when you have found the final target, cleanup the line to extract just the data you want.
Output
The
set -- gr3
and{ cat ... } |
are a testing harness, you would wrap this is a shell script i.e.and call like
This 2nd part is untested. Let me know if you have problems with it.
I hope this helps