AWK替换XML中的字符串值

时间:2021-08-26 16:52:32

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.

在File Test1.jmx中,我需要将一个字符串值替换为false,其中value = true或0或null。我需要对文件中出现的所有字符串执行此操作。

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:

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>

2 个解决方案

#1


2  

With GNU sed:

使用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>

#2


0  

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.

也可以与cat一起使用或将文件名放在命令的末尾。

#1


2  

With GNU sed:

使用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>

#2


0  

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.

也可以与cat一起使用或将文件名放在命令的末尾。