in perl, I am trying to open a file , find a string, replace the string, and close the file. I have scoured the internet, and this is what I have come up with:
#!/usr/bin/perl -w
# use warnings;
my $oldUrl = "someString";
my $newUrl = "someOtherString";
$file = "someFile";
open (MYFILE,"$file") or die "Can't open '$file': $!";
while(my $row = <MYFILE>){
if($row =~ /$oldUrl/)
{
$row = "$newUrl";
print MYFILE $row;
}
}
close(MYFILE);
this does nothing except print to screen the text: someString. I cant get the newUrl to get actually written in the file, what am i missing?
how would i do all files in a dir that end in .cfg:
#!/usr/bin/perl -w
use warnings;
my $directory = '/tftpboot';
my $oldUrl = "account.1.sip_server_host = s150133.trixbox.fonality.com";
my $newUrl = "account.1.sip_server_host = 162.221.24.130";
....
.....
open(FOO, $file)opens$filefor reading, but you're also trying to write to it. That won't work. Also, this won't fix your particular issue, but in general, you should use the safer, three-argument form ofopenand lexical filehandles:open my $fh, '<', $file or die $!;