Solved

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

Posted on 2006-10-25
9
229 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...
0
Comment
Question by:itcs-css
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:rivusglobal
ID: 17807390
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;
   
0
 
LVL 84

Expert Comment

by:ozo
ID: 17807431
{local @ARGV=("filename"); local $^I="";
  while( <> ){
    s/My Text-2/My Text/g;
    print;
  }
}
0
 
LVL 1

Author Comment

by:itcs-css
ID: 17807495
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?
0
 
LVL 1

Author Comment

by:itcs-css
ID: 17807522
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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:rivusglobal
ID: 17807935
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;
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 17807968
local $^I=".bak";
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 17828417
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
0
 
LVL 84

Expert Comment

by:ozo
ID: 17829331
it can
0
 
LVL 1

Author Comment

by:itcs-css
ID: 17833793
Thanks for the help guys.

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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now