2

I am working on a shell script which is getting data from another program and I am using that variable value to read the content from file keep appending to different files after some modification to it:

Below is an example:

readonly file_location=$location
readonly client_id = $id
readonly client_types = $type_of_client

Here $location, $id and $type_of_client value is being passed from another program. Below is an example:

$location will be full path name like this: /home/david/data/12345678 $id will be number: 120 $type_of_client will be space separted word: abc def pqr

Now inside this location /home/david/data/12345678 I have files like this: abc_lop.xml, def_lop.xml and pqr_lop.xml. Meaning _lop.xml is going to be same always so we can hardcode this. We just need to iterate client_types variable and make a file name as shown above and append header and footer into those file and make a new one. So I got this part working fine with below code:

#!/bin/bash

readonly file_location=$location
readonly client_id=$id
readonly client_types=$type_of_client

client_value=`cat "$file_location/client_$client_id.xml"`

for word in $client_types; do
    fn="${word}"_new.xml
    echo "$word"
    echo '<hello_function>' >>"$fn"
    echo '<name>Data</name>' >>"$fn"
    cat "$file_location/${word}_lop.xml" >>"$fn"
    echo '</hello_function>' >>"$fn"
done

Now second thing which I need to do is: I have an another xml file which is client_$client_id.xml. I need to copy my generated _new.xml file into client_$client_id.xml at a particular location. Below is my client_120.xml in which I need to add my generated _new.xml file. I need to replace whole below function with my generated _new.xml file.

<?xml version="1.0"?>

<!-- some data -->

        <function>
            <name>TesterFunction</name>
            <datavariables>
                <name>temp</name>
                <type>string</type>
            </datavariables>
            <blocking>
                <evaluate>val = 1</evaluate>
            </blocking>
        </function>
    </model>
</ModelMetaData>

So if this is my generated _new.xml file: I need to copy this whole file into above file and replace whole TesterFunction with it.

<hello_function>
<name>Data</name>
<Hello version="100">

<!-- some stuff here -->

</Hello>
</hello_function>

So final output will be like this:

<?xml version="1.0"?>

<!-- some data -->

    <hello_function>
    <name>Data</name>
    <Hello version="100">

    <!-- some stuff here -->

    </Hello>
    </hello_function>
    </model>
</ModelMetaData>
1

This should do it:

#!/bin/bash

readonly file_location="$location"
readonly client_id="$id"
readonly client_types="$type_of_client"

## Define the header and footer variables
header='<hello_function>
<name>Data</name>'
footer='</hello_function>'

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 "$file_location/${word}_lop.xml")" "$footer")

    ## Edit the target file and print
    perl -0pe "s#<function>\s*<name>DUMMYPMML.+?</function>#$file#sm"  model_"$client_id".xml
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.