在具有多个节点和属性值的xml文件中。我需要从每个节点中提取“ lb”属性。
<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample s="sample s1" lb="sample lb1"</httpSample>
<httpSample s="sample s2" lb="sample lb2"</httpSample>
<httpSample s="sample s3" lb="sample lb3"</httpSample>
</testResults>
但是用我的bash脚本我无法做到。 脚本是:
#!/bin/bash
httpSample=($(grep -oP '(?<=httpSample>)[^<]+' "location_of_XML.xml"))
for i in ${!httpSample[*]}
do
echo "$i" "${httpSample[$i]}"
# instead of echo use the values to send httpSample, etc
done
$SHELL
此脚本正在读取具有单个node值的简单xml 喜欢
<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample>string1</httpSample>
<httpSample>string2</httpSample>
<httpSample>string3</httpSample>
</testResults>
请让我知道我做错了。
答案 0 :(得分:0)
您可以尝试以下方法,即使它肯定不是最有效的方法,它应该为包含“ httpSample”文本的每条xml行提供lb值:
while read line
do
[[ "$line" = *"httpSample"* ]] && {
lb=$(echo "$line" | grep 'lb=' | cut -d'=' -f3 | cut -d'<' -f1 | tr -d '"')
# now do here whatever you want with "$lb" variable
}
done < xml_file