Reputation: 669
I have the following function which uses awk:
getStageMode() {
targ=$1; shift
mode=$(awk '
/[<]app-deployment/{a=1}
a && /[<]name[>]'"${targ}[<]"'/{n=1}
a && n && /[<]staging-mode[>]/{
sub(/[<]staging-mode[>]/,"", $0)
sub(/[<]\/staging-mode[>]/,"",$0)
print $0
exit
}
{print "n=" n }
' "${@}")
echo $mode
}
I am having trouble resting the n flag after it's set to 1. where is the proper place to reset it?
Upvotes: 1
Views: 189
Reputation: 43718
/[<]\/app-deployment/{a=0; n=0}
So, you would have:
getStageMode() {
targ=$1; shift
mode=$(awk '
/[<]\/app-deployment/{a=0; n=0}
/[<]app-deployment/{a=1}
a && /[<]name[>]'"${targ}[<]"'/{n=1}
a && n && /[<]staging-mode[>]/{
sub(/[<]staging-mode[>]/,"", $0)
sub(/[<]\/staging-mode[>]/,"",$0)
print $0
exit
}
{print "n=" n }
' "${@}")
echo $mode
}
Explanation: OP has a generated file with XML nodes like:
<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>
The AWK script sets a
when we are inside an app-deployment
XML node, and n
when that node has the name
we are looking for. Since some nodes have no staging-mode
, the original AWK, which didn't reset neither a
nor n
, could return the staging-mode
for the following node. Resetting those variables when exiting a node should fix that issue.
Upvotes: 2