in reply to Perl script to comment out lines in named.conf file
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl script to comment out lines in named.con file
by firewall00 (Acolyte) on Oct 05, 2007 at 00:26 UTC | |
by GrandFather (Saint) on Oct 05, 2007 at 00:45 UTC | |
by firewall00 (Acolyte) on Oct 05, 2007 at 01:20 UTC | |
by firewall00 (Acolyte) on Oct 05, 2007 at 04:05 UTC | |
by GrandFather (Saint) on Oct 05, 2007 at 04:31 UTC |