Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Replace Cont

Status
Not open for further replies.

vincebrown

Technical User
Apr 25, 2005
19
GB
Hi Guys, I posted a thread before where I wanted to know how to input a value between two xml tags. Can someone tell me how to simply replace a whole line in an xml file and replace it. Many thanks.

I need to replace: <PreprocessorFlags/>

With: <PreprocessorFlags>/U_CPPUNWIND</PreprocessorFlags>
 
Something like this will probably take care of you:

Code:
s#<PreprocessorFlags/>#<PreprocessorFlags>/U_CPPUNWIND</PreprocessorFlags>#g
 
Cool. thanks. I'm very new to Perl, how would it fit into the whole script? I know I open the filehandle first.
 
I suppose that really depends on what your script is doing. A very simple example would be something like the following:

Code:
open INFILE, "< input_file.txt" or die "Cannot open input file.\n$!\n";
open OUTFILE, "> output_file.txt" or die "Cannot open output file\n$!\n";

while ($_ = <INFILE>) {
  $_ =~ s#<PreprocessorFlags/>#<PreprocessorFlags>/U_CPPUNWIND</PreprocessorFlags>#g;
  print OUTFILE $_;
}

close OUTFILE;
close INFILE;

That little bit of code would open a file called input_file.txt, scan through all the lines (one at a time) replacing any occurance of <PreprocessorFlags/> with <PreprocessorFlags>/U_CPPUNWIND</PreprocessorFlags> and writing the text to output_file.txt.

If you're new to Perl, you might want to get used to reading through documentation. :)

A good place to start might be with the perl tutorials, give this a look:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top