Reputation: 31
I need to do a find and replace on 700+ XML files for the following:
<base>7.4</base>
needs to become
<base>7.2</base>
However, the only tool I have to use is Notepad++, and it continually crashes when I try to do the "replace in files" function. Is there another easy way to perform this mass find and replace? I'm not much of a coder, but they're all in the same folder, maybe there's a way I could use a .bat file to run through them? Any advice on what to do or how to write that would be much appreciated.
Thanks in advance,
Colette
Upvotes: 3
Views: 19844
Reputation: 674
You can download a free trial of PowerGREP from http://www.powergrep.com/
Then:
Search and replace
as your action type<base>7.4</base>
in Search box<base>7.2</base>
in ReplacementEnter values and click on Preview
Preview highlights proposed changed
Click on 'Replace'
Files changed
Upvotes: 2
Reputation: 53488
XML is annoying to process, because whilst it looks like plain text - it actually isn't. It's therefore not ideal to use 'text based' pattern matching to replace it, because ... well, you can break formatting on it. Personally I'd be suggesting:
XML::Twig
module (ppm install XML::Twig
); thisscript.pl file1.xml file2.xml file3.xml
)Like this
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
sub replace_base {
my ( $twig, $base ) = @_;
if ( $base->text eq "7.2" ) {
$base->set_text("7.4");
}
}
#iterate the files on the command line
foreach my $filename (@ARGV) {
#set up the processor
my $twig = XML::Twig->new(
#output formatting
'pretty_print' => 'indented_a',
#every time a 'base' element is encountered, run replace_base on it.
'twig_handlers' => { 'base' => \&replace_base }
);
#process this file
$twig->parsefile($filename);
#save the results to a '.new' file.
open( my $output, ">", "$filename.new" ) or die $!;
print {$output} $twig->sprint;
close($output);
}
Upvotes: 0