0

I'd like to replace the value of "ThreadGroup.num_threads" which is 2000 with another value i.e. 500. My file name is "test.xml"

here is the xml file that i want to replace 2000 value with 500:

<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="JWT-API Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
    <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
    <boolProp name="LoopController.continue_forever">false</boolProp>
    <stringProp name="LoopController.loops">1</stringProp> </elementProp>
    <stringProp name="ThreadGroup.num_threads">2000</stringProp>
    <stringProp name="ThreadGroup.ramp_time">1</stringProp>
    <boolProp name="ThreadGroup.scheduler">false</boolProp>
    <stringProp name="ThreadGroup.duration"></stringProp>
    <stringProp name="ThreadGroup.delay"></stringProp>
</ThreadGroup>

I have made a script that helps to replace the value but somehow i am hard-coding the values as well as hardcoded the path location. I need a way so that it recognizes the location itself just as i defined in awk command and replaces the value using parameter as defined $Ratio

#!/bin/bash
Ratio="500"
for file in Sample.jmx; do
        awk -F"[<>]" '/ThreadGroup.num_threads/{print $3}' $file > output.json
        sed -i 's:<stringProp name="ThreadGroup.num_threads">2000</stringProp>:<stringProp name="ThreadGroup.num_threads">500</stringProp>:' $file
done
Output=$((`cat < output.json`))
echo $Output
#UpScale = number_of_threads / 1000
UpScale=$((`cat < output.json` / $Ratio))
echo $UpScale
Serg
  • 2,346
  • 3
  • 29
  • 38
  • 4
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Sep 16 '19 at 20:07
  • 1
    So replace `2000` with `500`. What is the difficulty? – John Gordon Sep 16 '19 at 20:11
  • 1
    Whats the question? where is your code? this isnt a code writing service. People will be willing to help if you show you have made some attempt. – Chris Doyle Sep 16 '19 at 20:18
  • I have edited the post and put my work done but somehow i want to rid off all hard-coded stuff that i worked –  Sep 16 '19 at 20:40
  • you can create string using `'s:..text...'$Ratio'...text..:'` – furas Sep 16 '19 at 23:02
  • you could event create this string before `for`-loop - `text='s:..text...'$Ratio'...text..:'` and later use `sed -i $text $file` – furas Sep 16 '19 at 23:04
  • you could also search shorter text `s:ThreadGroup.num_threads">2000<:ThreadGroup.num_threads">'$Ratio'<:'` or maybe even shorter if there is no other `num_threads` - `'s:num_threads">2000<:num_threads">'$Ratio'<:'` – furas Sep 16 '19 at 23:09
  • @furas .. it would be helpful if you can write in a code the way you explain so that i can take as reference –  Sep 17 '19 at 00:07
  • Thanks Furas..it actually worked. but getting this error :```+ sed -i 's:2000:500:' Sample.jmx sed: -e expression #1, char 13: unterminated `s' command ``` –  Sep 17 '19 at 00:30
  • I see spaces between `' '` so it treats strings as separated arguments (first: `'s:2000:500:'`) and it doesn't concatenate them in one string. – furas Sep 17 '19 at 00:58

1 Answers1

0

You can put variable between strings

's:..text...'$Ratio'...text..:'

and it will concatenate all in one string - but there can't be spaces between strings and $Ratio

sed -i 's:<stringProp name="ThreadGroup.num_threads">2000</stringProp>:<stringProp name="ThreadGroup.num_threads">'$Ratio'</stringProp>:' $file

It there is no other num_threads then you could use shorter strings

sed -i 's:num_threads">2000<:num_threads">'$Ratio'<:' $file

Using () (or \( \) on some systems) you can catch text in input string and use it in output string as $1, $2

sed -i 's:(num_threads">)2000(<):$1'$Ratio'$2:' $file

This way you can create shorter string even for longer version

sed -i 's:(<stringProp name="ThreadGroup.num_threads">)2000(</stringProp>):$1'$Ratio'$2:' $file

On Linux Mint I have to use \( \) instead of (),

sed -i 's:\(num_threads">\)2000\(<\):$1'$Ratio'$2:' $file

or I have to add option -E or --regexp-extended

sed -i -E 's:(num_threads">)2000(<):$1'$Ratio'$2:' $file
furas
  • 134,197
  • 12
  • 106
  • 148