Search a string and append text after the string

Hi,

I have a file like this...

<o t="Batch" id="8410" p="/" g="32">
<a n="name">
<v s="DBBA1MM"/>
</a>
<a n="owner">
<v r="/[AdminUser]Administrator"/>
</a>
<a n="rights">
<v s="95"/>
</a>
<a n="debugLevel">
<v s="3"/>
</a>
<a n="avsStoreLoc">
<v s="/home/kp1771/fw_base_path/collectors/DBBA1MM/avs"/>
</a>
</o>
<o t="Batch" id="8410" p="/" g="27">
<a n="name">
<v s="DBBA1MM"/>
</a>
<a n="owner">
<v r="/[AdminUser]Administrator"/>
</a>
<a n="rights">
<v s="95"/>
</a>
<a n="avsStoreLoc">
<v s="/home/kp1771/fw_base_path/collectors/DBBA1MM/avs"/>
</a>
</o>

I have to add

<a n="debugLevel">
<v s="3"/>
</a>

after

<a n="rights">
<v s="95"/>
</a>

if <a n="debugLevel">
<v s="3"/>
</a> does not exits. I have to add the tag in red
If exists has to modify
<a n="debugLevel">
<v s="4"/>
</a>

can you help me on this..any type of help is appriciated thanks

Have you tried anything so far? I would use awk to do this. When it finds <a n="rights"> you could read ahead a few lines using getline. If it contains "debugLevel" you could print out the new debug level, if not, just add debug level 3. For every other line, just print it.

Hi Annihilannic,
I used awk '/rights/,/avsStoreLoc/ { print }' a.xml
to extract the text between "rights" and "avsStoreLoc" ..but it didnt help me much

Here, I've done most of the work for you, you should be able to finish it easily:

awk '
        /n="rights"/ {
                print
                getline ; print
                getline ; print
                getline
                if (match($0,"debugLevel")) {
                        print "<a n=\"debugLevel\">\n<v s=\"4\"/>\n</a>"
                        # consume and discard next two lines
                        getline
                        getline
                } else {
                        # do some stuff here
                        print
                }
                next
        }
        1 # print other lines
' inputfile > outputfile

You just need to fill in the "do some stuff here" part.

awesome..Thanks Annihilannic..It helped ..thanks

Hi Annihilannic,
The script works good..I have one more concern i should be able to print the changes in the same file where we are searching..how can I do.

cp -p originalfile originalfile.bak && awk '<your script here>' originalfile.bak > originalfile

Hi Annihilannic,
I am very sorry,I tried your script
cp -p originalfile originalfile.bak && awk '<your script here>' originalfile.bak > originalfile

It didnt put back the changes to original file.I tried different ways but could not.
Should I use any merge or join to put back the changes to original file.
Let me know if any thing needs to be done
Thanks for your valuable answers to my query.

Hi Annihilannic,
No worries,the script is working good..Thanks a lot