0

below is my config file for monitoring system and i want to change string on below file preferrable via linux bash.

i want to scan complete file and look for default_not_os_not_oracle block and then change where i see testemail string to alertmail in default_not_os_not_oracle block only. i dont mind getting help in solution on python/perl/sed/awk way

#
<!-- language: lang-xml -->
<instance:default_oracle>
<map>
/ora.*
/arch.*
</map>
<mail>
<to> %DBA% </to>
</mail>
</instance>

<instance:default_not_os_not_oracle>
<map> (?!/$|/users|/auto|/dev|/tmp|/var|/ora|/arch).* </map>
<action> mail </action>
<mail>
<to> testemail </to>
</mail>
</instance>


<instance:diskmon>
<warn>
<ge> 75 </ge>
<action> mail </action>
</warn>
<crit>
<ge> 90 </ge>
<action> page,mail </action>
</crit>
<map>
/apps
</map>
<mail> <to> [email protected] </to> </mail>
<page> <to> [email protected] </to> </page>
</instance>
#
3
  • Consider using a proper XML parser bc sed is not meant for such tasks. Commented Mar 15, 2019 at 22:15
  • 1
    Possible duplicate of Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms Commented Mar 15, 2019 at 22:18
  • The best way to proceed is using an XML parser such as xmlstarlet or directly xslt, both based on xpath. It is generally not recommended to use tools such as sed or awk or any other line-based processing tool to alter XML files because of the complicated structure they have. For one-off tasks if you know exactly how the thing looks like, it is generally not a problem. But if you do not know how the file looks like or you have to do it a gazillion times, use an XML parser. Commented Mar 17, 2019 at 11:17

1 Answer 1

1

The save way with input and output file:

sed "/default_not_os_not_oracle/,/\/instance/s/testemail/alertmail/g" infile.xml >outfile.xml

or inplace

sed -i "/default_not_os_not_oracle/,/\/instance/s/testemail/alertmail/g" infile.xml
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.