Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All,

I have a XML file which is looks like as below. <<please see the attachment >>

  <?xml version="1.0" encoding="UTF-8"?>
  <esites>
      <esite>
          <name>XXX.com</name>
          <storeId>10001</storeId>
                                  <module>
              <name>InventoryVisibility</name>
              <active>
                <activeStatus>true</activeStatus>
                <sku_display>true</sku_display>
                <cart>true</cart>
                <order_review>true</order_review>
                <process_order>true</process_order>
            </active>
              <standAlone>false</standAlone>
              <timeout>120</timeout>
              <codec>
                  
              </codec>
              <transport>
                  
              </transport>
              <throttle>
                  <throttlestatus>True</throttlestatus>
                  <throttlelimit>18</throttlelimit>
              </throttle>
          </module>

Need shell script where i need find the word �InventoryVisibility� in the XML file [blue in color ], once the word[inventoryVisibility�] is found I want to replace next 7 lines [red in color] with the below highlighted text.

  <active>
                  <activeStatus>False</activeStatus>
                  <sku_display> False </sku_display>
                  <cart> False </cart>
                  <order_review> False </order_review>
                  <process_order> False </process_order>
  </active>

your help is appreciated. Thanks in advance.

Is it always 7 lines? Wouldn't it be wiser to remove the <active> ... </active> and insert your replacement text?

Hi Rajeev,

This is quick and dirty, but should work.

If your first file is called file01.xml and you put the contents of the second in something like file02.xml you can do the following.

sed -e '/<active>/,/<\/active>/d' file01.txt > out01.txt

Then you should be able to run the following command.

sed -e '/InventoryVisibility/ r file02.txt' < out01.txt > out02.txt

Once you have done that all you should have to do is move out02,txt to filename.xml - remember and keep copies of the originals.

This can probably be done in a single line of sed or awk, but I don't use it enough.

Regards

Gull04

try also:

awk '
p==1 && /<\/active>/ {p=0}
p==1 {sub(">.*<",">False<")}
/> *InventoryVisibility *</ { p=1 }
1
' sample.xml
sed '/InventoryVisibility/,/<\/active>/ s/true/False/' filename