in reply to Re: Performing a search and replace in XML across multiple directories
in thread Performing a search and replace in XML across multiple directories

Oh sorry, the script ony works if I ask to replace text only. However it will not pickup contents such as greater and less than signs, hyphenation etc. It only seems to be working on text. I am not concerned with using HTML:Tokkeparser or not.
  • Comment on Re^2: Performing a search and replace in XML across multiple directories

Replies are listed 'Best First'.
Re^3: Performing a search and replace in XML across multiple directories
by ilottl (Novice) on May 16, 2006 at 22:57 UTC
    OK, I have updated the script to this.. It renames the file corrctly but I get no output in the renamed file.
    use File::Find; use strict; my $bak_ext = '.bak'; my $root_dir = 'C:/Temp/Chris/'; find(\&fix, $root_dir); sub fix { # if the extension fits... if ( /\.xml?/i ) { print "Processing $_\n"; my $new = $_; my $bak = $_ . $bak_ext; rename $_, $bak or die "Cannot rename $_ to $bak: $!"; open OUTPUT, "> $new" or die "Cannot open $new for writing: $! +"; while ( <OUTPUT> ) { Print Go Go Go; my $TheLine = $_; s/<label>([^<]*<\/label>)/<label denominator=no auto.number=no>$1/i; print OUTPUT; } } close OUTPUT; }

      What are you expecting while (<OUTPUT>) to do?

      Looks like what you really want is an in place edit. Someting like this may get you to where you want to be:

      use strict; use warnings; use File::Find; my $bak_ext = '.bak'; my $root_dir = 'C:/Temp/Chris/'; find(\&fix, $root_dir); sub fix { # if the extension fits... return if ! /\.xml?/i; print "Processing $_\n"; # set up in place edit local @ARGV = ($_); # local $^I = $bak_ext; # Edit the file while (<>) { # Edit $_ (the current line) as required here print; # Write out the new version of the line } }

      DWIM is Perl's answer to Gödel