使用bash迭代XML标记

时间:2017-08-28 14:03:04

标签: xml bash

我有一个包含以下内容的XML文件 -

<File Name="something.txt" >
 <EmailWhenMissing>Customer</EmailWhenMissing>
 <CustomerEmail>somebody@email.com</CustomerEmail>
</File>
<File Name="somethingElse.txt" >
 <EmailWhenMissing>Customer</EmailWhenMissing>
 <CustomerEmail>somebodyelse@email.com</CustomerEmail>
</File>

是否有任何方法或最佳方式遍历每个<File>元素,以便我可以单独执行命令? (即,以便我能够识别哪些文件标签有空白或缺少元素)

2 个答案:

答案 0 :(得分:1)

您可以使用xmlstarlet

for f in $(xmlstarlet select -t -v '//File/@Name' file.xml)
do
   echo $f
done

答案 1 :(得分:1)

如果我正确理解您的目标是验证每个File中的字段,请将以下内容视为相关示例:

#!/bin/bash
#      ^^^^- IMPORTANT: not /bin/sh

sep=$'\v' # pick a character that can't be in your data

while IFS="$sep" read -r Name EmailWhenMissing CustomerEmail; do
  # the line below this provides verbose logging when running with bash -x
  : Name="$Name" EmailWhenMissing="$EmailWhenMissing" CustomerEmail="$CustomerEmail"
  [[ $EmailWhenMissing ]] || { echo "File $Name is missing EmailWhenMissing"; }
  [[ $CustomerEmail ]] || { echo "File $Name is missing CustomerEmail"; }
done < <(xmlstarlet sel -t -m '//File' \
           -v ./@Name -o "$sep" \
           -v ./EmailWhenMissing -o "$sep" \
           -v ./CustomerEmail -n)

给出以下输入文件:

<root>
  <File Name="something.txt">
    <EmailWhenMissing>Customer</EmailWhenMissing>
    <CustomerEmail>somebody@email.com</CustomerEmail>
  </File>
  <File Name="somethingElse.txt">
    <EmailWhenMissing>Customer</EmailWhenMissing>
    <CustomerEmail>somebodyelse@email.com</CustomerEmail>
  </File>
  <File Name="NoEmailWhenMissing.txt">
    <CustomerEmail>somebodyelse@email.com</CustomerEmail>
  </File>
  <File Name="NoCustomerEmail.txt">
    <EmailWhenMissing>Customer</EmailWhenMissing>
  </File>
  <File Name="EmptyFile.txt"/>
</root>

......其输出是:

File NoEmailWhenMissing.txt is missing EmailWhenMissing
File NoCustomerEmail.txt is missing CustomerEmail
File EmptyFile.txt is missing EmailWhenMissing
File EmptyFile.txt is missing CustomerEmail

关于这里的bash代码,一些有用的阅读:

  • BashFAQ #1 - 如何逐行读取文件(数据流,变量)(和/或逐字段?)
  • BashFAQ #24 - 我在一个管道中的循环中设置变量。它们为什么在循环结束后消失?或者,为什么我不能管道数据来读取? - 解释< <(...)循环形式的推理。