在shell脚本中提取与另一个标签处于同一级别的标签

发布于 2025-01-05 09:18:01 字数 1159 浏览 1 评论 0原文

假设我有以下 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 技术交流群。

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

发布评论

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

评论(1

梦幻的心爱 2025-01-12 09:18:01

许多人(包括我自己)会告诉您,使用基于 reg-ex 的工具解析 xml 是一件愚蠢的事情,您应该使用专为 xml 解析而设计的工具。 Xpath 应该适用于此,并且 xmlstarlet 将是一个可以快速安装和使用的包。

也就是说,假设您的数据总是格式良好,那么制作一个 awk 脚本来搜索 1 个模式、设置一个标志、查找另一个模式、设置一个标志等是相当容易的。当您发现最终目标,清理该行以仅提取您想要的数据。

set -- gr2
{ cat - <<-EOS
<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>
EOS
} | awk '
    /[<]app-deployment/{a=1}
    a && /[<]name[>]'"$1"'/{n=1}
    a && n && /[<]staging-mode[>]/{
      sub(/[<]staging-mode[>]/,"", $0)
      sub(/[<]\/staging-mode[>]/,"",$0)
      print $0
      exit
    }
    #dbg { print "a=" a "\tn=" n }
  '

输出

       nostage

set -- gr3{ cat ... } | 是一个测试工具,您可以将其包装为 shell 脚本 ie

 cat printXMLarg.bash
 #!/bin/bash
   targ=$1; shift
   awk '
    /[<]app-deployment/{a=1}
    a && /[<]name[>]'"${targ}"'/{n=1}
    a && n && /[<]staging-mode[>]/{
      sub(/[<]staging-mode[>]/,"", $0)
      sub(/[<]\/staging-mode[>]/,"",$0)
      print $0
      exit
    }
    #dbg { print "a=" a "\tn=" n }
  ' "${@}"

并调用,如

  printXMLarg.bash gr3 *.xml

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.

set -- gr2
{ cat - <<-EOS
<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>
EOS
} | awk '
    /[<]app-deployment/{a=1}
    a && /[<]name[>]'"$1"'/{n=1}
    a && n && /[<]staging-mode[>]/{
      sub(/[<]staging-mode[>]/,"", $0)
      sub(/[<]\/staging-mode[>]/,"",$0)
      print $0
      exit
    }
    #dbg { print "a=" a "\tn=" n }
  '

Output

       nostage

The set -- gr3 and { cat ... } | are a testing harness, you would wrap this is a shell script i.e.

 cat printXMLarg.bash
 #!/bin/bash
   targ=$1; shift
   awk '
    /[<]app-deployment/{a=1}
    a && /[<]name[>]'"${targ}"'/{n=1}
    a && n && /[<]staging-mode[>]/{
      sub(/[<]staging-mode[>]/,"", $0)
      sub(/[<]\/staging-mode[>]/,"",$0)
      print $0
      exit
    }
    #dbg { print "a=" a "\tn=" n }
  ' "${@}"

and call like

  printXMLarg.bash gr3 *.xml

This 2nd part is untested. Let me know if you have problems with it.

I hope this helps

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