Mass Find and Replace in Multiple XML Files -
i need find , replace on 700+ xml files following:
<base>7.4</base>
needs become
<base>7.2</base>
however, tool have use notepad++, , continually crashes when try "replace in files" function. there easy way perform mass find , replace? i'm not of coder, they're in same folder, maybe there's way use .bat file run through them? advice on or how write appreciated.
thanks in advance,
colette
xml annoying process, because whilst looks plain text - isn't. it's therefore not ideal use 'text based' pattern matching replace it, because ... well, can break formatting on it. i'd suggesting:
- download perl (like activestate community edition)
- install
xml::twig
module (ppm installxml::twig
); - use script below - specify files want change on command line (e.g.
thisscript.pl file1.xml file2.xml file3.xml
)
like this
#!/usr/bin/env perl use strict; use warnings; use xml::twig; sub replace_base { ( $twig, $base ) = @_; if ( $base->text eq "7.2" ) { $base->set_text("7.4"); } } #iterate files on command line foreach $filename (@argv) { #set processor $twig = xml::twig->new( #output formatting 'pretty_print' => 'indented_a', #every time 'base' element encountered, run replace_base on it. 'twig_handlers' => { 'base' => \&replace_base } ); #process file $twig->parsefile($filename); #save results '.new' file. open( $output, ">", "$filename.new" ) or die $!; print {$output} $twig->sprint; close($output); }
Comments
Post a Comment