On 12/27/2006 08:17 AM, Goksie wrote:
Mumia W. wrote:
On 12/25/2006 11:51 AM, Goksie wrote:
[...]
If i run the script, the changes could not be effected bcos the files is
a readonly file.
I will be glad if someone can put me true
goksie
You probably want a mode of "+<" to open the file read-write.
perldoc -f open
when i used the below code and allow the printing to the files
#!/usr/bin/perl
use warnings;
use strict;
print "what is the ip to suspend\n";
our $ip = <STDIN>;
chomp($ip);
my $files = "/etc/pbx/mine.conf";
open my $fh, '+<', $files or die "can't open the files $files: $!";
while (<$fh>) {
s/host=$ip/host=$ip\.old/mg;
print $_;
}
mine.conf
[2.2.2.2]
type=friend
host=2.2.2.2
context=default
port=5060
dtmfmode=rfc2833
canreinvite=no
qualify=yes
allow=g723
the output of running the script is
[2.2.2.2]
type=friend
host=2.2.2.2.old
context=default
port=5060
dtmfmode=rfc2833
canreinvite=no
qualify=yes
allow=g723
but it does not change the file, but the control print i made to the
screen shows the change.
goksie
There are four ways I can think of to do this:
1) Collect the lines into an array, modify that array and write the
whole array back to the file.
2) Use in-place editing (read "perldoc perlrun").
3) Use Tie::File to treat the file as an array of lines, and modify the
line containing "host."
4) Use Config::IniFiles.
I demonstrate method 1 below:
use strict;
use warnings;
use File::Slurp;
our $ip = '2.2.2.2';
@ARGV = ('mine.conf');
my @lines;
while (<>) {
s/host=(\Q$ip\E)(?:\.old)?/host=$1\.old/mg;
push @lines, $_;
}
write_file 'mine.conf', @lines;
Methods 2, 3, and 4 are left as exercises for the reader. Good luck.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>