0

I want to replace a specific value in xml file.

My Test XML is as below -

      <Field Name="ABC" IsArray="false" IsLocked="false">
    <Description>THIS IS FOR ABC VALUE</Description>
    <Comment></Comment>
    <PropertyList>
            <Property Name="ABC" Type="Boolean">
            <DefaultValue>false</DefaultValue>
            </Property>
    </PropertyList>
    <PropertyValueList>
            <PropertyValue PropertyName="ABC">
                    <Value>true</Value>
            </PropertyValue>
    </PropertyValueList>
</Field>

I want to change value from true to false only for PropertyName=ABC

Any suggestions? sed/awk/perl anything would do.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Pankaj_Pandav
  • 79
  • 1
  • 8

2 Answers2

1

You could do this through awk,

awk '/PropertyName=\"ABC\"/{print; getline; sub(/true/,"false")}1' file
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • @user3780855 Its OK to ask someone accept an answer, but you should always wait some hour before accepting it, sine it may come a better one. – Jotne Jun 27 '14 at 05:19
  • @Jotne he's a new user. So that i posted the above link. And also he mentioned "This answer works for me". – Avinash Raj Jun 27 '14 at 05:20
  • And it does not work, it removes the line `` you need `awk '/PropertyName=\"ABC\"/{print;getline; sub(/true/,"false")}1'`. You could write: If you like my answer and you do not find some better after some time, consider accepting it. – Jotne Jun 27 '14 at 05:22
  • @jotne oh sorry. forget about that. I will follow the above. – Avinash Raj Jun 27 '14 at 05:24
  • And you should try to avoid using `getline`, look what `Ed` writes here: http://awk.freeshell.org/AllAboutGetline You could do: `awk '/PropertyName=\"ABC\"/{f=NR} NR=f+1{sub(/true/,"false")}1'` – Jotne Jun 27 '14 at 05:26
1

You can use xmlstarlet:

xmlstarlet ed -u "/Field/PropertyValueList/PropertyValue/Value/text()" -v "false" test.xml > result.xml
helderdarocha
  • 23,209
  • 4
  • 50
  • 65