我有一个包含以下内容的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>
元素,以便我可以单独执行命令? (即,以便我能够识别哪些文件标签有空白或缺少元素)
答案 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代码,一些有用的阅读:
< <(...)
循环形式的推理。