Dave Kettmann wrote:
Hi List,

Hello,

I know the number of files probably doesnt matter, but just in case
it does I thought I would mention it. What I need to do, is look thru
125 zone files and change the IPs. Or to be less specific, I need a
Find/Replace script that will run thru a list of files whose names
are in an array. Here is a snippet of code:

You can use perl's inplace edit variable to enable you to edit multiple files inplace.



#!/bin/perl

use strict;

my @files = (
  "zone.file1.com",
  "zone.file2.com",
  "zone.file3.com" )
                      ^
Missing semicolon ----|


for ( my $i = 0; $i < scalar @files; $i++ )

That is usually written as:

for my $i ( 0 .. $#files )

But you don't really need to array indexing to do this:

for my $file ( @files )


{
  open TMPFH, >>$files[$i];      #This is line 123 in the real file
^^
You are attempting to open the file for append (which is write-only to the end of the file) but since it isn't enclosed in quotes perl is interpreting it as the right shift operator. You should also verify that the file was opened correctly before attempting to use the filehandle.



while ( <TMPFH> ) {

You are trying to read from an invalid filehandle so the $_ variable will receive the undef value.



$_ =~ s/1\.1\.1\.1/5.5.5.5/ ;

Your regular expression isn't anchored so you may unintentionally change the wrong IP address.



    print $_ ;
  }
}

To do this with perl's inplace edit:

#!/bin/perl
use warnings;
use strict;

@ARGV = qw(
  zone.file1.com
  zone.file2.com
  zone.file3.com
  );

$^I = '.back'; # inplace edit variable. file names must be in @ARGV for this to work

while ( <> ) {
  s/\b1\.1\.1\.1\b/5.5.5.5/;
  print;
  }




John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to