[英]Shell Script to parse JTL/XML file to know if a specific node has a value
I have a JTL file which I need to parse to know if a node named < failure> has true
, as its value. 我有一个JTL文件,我需要解析它以知道名为<failure>的节点是否为true
,作为其值。
I need to call a script file which should be able to make this decision in a if
condition, based on which I need to do other actions. 我需要调用一个脚本文件,它应该能够在if
条件下做出这个决定,基于此我需要做其他的操作。 How do I implement the parsing of JTL/XML to know if a failure-true
exists or not? 如何实现JTL / XML的解析以了解是否存在failure-true
?
This JTL file has a lot of < failure> nodes in it. 这个JTL文件中有很多<failure>节点。
Edit: I may be constructing all this wrong. 编辑:我可能正在构建所有这些错误。 All am trying is a shell script which will do a specific action if the JTL file it parsed has a node < failure> with value as true
. 我正在尝试的是一个shell脚本,如果它解析的JTL文件的节点<failure>值为true
,它将执行特定的操作。
<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<httpSample t="153" lt="152" ts="1434726402307" s="true" lb="xyz" dt="text" by="14"/>
<httpSample t="169" lt="169" ts="1434726402603" s="true" lb="asdasd" dt="text" by="471">
<assertionResult>
<name>Response Assertion</name>
<failure>false</failure>
<error>false</error>
</assertionResult>
</httpSample>
.
.
.
And so it continues 所以它继续下去
grep
for <failure>true</failure>
in the .jtl file. grep
for .jtl文件中的<failure>true</failure>
。
if grep -q <failure>true</failure> file.jtl; then
echo found
else
echo not found
fi
If you do not care about which node has the value then this can be accomplished with grep. 如果您不关心哪个节点具有该值,那么可以使用grep完成此操作。
if grep -q '<failure>true<\/failure>' jmeter-test.jtl ;then
echo "FAILURE"
fi
The safe way to do this is with an XML-aware tool: 这样做的安全方法是使用支持XML的工具:
failure=$(xmlstarlet sel -t -m '//assertionResult/failure' -v . -n <jmeter-test.jtl)
if [[ $failure = true ]]; then
echo "failed"
else
echo "success"
fi
Unlike the naive grep
approach, this only recognizes failure
if it's in the right place -- under an assertionResult
and not in a comment, not in text taken from a program's output, etc. 与天真的grep
方法不同,这只会识别failure
如果它位于正确的位置 - 在assertionResult
而不是在注释中,而不是从程序输出中获取的文本等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.