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

Maybe if you told us what wasn't working we could help you. I'm suspicious of how you're using HTML::TokeParser, but I'm not terribly familiar with the module.

-sam

  • Comment on Re: Performing a search and replace in XML across multiple directories

Replies are listed 'Best First'.
Re^2: Performing a search and replace in XML across multiple directories
by ilottl (Novice) on May 16, 2006 at 22:11 UTC
    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.
      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
Re^2: Performing a search and replace in XML across multiple directories
by ilottl (Novice) on May 16, 2006 at 23:37 UTC
    Hi Sam, I updated the code (see below). I think what is happening is that it is not opening the file but is only processing the name of the file. How do I make it open the content of the file to do the substitution?
    use File::Find; use strict; my $bak_ext = '.bak'; my $root_dir = 'C:/Temp/Chris/Bills/'; find(\&wanted, $root_dir); sub wanted { # 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> ) { my $TheLine = $_; s/Draft/Scrap/i; s/<label>([^<]*<\/label>)/<label denominator=no auto.number=no>$1/i; print OUTPUT; } close OUTPUT; } else { print "Skipping $_\n"; } }
      Thanks Grandfather, that is what I was trying to do.