Say i have the following XML:
<app-deployment>
<name>gr1</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>stage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr2</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
<staging-mode>nostage</staging-mode>
</app-deployment>
<app-deployment>
<name>gr3</name>
<target>AdminServer</target>
<module-type>ear</module-type>
<source-path>/u01/app/wls1035_homes/wls1035_9999/grc864</source-path>
<security-dd-model>DDOnly</security-dd-model>
</app-deployment>
how can i extract the value of the staging-mode tag, say for the app-deployment named gr2?
Many people (including myself), will tell you that it is a fools errand to parse xml with reg-ex based tools, and that you should use tools designed for xml parsing. Xpath should work for this, and xmlstarlet would be a package you could install and use quickly.
That said, given that you assume your data will always be well formed, it is pretty easy to make an awk script to search for 1 pattern, set a flag, look for another pattern, set a flag, etc. And when you have found the final target, cleanup the line to extract just the data you want.
Output
The
set -- gr3and{ cat ... } |are a testing harness, you would wrap this is a shell script i.e.and call like
This 2nd part is untested. Let me know if you have problems with it.
I hope this helps