Reputation: 405
How can I adjust the following code to replace every occurrence of the value set for the element, ThreadGroup.num_threads.
Here is the code I'm trying to make work.
awk ' BEGIN { FS = "[<|>]" }
{
if ($2 == "stringProp name=\"ThreadGroup.num_threads\"") {
$newValue
}
print
}
' Test1.jmx
Here is the XML snippet I'm parsing.
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.num_threads">3</stringProp>
</ThreadGroup>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group2" enabled="true">
<stringProp name="ThreadGroup.num_threads">3</stringProp>
</ThreadGroup>
newValue=999999
Upvotes: 0
Views: 75
Reputation: 67507
Perhaps this is easier with sed
$ sed -r 's/("ThreadGroup.num_threads">)([0-9]+)</\19999</g'
Upvotes: 0
Reputation: 3065
In your code, the variable newValue
is never defined. Moreover, you do not need $
in front of your own variables.
Here is my suggestion:
awk '$0 ~ /stringProp name="ThreadGroup.num_threads"/
{sub(/<stringProp name="ThreadGroup.num_threads">[0-9]+/,
"<stringProp name=\"ThreadGroup.num_threads\">999999",
$0)}
{}1' inputFile
1st line: I check whether the current line contains the text stringProp name="ThreadGroup.num_threads"
2nd-4th line: If yes, I substitute the string <stringProp name="ThreadGroup.num_threads">
if it is followed by one or more numbers by the same string followed by 999999
.
5th line: Finally, I output each line.
Of course you can define a variable:
awk 'BEGIN{newValue=999999}
$0 ~ /stringProp name="ThreadGroup.num_threads"/
{sub(/<stringProp name="ThreadGroup.num_threads">[0-9]+/,
"<stringProp name=\"ThreadGroup.num_threads\">"newValue,
$0)}
{}1' inputFile
The output is:
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.num_threads">999999</stringProp>
</ThreadGroup>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group2" enabled="true">
<stringProp name="ThreadGroup.num_threads">999999</stringProp>
</ThreadGroup>
Upvotes: 1