Reputation: 395
I'm trying to replace the value 10 with the value 5 where testname="TG1"
Change This:
<stringProp name="ThreadGroup.num_threads">10</stringProp>
To This:
<stringProp name="ThreadGroup.num_threads">5</stringProp>
Snippet Example
Before:
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG1" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">10</stringProp>
</ThreadGroup>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG2" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">50</stringProp>
</ThreadGroup>
After:
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG1" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">5</stringProp>
</ThreadGroup>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG2" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">50</stringProp>
</ThreadGroup>
Upvotes: 1
Views: 73
Reputation: 1517
awk '{sub(/10<\/stringProp>/,"5</stringProp>")}1' file
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG1" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">5</stringProp>
</ThreadGroup>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="TG2" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">50</stringProp>
</ThreadGroup>
Upvotes: 0
Reputation: 246774
Assuming you actually have valid XML, this is really an xpath problem:
xmlstarlet ed \
--update '//ThreadGroup[@testname="TG1"]//stringProp[@name="ThreadGroup.num_threads"]' \
--value 5 \
file.xml
To save the file in place, change ed
to ed --inplace
Upvotes: 2
Reputation: 3055
Try this awk-command:
awk '$0 ~ "testclass=\"ThreadGroup\"" && $0 ~ "testname=\"TG1\""{replace=1}
$0 ~ "testclass=\"ThreadGroup\"" && $0 !~ "testname=\"TG1\""{replace=0}
replace{gsub("name=\"ThreadGroup.num_threads\">10</stringProp>",
"name=\"ThreadGroup.num_threads\">5</stringProp>",$0)}1' in.txt
It will replace all name="ThreadGroup.num_threads">10</stringProp>
by name="ThreadGroup.num_threads">5</stringProp>
if they are within <... testclass="ThreadGroup" testname="TG1" ...>
Upvotes: 0