Re: Regular Expression and file editing.
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 27 Dec 2006 07:25:31 -0800
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
Please do read that documentation yourself, Mumia. Opening a file for
read-write only works for fixed-length record files.
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.
Of course it doesn't. You didn't print to the file at all. You only
printed to the screen.
First, please disregard Mumia's suggestion. It does not apply to your
situation.
Second, you have two choices here. The compact, but potentially
confusing way, or the expanded but consequently longer way. The
compact way first:
#!/usr/bin/perl
use strict;
use warnings;
print "what is the ip to suspend\n";
chomp (my $ip = <STDIN>);
{
local @ARGV = ("/etc/pbx/mine.conf");
local $^I = q{};
while (<>) {
s/host=$ip/host=$ip\.old/mg;
print;
}
}
__END__
This program takes advantage of the inline editing feature. This is
enabled by the $^I variable. It makes the default output filehandle to
be a new file which is a copy of the file currently being read from
@ARGV, as processed by the <> operator.
If that's too many shortcuts to wrap your head around, and I don't
blame you if it is, do it out the long way:
#!/usr/bin/perl
use strict;
use warnings;
print "what is the ip to suspend\n";
chomp (my $ip = <STDIN>);
my $file = "/etc/pbx/mine.conf";
open my $fh, '<', $file or die "Cannot open $file for reading: $!";
open my $ofh, '>', "$file.new" or die "Cannot open $file.new for
writing: $!";
while (my $line = <$fh>) {
$line =~ s/host=$ip/host=$ip\.old/mg;
print $ofh $line;
}
close $fh;
close $ofh;
rename "$file.new", $file or die "Could not rename $file.new to $file:
$!";
__END__
That is, explicitly open the file for reading, while opening a new
blank file for writing. For every line read in from the original, make
your changes, and then print the line to the new file. When all is
done, move the new file to the old file's name.
Hope this helps,
Paul Lalli
.
- References:
- Regular Expression and file editing.
- From: Goksie
- Re: Regular Expression and file editing.
- From: "Mumia W."
- Re: Regular Expression and file editing.
- From: Goksie
- Regular Expression and file editing.
- Prev by Date: Re: what's your most favourite feature of perl?
- Next by Date: better way to skip first few lines of file read?
- Previous by thread: Re: Regular Expression and file editing.
- Next by thread: Re: Regular Expression and file editing.
- Index(es):
Relevant Pages
|