I have an xml file (client_23.xml)
in which I need to add few lines at a particular section basis on what is there in other xml file (abc_lop.xml)
.
Here is my abc_lop.xml
file in which you see I have lot of ClientField
lines with name
, pptype
and dataType
in it.
<Hello version="100">
<DataHolder numberOfFields="67">
<ClientField name="target" pptype="aligning" dataType="string">
<Value value="panel_3646"/>
<Value value="panel_3653"/>
</ClientField>
<ClientField name="category_3652_0_count" pptype="symetrical" dataType="double"/>
<ClientField name="category_3652_2_count" pptype="symetrical" dataType="double"/>
<ClientField name="category_3646_0_count" pptype="symetrical" dataType="double"/>
<ClientField name="pme.cdert" pptype="symetrical" dataType="double"/>
<ClientField name="pme.age" pptype="symetrical" dataType="double"/>
<ClientField name="category_3648_1_count" pptype="symetrical" dataType="double"/>
<ClientField name="pme.number" pptype="symetrical" dataType="double"/>
<ClientField name="pme.gender" pptype="aligning" dataType="string">
<Value value=""/>
<Value value="F "/>
<Value value="NA"/>
</ClientField>
<ClientField name="pme.status" pptype="aligning" dataType="string">
<Value value=""/>
<Value value="A"/>
<Value value="S"/>
<Value value="NA"/>
</ClientField>
<ClientField name="pme.selling_id" pptype="aligning" dataType="string">
<Value value="c0"/>
<Value value="c1"/>
<Value value="NA"/>
</ClientField>
</DataHolder>
</Hello>
I need to read this file and extract name
from those ClientField
lines and if its pptype
is not aligning
then I need to construct this line for each name: Below is an example for two names, only first value is different and apart from that other two will always be same.
<eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
<eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>
Now if its pptype
is aligning
, then construct line like this:
<eval>upsert("target", "NA", $calty_strings)</eval>
<eval>upsert("pme.gender", "NA", $calty_strings)</eval>
And all these changes I have to make in client_23.xml
file and then make a new file with it so my new file will look like this as an example: I will have a function with name data_values
in which I need to add above stuff in the <block>
tag as shown below.
<function>
<name>data_values</name>
<variables>
<variable>
<name>temp</name>
<type>double</type>
</variable>
</variables>
<block>
<eval>temp = 1</eval>
<eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
<eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>
<eval>upsert("target", "NA", $calty_strings)</eval>
<eval>upsert("pme.gender", "NA", $calty_strings)</eval>
</block>
</function>
This is what I have currently in client_23.xml
file so after adding, it will look like above:
<function>
<name>data_values</name>
<variables>
<variable>
<name>temp</name>
<type>double</type>
</variable>
</variables>
<block>
<eval>temp = 1</eval>
</block>
</function>
I have a very simple shell script which terdon helped me earlier in which we are using perl script as shown below. I adding header and footer in the abc_lop.xml
file and then storing it in file
variable and then using that file value to put at a particular section in client_23.xml
file but I am not sure how to do above things.
SCRIPT:-
for word in $client_types
do
## Concatenate the header, the contents of the target file and the
## footer into the variable $file.
file=$(printf '%s\n%s\n%s' "$header" "$(cat "$path/${word}_lop.xml")" "$footer")
## Edit the target file and print
perl -0pe "s#<eval>planting_model = 0</eval>#<eval>planting_model = 1</eval> s#<trestra-config>.* </trestra-config>##sm; s#<function>\s*<name>DUMMY_FUNCTION.+?</function>#$file#sm" client_"$client_id".xml > "$word"_new_file.xml
done
Here client_types
will be like this: abc def pqr
and $client_id
is 23.
Now I need to add above functionality which I am not sure how can I do that easily?