Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Feb 2002 09:50:25 -0700
From:      z thompson <cublai@lastamericanempire.com>
To:        Eric Six <erics@sirsi.com>
Cc:        "'questions@FreeBSD.ORG'" <questions@FreeBSD.ORG>
Subject:   Re: Perl question...
Message-ID:  <20020201095025.A79152@titus.lastamericanempire.com>
In-Reply-To: <DC32C8CEB3F8D311B6B5009027DE5AD503D207F8@stlmail.dra.com>; from erics@sirsi.com on Fri, Feb 01, 2002 at 09:55:19AM -0600
References:  <DC32C8CEB3F8D311B6B5009027DE5AD503D207F8@stlmail.dra.com>

next in thread | previous in thread | raw e-mail | index | archive | help
* Eric Six <erics@sirsi.com> [020201 09:00]:
> 
> 
> Though this doesn't pertain to BSD, except for the fact I am running this on
> freebsd ;), I wanted to run it by the list as I have gotten wonderful
> answers before!
> 
> I have about 400 primary and 300 secondary DNS records that I have migrated
> from a bind4 server. I need to add a '$TTL value;' to the first line of all
> my zone files... 
> 
> I have found ways to append lines to the file, but not to create a new one
> at the very beginning. Also, any ideas on how to automate doing this to all
> the files in each dir?
> 

With temporary files...

my $old_file = 'some_file';             # original file
my $tmp_file = '>some_file.tmp';        # a temp file
my $new_line = '$TTL value;';           # stuff to add to file

# get the original file contents
open OLD, $old_file or die $!, "\n";
my @contents = <OLD>;
close OLD or die $!, "\n";

# print new stuff and original contents to temp file
open TMP, $tmp or die $!, "\n";
print TMP $new_line, "\n", @contents;
close TMP or die $!, "\n";

# rename the temp file to the old file
rename $tmp_file, $old_file 
        or die 'Failed to rename ', $tmp_file, ' to ', $old_file, ': ',
        $!, "\n";
        


If you have a small number of directories, 


my @dirs = qw(/etc/namedb/one /etc/namedb/two);

for my $dir (@dirs){
        chdir $dir or die 'Failed to cd to ', $dir, ': ', $!, "\n";
        while(my $file = <*>){
                next if $file =~ /filter/; # skip certain files

                # use above, i.e. open old, print tmp, rename
        }
}


This is fairly overt method.


Zach Thompson





To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020201095025.A79152>