1

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>

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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" ...>

Comments

0
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.