Hi Guys! I am very new to perl
I have similar kind of problem where I need to compare 2 files , lets say :
file1 contents=
M|_|03-04-2012_08:21:03|_|Device1|_|8|_|Int/0.101|_|
M|_|03-04-2012_08:01:05|_|Device1|_|8|_|Int/0.101|_|
C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router
P|_|04-04-2012_12:37:14|_|Device4_|CPU|_|CPU|
File2 contents are :
C|_|09-03-2011_06:49:52|_|Device5|_|cpu-1|_|cpu-1|
P|_|04-04-2012_12:37:14|_|Device4|_|CPU|_|CPU
C|_|09-03-2011_06:49:52|_|Device3|_|192.165.161.41^150^router|_|192.165.161.41^150^router
I have some conditions which I need to chcek and then filter desired o/p , those conditions are as below :
if(($array1[0]=~/c/i) && ($array2[0]= ~/c/i))
{
if ($arra1[2]=array2[2] && array1[4]=array2[4])
{
don't add anything
}
else
{
append matched line to file2
}
}
elsif(($array1[0]=~/M/i) && ($array2[0]= ~/M/i))
{
if ($arra1[2]=array2[2] && array1[4]=array2[4])
{
don't add anything
}
else
{
append matched line to file2
}
}
I tried to apply this logic in below code but not working as expected , please help !
#!/usr/local/bin/perl
use strict;
my @array1;
my @array2;
#my $count=0;
my $i=0;
open (OUTFILE ,">file2.dat") || die "can't create" ;
open (FILE1,"file1.dat");
while(my $value1=<FILE1>)
{
my $i= $i +1 ;
@array1=split('\|_\|',$value1);
chomp($value1);
my $dev1=$array1[2] ;
my $attribute1=$array1[4];
my $profile1=$array1[0];
open (FILE2, "file2.dat");
while (my $value2=<FILE2>)
{
@array2=split('\|_\|',$value2);
chomp($value2);
my $dev2=$array2[2] ;
my $attribute2=$array2[4];
my $profile2=$array2[0];
if(($profile1 = ~/C/i) and ($profile2 = ~/C/i))
{
if(($dev1 = $dev2) && ($attribute1 = $attribute2))
{
$i ++ ;
}
else
{
print OUTFILE "$value1" ;
}
}
else
{
if(($profile1 = ~/M/i) and ($profile2 = ~/M/i))
{
if(($dev1 = $dev2) && ($attribute1 = $attribute2))
{
$i ++ ;
}
else
{
print OUTFILE "$value1" ;
}
}
}
}
}
close FILE1;
close FILE2;
close OUTFILE;
}