-1

i have xml file here as shown in below

 <?xml version="1.0" encoding="utf-8"?><Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytype" xmlns="http://we.xyti.com/2011/01/gone">  <Parameters>
<Parameter Name="mytype" Value="-1" />
<Parameter Name="new1" Value="" />
<Parameter Name="new2" Value="" />
<Parameter Name="new3" Value="" />
<Parameter Name="new4" Value="" /> </Parameters></Application></Application>`

In the above xml i need to add value in each line eg:- instead "" have to put value as test, different for each Name attribute. E.g.

 <?xml version="1.0" encoding="utf-8"?><Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/mytype" xmlns="http://we.xyti.com/2011/01/gone">  <Parameters>
<Parameter Name="mytype" Value="-1" />
<Parameter Name="new1" Value="test1" />
<Parameter Name="new2" Value="test2" />
<Parameter Name="new3" Value="test3" />
<Parameter Name="new4" Value="test4" /> </Parameters></Application></Application>
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
pramod s
  • 1
  • 3
  • @WiktorStribiżew it didnt slve my qauestion coz i need add value in "" on each line :) -thank you for reply – pramod s Sep 14 '18 at 12:50
  • Yes, so what is the problem with `sed -E 's/(enable-welcome-root=")"/\1NEWVALUE"/' file`? The solution is there, you just need to remove `[^"]+` that requires a value to be present (and will be replaced). Add `g` at the end to replace multiple occurrences : `sed -E 's/(enable-welcome-root=")"/\1NEWVALUE"/g' file` – Wiktor Stribiżew Sep 14 '18 at 12:51
  • i tried :) and in mycase i need added where "" need to add "test" @WiktorStribiżew all will be diffenrt value – pramod s Sep 14 '18 at 12:56
  • So replace `NEWVALUE` with `test` in my above code. – Wiktor Stribiżew Sep 14 '18 at 12:58
  • sed -e 's/(Value=")"/\test"/g' "C:\Users\user1\Desktop\test\Cloud.xml" getting error -e expression #1, char 21: Unknown option to `s :( – pramod s Sep 14 '18 at 13:05
  • Copy/paste: `sed -E 's/( Value=")"/\1test"/g' file > newfile`. Are you on Windows? Make sure ``\`` do not need to double the ``\``s in your path, or just use single quotes, `'C:\Users\user1\Desktop\test\Cloud.xml'` – Wiktor Stribiżew Sep 14 '18 at 13:07
  • invalid option -- E – pramod s Sep 14 '18 at 13:09
  • If `-E` does not work, try `-r`. If `-r` does not work, use `sed 's/\( Value="\)"/\1test"/g' 'C:\Users\user1\Desktop\test\Cloud.xml' > 'C:\Users\user1\Desktop\test\Cloud_out.xml'` – Wiktor Stribiżew Sep 14 '18 at 13:10
  • : -e expression #1, char 24: Unknown option to `s' sorry for long discussion .. and thank you so much so helping .. plz help me out in slovein this am using this command "C:\Users\bizruntime\Desktop\test\sed.exe" -e "s/\( Value="\)"/\1test"/g" "C:\Users\user1\Desktop\test\Cloud.xml" > "C:\Users\user1\Desktop\test\Cloud_out.xml" – pramod s Sep 14 '18 at 13:13
  • [Look here](http://rextester.com/XFKS15590), you keep using `-e`, don't. – Wiktor Stribiżew Sep 14 '18 at 13:16
  • Aswme thank you so much :) but last i need add different different value for new 1 and new2 and new3 line on value="' – pramod s Sep 14 '18 at 13:20
  • That is really simple, see http://rextester.com/UHXI47492 – Wiktor Stribiżew Sep 14 '18 at 13:22
  • Yes sloved .. thank you so much mate :) @WiktorStribiżew – pramod s Sep 14 '18 at 13:28
  • **Do not use regex to parse XML.** It will fail to omit commented-out XML properly. It will fail in a myriad of ways. Just because you can hack a regex for a simple case doesn't mean you ought to. Commenters: Recommend real, robust solutions such as those based upon XML parsers or XPath be used instead. It's the responsible thing to do. See @Cyrus's answer below for one robust way to solve OP's problem. – kjhughes Sep 14 '18 at 15:48

1 Answers1

1

With this xml file

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://we.xyti.com/2011/01/gone" Name="fabric:/mytype">
  <Parameters>
    <Parameter Name="mytype" Value="-1"/>
    <Parameter Name="new1" Value=""/>
    <Parameter Name="new2" Value=""/>
    <Parameter Name="new3" Value=""/>
    <Parameter Name="new4" Value=""/>
  </Parameters>
</Application>

and xmlstarlet:

xmlstarlet edit -N x="http://we.xyti.com/2011/01/gone" --update '//x:Parameter/@Value' --value "test" file.xml

Output:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://we.xyti.com/2011/01/gone" Name="fabric:/mytype">
  <Parameters>
    <Parameter Name="mytype" Value="test"/>
    <Parameter Name="new1" Value="test"/>
    <Parameter Name="new2" Value="test"/>
    <Parameter Name="new3" Value="test"/>
    <Parameter Name="new4" Value="test"/>
  </Parameters>
</Application>
Cyrus
  • 84,225
  • 14
  • 89
  • 153