Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Feb 2002 19:26:21 +0100
From:      F.Xavier Noria <fxn@isoco.com>
To:        z thompson <cublai@lastamericanempire.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Perl question...
Message-ID:  <20020201192621.348fcfe4.fxn@isoco.com>
In-Reply-To: <20020201095025.A79152@titus.lastamericanempire.com>
References:  <DC32C8CEB3F8D311B6B5009027DE5AD503D207F8@stlmail.dra.com> <20020201095025.A79152@titus.lastamericanempire.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 1 Feb 2002 09:50:25 -0700
z thompson <cublai@lastamericanempire.com> wrote:

: * Eric Six <erics@sirsi.com> [020201 09:00]:

: > 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

<snipped solution>

Recursion in a directory tree is somewhat tricky. It is generally
accepted that the best (and portable) way to accomplish that kind of
work in Perl is to delegate the recursion to the standard module
File::Find like this:

    # untested
    use File::Find;

    my $header = 'append this header to the top';

    find(\&callback, '/root/dir/one', '/root/dir/two');
   
    sub callback {
        return unless /some filter regexp/;
        open FILE, $_ or die $!;
	my $contents;
        {local $/; $contents = <FILE>;}
        close FILE;
        open FILE, ">$_" or die $!;
        print FILE $header, "\n";
        print FILE $contents;
        close FILE;
    }

-- fxn

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?20020201192621.348fcfe4.fxn>