troubleshooting Question

Have a perl script, but need it to work on a whole directory not just one file name at a time

Avatar of richsark
richsarkFlag for United States of America asked on
PerlScripting Languages* BashShell Scripting
51 Comments1 Solution366 ViewsLast Modified:
Hello,  I have a perl script that I need it to look into a directory named mpl.  right now I run it but it does it for one file at a time. Sucks cause There are a few hundred. So If I can seek hellp to make my perl script open a whole directory and do what it does per one file but in a whole directory.

I need my script adjusted  to look into my directory mpl and read all the zonefiles and change the serial number using date with an extra number 2019011300 ; serial for example

I run the script like this on the below:

./zonechanges.pl db.apptoapp.net.new vipchange

Copy of the script:

#!/usr/bin/perl

open(ZONE,  $ARGV[0]) || die "Zone file error $!";
while ( $line = <ZONE> ) {
        chomp $line;
        push @fileLines, $line;
}
close ZONE;



# Assuming simple CSV of old_ip, new_ip
open(IPMAP, $ARGV[1]) || die "IP Map file error: $!";
while ( $line = <IPMAP> ) {
        chomp $line;
        ($old, $new) = split(/,/, $line);
        $old =~ s/\s+//;
        $new =~ s/\s+//;
        $ip{$old} = $new;
}
close IPMAP;


## For every line
for ($count=0; $count < @fileLines; $count++) {
#       print STDERR "OUTER FOR ($count)\n";
        $line = $fileLines[$count];
#       print STDERR "OUTER FOR have line $line\n";
        ## For every old address in the remap
        for $old (keys(%ip)) {
                #print STDERR "INNER FOR ($old)\n";
                ## If the line matches the old address
                if ( $line =~ /$old/ ) {
                        ($dnsName, @crap) = split(/\s+/, $line);
                        ## insert a new line with a new record or the new sites
                        ## if this is a site specific record
                        if ( $dnsName =~ m/\-mpl|\-jax/ ) {
                                $line =~ s/\-mpl/\-shk/;
                                $line =~ s/\-jax/\-str/;
                                $line =~ s/$old/$ip{$old}/;
                                push @fileLines, $line;

                        ## Othefwise, change the address to new
                        } else {
                                print STDERR "INNER FOR ELSE - line did not match $old\n";
                               $fileLines[$count] =~ s/$old/$ip{$old}/;
                        }
                }
        }
}

#i Dump the updates to STDOUT
foreach $line (@fileLines) {
        print $line."\n";
}
ASKER CERTIFIED SOLUTION
nociSoftware Engineer
Join our community to see this answer!
Unlock 1 Answer and 51 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 51 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros