Reputation: 55
i would like to detect new service by comparing two files value. first file value :
FE_EquipmentConstantsSrv_vvvvv
FE_EquipRoutingSrv_vvvvv
FE_ETA_BETA_vvvvv_FrontEnd_GENERAL
FE_DTS_MTSPACE_vvvvv
FE_DTS_MTSPC_vvvvv
FE_DTS_Transfer_vvvvv
second file value:
FE_DTS_Transfer_1201.1
FE_ETA_BETA_1451.1_FrontEnd_GENERAL
FE_EquipRoutingSrv_1202.1
FE_EquipmentConstantsSrv_1203.1
FE_MESController_1202.1
FE_DTS_MTSPACE_1301.1
FE_DTS_MTSPC_1201.1
in this case, we will ignore version number example 1451.1 ,1202.1,1301.1. we only care alphabet. if second file value doesn't exist in first file, show new service detect by it's value. but my code doesn't check all in once and show value . I also not sure how can i ignore the version number in second file while checking the values.
use warnings;
use strict;
use threads;
my $firstFile="servicePattern.txt";
my $secondFile="Pattern1.txt";
if(-f $firstFile){
open(svrPatternFile, $firstFile) or die("Could not open file.");
open( my $fh6, '<', $secondFile) or die "Could not open file 'secondFile' $!";
foreach my $theServicePattern (<svrPatternFile>)
{
$theServicePattern=~ s/\r|\n|\f//g; ## remove end of line, white trailing space etc..
my $theServicePatternForDB=$theServicePattern;
foreach my $detectservice(<$fh6>)
{
$detectservice=~ s/\r|\n|\f//g;
my ($FE,$processID,$version) = split /_/, $detectservice;
my ($FE1,$processID1,$version1) = split /_/, $theServicePatternForDB;
if($processID =~ $processID1 )
{
print"no new service detected\n";
}
else
{
print"new service detected $processID\n";
}
}
}
}
Upvotes: 1
Views: 154
Reputation: 40718
Here is an example. I read the IDs from the first file into hash (after deleting the vvvvv
field), then compare each line in the second file (after deleting the version field) to the hash values of the first file:
use feature qw(say);
use strict;
use warnings;
{
my $ids = read_existing_service_ids();
my $fn = 'Pattern1.txt';
open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
while (my $line = <$fh>) {
chomp $line;
$line =~ s/_\d+\.\d+(_?)/$1/;
$line =~ s/^\s*//;
$line =~ s/\s*$//;
if (!exists $ids->{$line}) {
say "New service detected $line";
}
}
close $fh;
}
sub read_existing_service_ids {
my $fn = 'servicePattern.txt';
open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
my %ids;
while( my $line = <$fh> ) {
chomp $line;
$line =~ s/_vvvvv(_?)/$1/;
$line =~ s/^\s*//;
$line =~ s/\s*$//;
$ids{$line}++;
}
close $fh;
return \%ids;
}
Output:
New service detected FE_MESController
Upvotes: 1