在linux控制台中从XML中删除标记内容

时间:2017-03-01 14:43:23

标签: xml bash jmeter xmlstarlet

我在操作Jmeter jtl报告文件时遇到了一项具有挑战性的任务。基本思想是从jtl中删除XML格式的最终​​报告不需要的不需要的记录(beanshell采样器等)。因此,我试图找到解决方案来删除包含 ctrlSampler 字符串的示例标记。

尝试使用此帖子Delete nodes in xml if they contain certain text using sed中的解决方案,但没有成功

gawk -v IGNORECASE=1 '{ p = 1 } /<sample/{ t = $0; while (getline > 0) { t = t ORS $0; if (ctrlSampler) p = 0; if (/<\/sample>/) break } $0 = t } p' sourceJtl > targetJtl

以下是样本JTL

<?xml version="1.0" encoding="UTF-8"?>
<testResults version="1.2">
<sample t="15" lt="0" ts="1488375720156" s="true" lb="ctrlSampler - fWrite - reset noExecFailures variable" rc="200" rm="OK" tn="setUp prereqs 1-1" dt="text" by="0">
  <responseHeader class="java.lang.String"></responseHeader>
  <requestHeader class="java.lang.String"></requestHeader>
  <responseData class="java.lang.String"></responseData>
  <samplerData class="java.lang.String">        boolean  noExecFailures;
        vars.put(&quot;noExecFailures&quot;,&quot;true&quot;);

     f = new FileOutputStream(&quot;/opt/jmeter_test/execResult.property&quot;);
     p = new PrintStream(f);
     this.interpreter.setOut(p);
     print(&quot;noExecFailures=&quot; + vars.get(&quot;noExecFailures&quot;));
     f.close();
   </samplerData>
</sample>
<sample t="21" lt="0" ts="1488375720648" s="true" lb="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1::Notification::GetGlobalUrlsRequest" rc="200" rm="" tn="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1" dt="" by="598">
  <assertionResult>
    <name>Response Assertion - Verify success response</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
  <assertionResult>
    <name>Response Assertion - Verify response specific params</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
  <responseHeader class="java.lang.String"></responseHeader>
  <requestHeader class="java.lang.String"></requestHeader>
  <responseData class="java.lang.String"/>
  <samplerData class="java.lang.String">{
    &quot;headers&quot;: {
        &quot;messageId&quot;: &quot;5e5beb41-9cf0-4ef2-b678-7f47323b1b27&quot;
    },
    &quot;payload&quot;: {
        &quot;qualifier&quot;: &quot;com.playtech.ums.clientapi.notification.GetGlobalUrlsRequest&quot;,
        &quot;brandName&quot;: &quot;playtech32001&quot;,
        &quot;context&quot;: {
            &quot;sessionContext&quot;: {
                &quot;clientType&quot;: &quot;casino&quot;,
                &quot;clientPlatform&quot;: &quot;download&quot;
            },
            &quot;clientVersion&quot;: &quot;1&quot;,
            &quot;languageCode&quot;: &quot;EN&quot;
        },
        &quot;urlTypeList&quot;: [
            &quot;cashier&quot;,
            &quot;cashier_ext&quot;,
            &quot;myAccount&quot;
        ]
    }
}</samplerData>
</sample>
<sample t="7" lt="0" ts="1488375720706" s="false" lb="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1::Notification::GetGlobalUrlsRequest" rc="200" rm="" tn="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1" dt="" by="598">
  <assertionResult>
    <name>Response Assertion - Verify success response</name>
    <failure>false</failure>
    <error>false</error>
  </assertionResult>
  <assertionResult>
    <name>Response Assertion - Verify response specific params</name>
    <failure>true</failure>
    <error>false</error>
    <failureMessage>Test failed: text expected to contain /INJECT FAILIRE/</failureMessage>
  </assertionResult>
  <responseHeader class="java.lang.String"></responseHeader>
  <requestHeader class="java.lang.String"></requestHeader>
  <responseData class="java.lang.String"/>
  <samplerData class="java.lang.String">{
    &quot;headers&quot;: {
        &quot;messageId&quot;: &quot;edcf182a-82a4-4649-9e4b-b586e95fa26c&quot;
    },
    &quot;payload&quot;: {
        &quot;qualifier&quot;: &quot;com.playtech.ums.clientapi.notification.GetGlobalUrlsRequest&quot;,
        &quot;brandName&quot;: &quot;playtech32001&quot;,
        &quot;context&quot;: {
            &quot;sessionContext&quot;: {
                &quot;clientType&quot;: &quot;casino&quot;,
                &quot;clientPlatform&quot;: &quot;download&quot;
            },
            &quot;clientVersion&quot;: &quot;1&quot;,
            &quot;languageCode&quot;: &quot;EN&quot;
        },
        &quot;urlTypeList&quot;: [
            &quot;cashier&quot;,
            &quot;cashier_ext&quot;,
            &quot;myAccount&quot;
        ]
    }
}</samplerData>
</sample>

