You should use XML parsing tools for XML data. xmlstarlet
is a good choice. Regular expressions are just not powerful enough (canonical reference)
If your data looks like:
<root>
<foo>
<active>true</active>
<codePool>private</codePool>
</foo>
<foo>
<active>true</active>
<codePool>community</codePool>
</foo>
</root>
Then
xmlstarlet ed --update '//active[.="true" and ../codePool="community"]' -v false file.xml
produces
<?xml version="1.0"?>
<root>
<foo>
<active>true</active>
<codePool>private</codePool>
</foo>
<foo>
<active>false</active>
<codePool>community</codePool>
</foo>
</root>
Here's an awk program that does what you request. Keep in mind that it's fragile: if the input changes, this code will stop working. It does just use plain string operations.
awk '
BEGIN {
marker = "<codePool>community</codePool>"
srch = "<active>true</active>"
repl = "<active>false</active>"
}
index($0, marker) {
i = index(prev, srch)
if (i > 0)
prev = substr(prev, 1, i-1) repl substr(prev, i+length(srch))
}
{
if (prev) print prev
prev = $0
}
END {if (prev) print prev}
'
-z
in there. Good, good...