gpt4 book ai didi

bash - 使用 xmlstarlet,如何更改元素的值

转载 作者:行者123 更新时间:2023-11-29 09:29:50 25 4
gpt4 key购买 nike

请问如何使用 xmlstarlet 替换 ThreadGroup.num_threads 的所有实例的值?

之前

 <stringProp name="ThreadGroup.num_threads">FOOO BARR</stringProp>

之后

<stringProp name="ThreadGroup.num_threads">99999</stringProp>

这是 XML 片段

  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">778</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">FOOO BARR</stringProp>
<stringProp name="ThreadGroup.ramp_time">66</stringProp>
<longProp name="ThreadGroup.start_time">44</longProp>
<longProp name="ThreadGroup.end_time">55</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration">77</stringProp>
<stringProp name="ThreadGroup.delay">0</stringProp>
</ThreadGroup>

最佳答案

您可以使用 xmlstarlet ed -u 来解决这个问题。指定一个 XPath 表达式,然后使用 -v 设置新值:

xmlstarlet ed -u '/ThreadGroup/stringProp[@name="ThreadGroup.num_threads"]' \
-v 99999 file.xml

(根据您的 XML 文件的其余部分,您可能必须修改 XPath 表达式以使其更具体并考虑更多嵌套)

这是一个完整的例子:

$ cat file.xml
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">778</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">99999</stringProp>
<stringProp name="ThreadGroup.ramp_time">66</stringProp>
<longProp name="ThreadGroup.start_time">44</longProp>
<longProp name="ThreadGroup.end_time">55</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration">77</stringProp>
<stringProp name="ThreadGroup.delay">0</stringProp>
</ThreadGroup>

$ xmlstarlet ed -u '/ThreadGroup/stringProp[@name="ThreadGroup.num_threads"]' \
-v 99999 file.xml
<?xml version="1.0"?>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Blogs" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">778</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">99999</stringProp>
<stringProp name="ThreadGroup.ramp_time">66</stringProp>
<longProp name="ThreadGroup.start_time">44</longProp>
<longProp name="ThreadGroup.end_time">55</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration">77</stringProp>
<stringProp name="ThreadGroup.delay">0</stringProp>
</ThreadGroup>

关于bash - 使用 xmlstarlet,如何更改元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35636357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com