For a start you have to create a new instance of the file. Perl helps by facilitating inplace editing of files using @ARGV, $^I and while (<>) {...} magic.
The other less common element Perl used in this code is the scalar context range operator ...
use strict; use warnings; open OUTFILE, '>', 'delme.txt'; print OUTFILE <<TEXT; zone "dontchangethisdomain.com" { type master; file "/somepath/to/dontchangethisdomain.com"; notify yes; } zone "mydomain.com" { type master; file "/path/to/zone/mydomain.com"; notify yes; } zone "leavethisalonetoo.com" { type master; file "/other/path/to/leavethisalonetoo.com"; notify yes; } TEXT close OUTFILE; my $targetdomain = 'mydomain.com'; @ARGV = ('delme.txt'); $^I = '.bak'; while (<>) { print "#" if m/^zone "\Q$targetdomain\E" {/ .. m/^}/; print; }
Generates delme.txt containing:
zone "dontchangethisdomain.com" { type master; file "/somepath/to/dontchangethisdomain.com"; notify yes; } #zone "mydomain.com" { #type master; #file "/path/to/zone/mydomain.com"; #notify yes; #} zone "leavethisalonetoo.com" { type master; file "/other/path/to/leavethisalonetoo.com"; notify yes; }
In reply to Re: Perl script to comment out lines in named.con file
by GrandFather
in thread Perl script to comment out lines in named.conf file
by firewall00
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |