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!

Replace 1

Status
Not open for further replies.

vincebrown

Technical User
Apr 25, 2005
19
GB
Can someone please show me how to make this script replace '<PreprocessorFlags/>' with '<PreprocessorFlags>U_CPPUNWIND</PreprocessorFlags>'. Thanks.

open FILE, 'C:/cpptest\C++TestFiles\Projects\Hello\Product\ProjectConfig.pcfg';
foreach(<FILE>){
push (@file,$_);
}
close FILE;
unlink 'C:/cpptest\C++TestFiles\Projects\Hello\Product\ProjectConfig.pcfg';
open FILE, '>C:/cpptest\C++TestFiles\Projects\Hello\Product\ProjectConfig.pcfg';
foreach (@file){
if ($_ =~ /(.*)(<PreprocessorFlags\/\>)(.*)/){
print FILE " $1\t$2/U_CPPUNWIND</PreprocessorFlags>$3\n";
}else{
print FILE $_;
}
}
close FILE;
 
Code:
my $file = 'C:\cpptest\C++TestFiles\Projects\Hello\Product\ProjectConfig.pcfg';
open FILE, "$file";
foreach(<FILE>){
    push (@file,$_);
}
close FILE;
unlink $file;
open FILE, ">$file";
    foreach (@file){
        if ($_ =~ /<(PreprocessorFlags)\/>/){
            print FILE "<$1>U_CPPUNWIND</$1>\n";
        }else{
            print FILE $_;
        }        
    }    
close FILE;


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
if it's an important file I'd make a backup copy first before deleting the original file. Then do the edits like perluserpengo has shown you. You can use the File::Copy module to make a backup copy of the original file. If the files not that important then never mind. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top