Re: Regular Expression and file editing.



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

.



Relevant Pages

  • Re: A script to flag commonly misused words
    ... is preferable in general as it allows you to scope warnings. ... use strict; ... I first wrote a script to extract the words to flag from your ... I discovered that at least one of the expressions you ...
    (comp.lang.perl.misc)
  • Re: [PATCH] Speed up "make headers_*"
    ... 'use strict' and 'use warnings' is recommended. ... The parentheses are not needed for most of the built-in functions. ... More or less the same comments would apply to the next script as well. ...
    (Linux-Kernel)
  • Re: Passing vars to a "require"d script
    ... > require'd script didn't seem to pull in the form data, ... >> use strict; ... >> use warnings; # main program ...
    (comp.lang.perl.misc)
  • Re: Pipe input over several scripts
    ... The problem is that script c is not getting the input. ... use strict; ... use warnings; ... buffer where script_c can get it. ...
    (comp.lang.perl.misc)
  • Re: Counting column delimiters per row in a text file
    ... Then parse (see the parse method) ... This can't be more than 20 lines or so including use strict and use ... Put the data in the __DATA__ section of your script. ... use warnings; ...
    (comp.lang.perl.misc)