From owner-freebsd-questions Fri Feb 1 8:45:12 2002 Delivered-To: freebsd-questions@freebsd.org Received: from titus.lastamericanempire.com (dsl081-101-239.den1.dsl.speakeasy.net [64.81.101.239]) by hub.freebsd.org (Postfix) with ESMTP id 5484E37B402 for ; Fri, 1 Feb 2002 08:45:09 -0800 (PST) Received: by titus.lastamericanempire.com (Postfix, from userid 1001) id 880AB23012; Fri, 1 Feb 2002 09:50:25 -0700 (MST) Date: Fri, 1 Feb 2002 09:50:25 -0700 From: z thompson To: Eric Six Cc: "'questions@FreeBSD.ORG'" Subject: Re: Perl question... Message-ID: <20020201095025.A79152@titus.lastamericanempire.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: ; from erics@sirsi.com on Fri, Feb 01, 2002 at 09:55:19AM -0600 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG * Eric Six [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 = ; 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