14.04 - Grep replace in multiple xml files - Ask Ubuntu


i have below command find .xml files containing <active>true</active> having <codepool>community</codepool> after line on next line.

grep -rzl '<active>true</active>.*<codepool>community</codepool>' --include='*.xml' --color=always 

now how combine sed replace true string inside <active>... tag false string along matched lines ?

you should use xml parsing tools xml data. xmlstarlet choice. regular expressions not powerful enough (canonical reference)

if 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" , ../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 awk program request. keep in mind it's fragile: if input changes, code stop working. use plain string operations.

awk '     begin {         marker = "<codepool>community</codepool>"         srch = "<active>true</active>"         repl = "<active>false</active>"     }     index($0, marker) {         = 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} ' 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User