troubleshooting Question

find and replace using perl

Avatar of Vlearns
Vlearns asked on
Perl
8 Comments1 Solution244 ViewsLast Modified:
!/usr/local/bin/perl

use strict;

# change these as needed
my $dat_pre = 'imap_';
my $dat_ext = 'conf';
my $read_pre = 'mail_imap_auto_conf_';
my $read_ext = 'README';
my $read_str = "Version 1.5\n* [bug 4923372] Control setting via conf server - for all farms\n\n";

opendir DIR, '.' or die "could not open curr dir: $!";
my @dirs = grep { -d $_ } readdir DIR;
closedir DIR;

foreach my $dir (@dirs) {
    if (-f "$dir/$dat_pre$dir.$dat_ext") {
        my $fil = "$dir/$dat_pre$dir.$dat_ext";
        rename $fil, "$fil.bak" or die "could not backup $fil: $!";
        open IN, "$fil.bak" or die "could not read $fil.bak: $!";
        open OUT, ">$fil" or die "could not write $fil: $!";
        while (<IN>) {
            print OUT $_;
            print OUT qq($1adsOn: false,\n$Exclusionlist: "RIM",\n$1coreTimeout: 400,\n) if /(\s*)FetchOn: true,/
        }
        close OUT;
        close IN;
    } else {
        warn "$dir/$dat_pre$dir.$dat_ext does not exist - skipping $dir";
    }
    if (-f "$dir/$read_pre$dir.$read_ext") {
        my $fil = "$dir/$read_pre$dir.$read_ext";
        print ` cp $fil "$fil.bak" ` or die "could not backup $fil: $!";
        open IN, "$fil.bak" or die "could not read $fil.bak: $!";
        open OUT, ">$fil" or die "could not write $fil: $!";
        my $done;
        while (<IN>) {
            if ($done) {
                print OUT $_;
            } else {
                if (m{^\s*Version\b}) {
                    print OUT $read_str, $_;
                    $done++;
                } else {
                    print OUT $_;
                }
            }
        }
        close OUT;
        close IN;
    } else {
        warn "$dir/$read_pre$dir.$read_ext does not exist - skipping $dir";
    }
}

In the script

 print OUT qq($1adsOn: false,\n$Exclusionlist: "RIM",\n$1coreTimeout: 400,\n) if /(\s*)FetchOn: true,/

adds the  3 new entries after the (\s*)FetchO entry.

Would it be possible to add a feature that lets you replace an existing entry with a new one..

So if i add a new parameter like

my $replace_key = 'Exclusionlist';
my $replace_string = "Exclusionlist: "mynewvalue";

The program would find  every instance of the key Exclusionlist in every file and replace it with the new string assigned to replace_string



the data diles on which this script operates look like these

{
   map: {
      FIXME: {
         FetchOn: false,
         lightsaberYCA: "",
         adsOn: false,
         Exclusionlist: "NONE",
         coreTimeout: 400.0,
         lightsaberServer: "NONE"
      },

.....more entries like these .........

      "323": {
         FetchOn: true,
         adsOn: true,
         Exclusionlist: "RIM",
         coreTimeout: 400.0,
         lightsaberYCA: "ahoo.mprod",
         lightsaberServer: "lsahoo.com:4080"
      }

}

!/usr/local/bin/perl

use strict;

# change these as needed
my $dat_pre = 'imap_';
my $dat_ext = 'conf';
my $read_pre = 'mail_imap_auto_conf_';
my $read_ext = 'README';
my $read_str = "Version 1.5\n* [bug 4923372] Control setting via conf server - for all farms\n\n";

opendir DIR, '.' or die "could not open curr dir: $!";
my @dirs = grep { -d $_ } readdir DIR;
closedir DIR;

foreach my $dir (@dirs) {
    if (-f "$dir/$dat_pre$dir.$dat_ext") {
        my $fil = "$dir/$dat_pre$dir.$dat_ext";
        rename $fil, "$fil.bak" or die "could not backup $fil: $!";
        open IN, "$fil.bak" or die "could not read $fil.bak: $!";
        open OUT, ">$fil" or die "could not write $fil: $!";
        while (<IN>) {
            print OUT $_;
            print OUT qq($1adsOn: false,\n$Exclusionlist: "RIM",\n$1coreTimeout: 400,\n) if /(\s*)FetchOn: true,/
        }
        close OUT;
        close IN;
    } else {
        warn "$dir/$dat_pre$dir.$dat_ext does not exist - skipping $dir";
    }
    if (-f "$dir/$read_pre$dir.$read_ext") {
        my $fil = "$dir/$read_pre$dir.$read_ext";
        print ` cp $fil "$fil.bak" ` or die "could not backup $fil: $!";
        open IN, "$fil.bak" or die "could not read $fil.bak: $!";
        open OUT, ">$fil" or die "could not write $fil: $!";
        my $done;
        while (<IN>) {
            if ($done) {
                print OUT $_;
            } else {
                if (m{^\s*Version\b}) {
                    print OUT $read_str, $_;
                    $done++;
                } else {
                    print OUT $_;
                }
            }
        }
        close OUT;
        close IN;
    } else {
        warn "$dir/$read_pre$dir.$read_ext does not exist - skipping $dir";
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 8 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 8 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