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> abc@gmail.com </to> </mail>
<page> <to> abc@gmail.com </to> </page>
</instance>
#
abctx007
  • 29
  • 5
  • Consider using a proper XML parser bc sed is not meant for such tasks. – oguz ismail Mar 15 '19 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](https://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la) – Cyrus Mar 15 '19 at 22:18
  • The best way to proceed is using an XML parser such as [tag:xmlstarlet] or directly [tag:xslt], both based on [tag: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. – kvantour Mar 17 '19 at 11:17

1 Answers1

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
UtLox
  • 3,744
  • 2
  • 10
  • 13