Find a line, and comment out the next 5 lines

Discussion in 'Perl' started by joe shaboo, Apr 13, 2004.

  1. joe shaboo

    joe shaboo Guest


    I'm new to Perl. I'm trying to write a simple script which will
    comment out a domain in my named.conf file for Bind.

    What I would like to do, is write a script which will comment out a
    domain from my named.conf if I no longer want to host that domain.

    So, what I'd like to do is create a means to search for a domain
    ( and comment out that file and all lines associated with
    the file, and put a date when it was commented out. I'd also like to
    take the UID of who commented it out, so if needed, we can ask that
    person why it was commented out. As a hint we use sudo, so this should
    be plausible, but again, not sure of the system command. If I use id,
    it says I'm root, but how can I get the name of the user before I
    became root?

    named.conf example

    zone "" {
    type master;
    file "/path/to/zone/";
    notify yes;

    What I'd like to do

    // Domain taken out of named.conf at request of Customer by UID, date
    // zone "" {
    // type master;
    // file "/path/to/zone/
    // notify yes;
    // }

    Sounds simple enough, but I am not sure how to do it.

    many thanks,

    joe ()
    joe shaboo, Apr 13, 2004
    1. Advertisements

  2. mdh

    mdh Guest

    After providing you the obligatory warning about automating anything
    to do with DNS (smile), what you're basically try to accomplish is the
    following steps:

    1) examine a multiline string for the following pattern:

    \nzone "YOURTARGETZONE" {\n(arbitrary characters)\n(arbitrary
    characters)\n(arbitrary characters);\n(arbitrary characters)}\n

    2) take the string (the entire file) and chop it into three parts:
    $prematch -- the characters before the match
    $thematch -- the characters matching the pattern
    $postmatch - the characters after the match

    3) on $thematch, use the substitution operator to replace the
    newlines with "\n//"

    4) join $prematch, $thematch, and $postmatch back into one string

    5) write the result joined string back out as your new zone file

    Try the following code snippet...



    my $UID = "joeblow\";
    my $datestring = `date -u "+%m/%d/%Y %H:%M:%S"`;
    $datestring =~ tr/\n\r//d; # delete trailing newline or carriage
    my $targetdomain = "";
    # define a variable using the HERE construct for simulated zone file
    my $sampleinput = <<"EOF";

    zone "" {
    type master;
    file "/somepath/to/";
    notify yes;

    zone "" {
    type master;
    file "/path/to/zone/";
    notify yes;

    zone "" {
    type master;
    file "/other/path/to/";
    notify yes;


    my $prematch;
    my $thematch;
    my $postmatch;

    # use a regular expression with "non-greedy" matching to the token
    # "zone" and the closing brace character (}) to anchor on the zone
    # definition matching the domain you want to comment out -- you
    # want "non-greedy" matching cuz you want the shortest string that
    # starts with "zone", has your target, and ends with a closing
    # brace -- if you use a greedy match (.*), you'll match on the last
    # brace in the file, commenting out multiple domains in error

    if ($sampleinput =~ m/\n(.?)zone "$targetdomain"
    \{\n(.*)type(.*);\n(.*)file(.*);\n(.*)notify(.*);\n(.?)\}/is) {
    $prematch = $`;
    $thematch = $&;
    $postmatch = $';
    $thematch =~ s/\n/\n\/\//g; # globally replace newlines with
    newline and //
    print "$prematch\n";
    print "//domain taken out of named.conf at request of Customer by
    $UID, $datestring";
    print "$thematch";
    print "$postmatch\n";

    mdh, Apr 20, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Doan
    Larry Doan
    Oct 22, 2003
  2. Alec S.
    Alec S.
    Apr 16, 2005
  3. Deniz Bahar
    Andrey Tarasevich
    Mar 9, 2005
  4. bolega
    Steven D'Aprano
    Jan 14, 2011
  5. oldyork90

    find it, cut it out, find next

    oldyork90, Jan 16, 2012, in forum: Perl Misc
    John W. Krahn
    Jan 22, 2012

Share This Page