We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

I need a simple Perl script to replace strings in an XML file

itcs-css
itcs-css asked
on
Medium Priority
249 Views
Last Modified: 2010-05-18
I need a simple Perl script to replace strings in an XML file.  Machine: Windows Server 2003 with ActivePerl 5.8.6

We need to manipulate an XML file for IIS config offline and the VB code needed to search and replace one string in the file is hideous and buggy.  Is there a simple Perl script to read in a file, search for "My Text-2", replace that with "My Text" and write the file back to either the original or a separate file?

Perl has to be the easiest language for this...prove me right!

Simplest, fastest one wins...
Comment
Watch Question

Hi itcs-css,

Try this:

$filename = "filename";
$search = "My Text-2";
$replace = "My Text";

die unless open( FH, "<$filename" );
die unless open( OUT, ">$filename.tmp" );
while $line ( <FH> ) {
    $line =~ /$search/$replace/;
    print OUT $line;
}
close( FH );
close( OUT );
unlink( $filename );
rename "$filename.tmp", $filename;
   
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
{local @ARGV=("filename"); local $^I="";
  while( <> ){
    s/My Text-2/My Text/g;
    print;
  }
}

Author

Commented:
Thanks for the quick response...

Unfortunately the code is giving syntax errors for lines 7 near "while $line", and 10 near "}", and complains that a scalar is found where operator expected on line 8.

Any chance someone could fix this?

Author

Commented:
Hi ozo,

Thanks for the quick response...

Unfortunately for yours I get...

Can't do inplace edit without backup at "blah blah.pl" line 2

Any ideas?
whoops sorry about that, try this:

$filename = "filename";
$search = "My Text-2";
$replace = "My Text";

die unless open( FH, "<$filename" );
die unless open( OUT, ">$filename.tmp" );
while( <FH> ) {
    $line = $_;
    $line =~ /$search/$replace/;
    print OUT $line;
}
close( FH );
close( OUT );
unlink( $filename );
rename "$filename.tmp", $filename;
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
local $^I=".bak";

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Im not sure if im missing something, but cannot this go straight on the command line...

perl -i.bak -pe "s/My Text-2/My Text/g" yourfilename
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
it can

Author

Commented:
Thanks for the help guys.

I will give out the points here in a minute...

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.