user1654528
user1654528

Reputation: 395

AWK replace a string value in XML

In the File Test1.jmx I need to replace a string value to be false where the value = true or 0 or null. I need to do this for all occurrences of the string in the file.

before:
<boolProp name="ThreadGroup.scheduler">true</boolProp>
after:
<boolProp name="ThreadGroup.scheduler">false</boolProp>

Here's my erroneous code:

overrides_scheduler="BLAAA FOO MOO"
overRides=$(awk -v newValue="$overrides_scheduler" '$0 ~ /boolProp name="ThreadGroup.scheduler"/ {a=$0; sub(/<boolProp name="ThreadGroup.scheduler">[a-zA-Z0-9]/,"<boolProp name=\"ThreadGroup.scheduler\">"newValue,a); print a; next;}{}1' "Test1.jmx");
echo  "$overRides" > "Test1.jmx"

Test1.jmx:

<boolProp name="ThreadGroup.scheduler">true</boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<boolProp name="ThreadGroup.scheduler"></boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">0</boolProp>

Upvotes: 0

Views: 591

Answers (2)

Yaron
Yaron

Reputation: 1242

I managed to do the following:

echo '<boolProp name="ThreadGroup.scheduler">true</boolProp>' | awk -F'\">|</' '/ThreadedGroup.scheduler/ {sub(/true/,"false");print}'

Can also be used with cat or putting the filename at the end of the command.

Upvotes: 0

Cyrus
Cyrus

Reputation: 88646

With GNU sed:

sed -r 's|(<boolProp name="ThreadGroup.scheduler">)(true\|0\|null)(</boolProp>)|\1false\3|' Test1.jmx

Output:

<boolProp name="ThreadGroup.scheduler">false</boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<boolProp name="ThreadGroup.scheduler"></boolProp>
<longProp name="ThreadGroup.end_time">1363247040000</longProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>

Upvotes: 2

Related Questions