</testResults>

需要的结果

 <?xml version="1.0" encoding="UTF-8"?>
    <testResults version="1.2">
    <sample t="21" lt="0" ts="1488375720648" s="true" lb="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1::Notification::GetGlobalUrlsRequest" rc="200" rm="" tn="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1" dt="" by="598">
      <assertionResult>
        <name>Response Assertion - Verify success response</name>
        <failure>false</failure>
        <error>false</error>
      </assertionResult>
      <assertionResult>
        <name>Response Assertion - Verify response specific params</name>
        <failure>false</failure>
        <error>false</error>
      </assertionResult>
      <responseHeader class="java.lang.String"></responseHeader>
      <requestHeader class="java.lang.String"></requestHeader>
      <responseData class="java.lang.String"/>
      <samplerData class="java.lang.String">{
        &quot;headers&quot;: {
            &quot;messageId&quot;: &quot;5e5beb41-9cf0-4ef2-b678-7f47323b1b27&quot;
        },
        &quot;payload&quot;: {
            &quot;qualifier&quot;: &quot;com.playtech.ums.clientapi.notification.GetGlobalUrlsRequest&quot;,
            &quot;brandName&quot;: &quot;playtech32001&quot;,
            &quot;context&quot;: {
                &quot;sessionContext&quot;: {
                    &quot;clientType&quot;: &quot;casino&quot;,
                    &quot;clientPlatform&quot;: &quot;download&quot;
                },
                &quot;clientVersion&quot;: &quot;1&quot;,
                &quot;languageCode&quot;: &quot;EN&quot;
            },
            &quot;urlTypeList&quot;: [
                &quot;cashier&quot;,
                &quot;cashier_ext&quot;,
                &quot;myAccount&quot;
            ]
        }
    }</samplerData>
    </sample>
    <sample t="7" lt="0" ts="1488375720706" s="false" lb="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1::Notification::GetGlobalUrlsRequest" rc="200" rm="" tn="[TP7][IMS][TEST PLAYER REQUESTS SUCCESS FLOW] 2-1" dt="" by="598">
      <assertionResult>
        <name>Response Assertion - Verify success response</name>
        <failure>false</failure>
        <error>false</error>
      </assertionResult>
      <assertionResult>
        <name>Response Assertion - Verify response specific params</name>
        <failure>true</failure>
        <error>false</error>
        <failureMessage>Test failed: text expected to contain /INJECT FAILIRE/</failureMessage>
      </assertionResult>
      <responseHeader class="java.lang.String"></responseHeader>
      <requestHeader class="java.lang.String"></requestHeader>
      <responseData class="java.lang.String"/>
      <samplerData class="java.lang.String">{
        &quot;headers&quot;: {
            &quot;messageId&quot;: &quot;edcf182a-82a4-4649-9e4b-b586e95fa26c&quot;
        },
        &quot;payload&quot;: {
            &quot;qualifier&quot;: &quot;com.playtech.ums.clientapi.notification.GetGlobalUrlsRequest&quot;,
            &quot;brandName&quot;: &quot;playtech32001&quot;,
            &quot;context&quot;: {
                &quot;sessionContext&quot;: {
                    &quot;clientType&quot;: &quot;casino&quot;,
                    &quot;clientPlatform&quot;: &quot;download&quot;
                },
                &quot;clientVersion&quot;: &quot;1&quot;,
                &quot;languageCode&quot;: &quot;EN&quot;
            },
            &quot;urlTypeList&quot;: [
                &quot;cashier&quot;,
                &quot;cashier_ext&quot;,
                &quot;myAccount&quot;
            ]
        }
    }</samplerData>
    </sample>

    </testResults>

由于

2 个答案:

答案 0 :(得分:0)

试试这个 -

测试 -

sed -n '/sample/{/ctrlSampler/p}' f > f1

cat f1 ##### Here we have only one record. 
<sample t="15" lt="0" ts="1488375720156" s="true" lb="ctrlSampler - fWrite - reset noExecFailures variable" rc="200" rm="OK" tn="setUp prereqs 1-1" dt="text" by="0">

删除包含sample和ctrlSampler的行 -

sed '/sample/{/ctrlSampler/d}' f > f2

diff f f2
3d2
< <sample t="15" lt="0" ts="1488375720156" s="true" lb="ctrlSampler - fWrite - reset noExecFailures variable" rc="200" rm="OK" tn="setUp prereqs 1-1" dt="text" by="0">

答案 1 :(得分:0)

出于兴趣,为什么不能只使用Filter Results Tool,它有--exclude-label-regex选项,因此您可以过滤不需要的采样器。

可以使用JMeter Plugins Managermanually

安装该插件

JMeter Plugins Manager Filter Results Tool

成功安装后,您会在JMeter&#34; bin&#34;中找到FilterResults.sh。夹