Modify xml using sed or awk

Hi All,

I want to modify(changing the status from "on" to "off" status of [GoogleOverhaul] Stage-element value from the below xml file using sed or awk:

File Name: global.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
  <widget>
        <name>HTTP-POOL</name>
        <attributes>
            <maxconnections>5000</maxconnections>
            <connectionsperhost>50</connectionsperhost>
            <sockettimeout>1000</sockettimeout>
            <connectiontimeout>200</connectiontimeout>
            <status environment="mwh">on</status>
            <status environment="las">on</status>
            <status environment="iad">on</status>
            <status environment="dub">on</status>
            <status environment="lon">on</status>
            <status environment="hkg">on</status>
            <status environment="development">on</status>
            <status environment="staging">on</status>
            <status environment="testlab-1">on</status>
            <status environment="perflab">on</status>
        </attributes>
    </widget>
  <widget>
    <name>GoogleOverhaul</name>
    <attributes>
        <status environment="mwh">on</status>
        <status environment="las">on</status>
        <status environment="iad">on</status>
        <status environment="dub">off</status>
        <status environment="lon">off</status>
        <status environment="hkg">off</status>
        <status environment="development">on</status>
        <status environment="staging">on</status>
        <status environment="testlab-1">off</status>
        <status environment="perflab">off</status>
    </attributes>
</widget>
 <widget>
        <name>RA</name>
        <attributes>
            <status environment="mwh">off</status>
            <status environment="las">off</status>
            <status environment="iad">off</status>
            <status environment="hkg">off</status>
            <status environment="dub">off</status>
            <status environment="lon">off</status>
            <status environment="development">off</status>
            <status environment="staging">off</status>
            <status environment="testlab-1">off</status>
            <status environment="perflab">off</status>
        </attributes>
    </widget>
</config>

Thanks for any help.

Please use code tags as required by forum rules!

Try

awk     '/<name>GoogleOverhaul<\/name>/ {L=1}
         /<\/widget>/                   {L=0}
         L && /staging.*on/             {sub(/>on</,">off<")}
         1
        ' global.xml
2 Likes
sed -i 's:<status environment="staging">on</status>:<status environment="staging">off</status>:' global.xml

Try

awk '$1=="name" {f=($2=="GoogleOverhaul")?1:0} f && $1~/"staging"/{$2="off"}1' RS=\< ORS=\< FS=\> OFS=\> file

If it gets more complicated it may be better to use specializer XML tooling....

--
@balajesuri: that will change all staging elements to "off"

1 Like

@Scrutinizer: Yeah, you're right. Didn't notice multiple instances of that line! :slight_smile:

Thanks a lot to everyone, for your valuable solution.