Maverick
Maverick

Reputation: 587

How to modify the .ini file in perl

I am trying to write a script which will modified 4 lines in InI file :

Below is the content of my Ini file:

gEnableImps=1
gEnableBmps=1
gEnableSuspend=3
gDot11Mode=0
gEnableHandoff=0
gRoamingTime=0 


Intf0MacAddress=000AF6798980
Intf1MacAddress=000AF5788981
Intf2MacAddress=000AF2898982
Intf3MacAddress=000AF5893983


InfraUapsdVoSrvIntv=0 
InfraUapsdViSrvIntv=0
InfraUapsdBeSrvIntv=0
InfraUapsdBkSrvIntv=0 


DelayedTriggerFrmInt=18000


gEnableFWRssiMonitoring=0  

I have to modify below four line every day :

Intf0MacAddress=000AF6798980
Intf1MacAddress=000AF5788981
Intf2MacAddress=000AF2898982
Intf3MacAddress=000AF5893983

In above lines I need to change 6798980, 5788981,2898982,5893983 part I mean in each number any to consecutive digit should get change and the change digit should not get copy of another number i.e in each number unique digit should change.

Exp :

6798980  after change -> 6791280
5788981  after change -> 5783481   
2898982  after change -> 2897682

I have written code but i am unable to change the number .. can any body give me some help

My Script:

#!/usr/bin/perl -w
use strict;
open(FH,"+<","WCN1314_qcom_cfg.ini")
                       or die "File not found";

my @lines=<FH>;
foreach my $line (@lines)
{
  if(($line =~ /Intf0MacAddress/ ||$line =~ /Intf1MacAddress/||$line =~ /Intf2MacAddress/||$line =~ /Intf3MacAddress/) )
{
  print "$line\n";
}  
else
{
  print "Not found\n";
}
}  

Upvotes: 1

Views: 1730

Answers (3)

Jeffrey Ray
Jeffrey Ray

Reputation: 1264

No need to re-invent the wheel by parsing and writing .ini files yourself. I would suggest using the Config::IniFiles or another similar module from CPAN . This code does what you need in just 3 lines.

my $cfg = Config::IniFiles->new( -file => $file, -fallback => 'GENERAL' );
$cfg->setval('GENERAL', 'Intf0MacAddress', '999999999999');
$cfg->RewriteConfig;

Upvotes: 4

synacker
synacker

Reputation: 1782

#!/usr/bin/perl -w
use strict;
open(FH,"+<","WCN1314_qcom_cfg.ini")
                       or die "File not found";
use 5.010;
my $file=join "", <FH>;

for (my $index = 0; $index < 4; $index++) {
    if ($file =~ m/(Intf($index)MacAddress=000AF)(\d{7})/) {
        my $number = $3;
        #Do what you need with number.
        $file =~ s/$&/Intf($index)MacAddress=000AF($number)/;
    }
}

open(FH,">","WCN1314_qcom_cfg.ini");
print FH $file;
close FH;

Upvotes: -1

choroba
choroba

Reputation: 241968

I do not understand what you want to do with the numbers. Can you try to explain in detail? Are you sure your example is correct? Can you explain why some digits changed and some did not?

Anyway, you can use something like the following:

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

my $file = 'WCN1314_qcom_cfg';
open my $OLD, '<', "$file.ini" or die $!;
open my $NEW, '>', "$file.new" or die $!;
my $digit = 1;
while (my $line = <$OLD>) {
    if (my ($num, $addr) = $line =~ /^Intf([0-3])MacAddress=000AF([0-9]+)/) {
        my @digits = split //, $addr;

        # Do what you need with the digits

        $line = "Intf${num}MacAddress=000AF" . (join q(), @digits) . "\n";
    }
    print {$NEW} $line;
}
close $NEW;
close $OLD;

if (-f "$file.bak") { unlink "$file.bak" or die "Cannot remove backup: $!"; }
rename "$file.ini", "$file.bak"          or die "Cannot backup.";
rename "$file.new", "$file.ini"          or die "Cannot create ini.";

Upvotes: 2

Related Questions