0

I need to run a user-data script in an ec2-instance that changes an XML in a folder.

I have an XML like this one in a path in the ec2:

<Root>
    <Properties>
       <Property>
          <Name>myProp</Name>
          <Value>old_value</Value>
       </Property>
       <Property>
          <Name>anotherProp</Name>
          <Value>other_value</Value>
       </Property>
    </Propierties>
</Root>

I want to change old_value into new_value. But just that one, not other_value.

How can I do this?

I've tried a powershell script but I don't know how to run it to test it:

#!/bin/bash
$path = '/home/wowza/conf/Server.xml'

$new_value = 'new_value'

$xml = [xml](Get-Content $path)

$xml.Data.Course.Subject

$property = $xml.Root.Server.Properties.Property | where {$_.Name -eq 'myProp'}
$property.Value = $new_value

$xml.Save($path)

Please if you send me a script don't forget to include how to run it to test it, the interpreter for the #! line and what things to install. Thank you!

1

2 Answers 2

2

With GNU sed:

sed -i '/<Name>myProp<\/Name>/,/<Value>old_value<\/Value>/s/old_value/new_value/' file.xml

Output to file file.xml:

<Root>
    <Properties>
       <Property>
          <Name>myProp</Name>
          <Value>new_value</Value>
       </Property>
       <Property>
          <Name>anotherProp</Name>
          <Value>other_value</Value>
       </Property>
    </Propierties>
</Root>
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfectly!!!!! Thank you. Do you know how I could make it independent of the old_value text? I mean, to get it changed even if old_value is different?
Try this: sed -i '/<Name>myProp<\/Name>/,/<Value>/s/<Value>[^<]*/<Value>new_value/' file.xml or sed -E '/<Name>myProp<\/Name>/,/<Value>/s/(<Value>).*(<\/Value>)/\1new_value\2/' file.xml
1

The code below will allow you to set the new_value for the targeted field even if you don't know what the old_value is:

#!/bin/bash

sed -i -e '/<Name>myProp<\/Name>/,/<Value>/s/^\s*<Value>.*$/<Value>new_value<\/Value>/' /folder/xml-file

The only caveat here is that the script will strip white space indentations from the line to be changed, but that is only a visual thing that does not affect the code result; you can add the indentation spaces you need afterwards with another sed command, if that is important to you.

2 Comments

Do you know how I could make it independent of the old_value text? I mean, to get it changed even if old_value is different?
The code above already does that - it will change the value of the targeted field regardless of what the old_value is (and you do not need to refer to the old_value anywhere). Just change the new_value on the code above to whatever you want to update the value to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.