954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparing Two Files in Perl

Hi All,

New to perl and looking for a solution.

I have two txt files as below

File 1:

dn: o=GEEVET REMEDIES,c=IN
objectClass: top
objectClass: organization
o: GEEVET REMEDIES

dn: o=K.G.RUBBER (INDIA) PRIVATE LIMITED,c=IN
objectClass: top
objectClass: organization
o: K.G.RUBBER (INDIA) PRIVATE LIMITED 

dn: o=SUDESH JAIN,c=IN
objectClass: top
objectClass: organization
o: SUDESH JAIN 

dn: o=CLASSIC COLORS AND CRAFTS,c=IN
objectClass: top
objectClass: organization
o: CLASSIC COLORS AND CRAFTS 

dn: o=VIJAY PRATISTHAN,c=IN
objectClass: top
objectClass: organization
o: VIJAY PRATISTHAN


File 2

dn: o=GURSUKH ENERGY INDIA PRIVATE LIMITED,c=IN
objectClass: top
objectClass: organization
o: GURSUKH ENERGY INDIA PRIVATE LIMITED 

dn: o=K.G.RUBBER (INDIA) PRIVATE LIMITED,c=IN
objectClass: top
objectClass: organization
o: K.G.RUBBER (INDIA) PRIVATE LIMITED 

dn: o=CLASSIC COLORS AND CRAFTS,c=IN
objectClass: top
objectClass: organization
o: CLASSIC COLORS AND CRAFTS 

dn: o=VARALAXMI INDUSTRIES,c=IN
objectClass: top
objectClass: organization
o: VARALAXMI INDUSTRIES


Both these files have some data that is common and some that is just one one of the files. I want to match data from both files and have data in a combined form.

The code that I used:

#!/usr/bin/perl
$f1 = '1.txt';
open FILE1, "$f1" or die "Could not open file \n";
$f2= '2.txt';
open FILE2, "$f2" or die "Could not open file \n";

$outfile = '3.txt';

my @outlines;

foreach (<FILE1>) 
{
	$y = 0;
	$outer_text = $_;

	seek(FILE2,0,0);

	foreach (<FILE2>) 
	{
		$inner_text = $_;
		if($outer_text eq $inner_text) 
		{
			$y = 1;
			#print "$outer_text, Match found \n";
			last;
		}
	}

	if($y != 1) 
	{
		#print "$outer_text,No Match Found \n";
		push(@outlines, $outer_text);
	}
}

open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n";
print OUTFILE @outlines;
close OUTFILE;

close FILE1;
close FILE2;


Actual Output that I Get:

dn: o=GEEVET REMEDIES,c=IN
o: GEEVET REMEDIES
dn: o=SUDESH JAIN,c=IN
o: SUDESH JAIN 
dn: o=VIJAY PRATISTHAN,c=IN
o: VIJAY PRATISTHAN


The Output that I NEED:

dn: o=GURSUKH ENERGY INDIA PRIVATE LIMITED,c=IN
objectClass: top
objectClass: organization
o: GURSUKH ENERGY INDIA PRIVATE LIMITED 

dn: o=GEEVET REMEDIES,c=IN
objectClass: top
objectClass: organization
o: GEEVET REMEDIES

dn: o=K.G.RUBBER (INDIA) PRIVATE LIMITED,c=IN
objectClass: top
objectClass: organization
o: K.G.RUBBER (INDIA) PRIVATE LIMITED 

dn: o=SUDESH JAIN,c=IN
objectClass: top
objectClass: organization
o: SUDESH JAIN 

dn: o=CLASSIC COLORS AND CRAFTS,c=IN
objectClass: top
objectClass: organization
o: CLASSIC COLORS AND CRAFTS 

dn: o=VARALAXMI INDUSTRIES,c=IN
objectClass: top
objectClass: organization
o: VARALAXMI INDUSTRIES

dn: o=VIJAY PRATISTHAN,c=IN
objectClass: top
objectClass: organization
o: VIJAY PRATISTHAN

As you can see that the actual output and the output that I need is missing out the below things.

objectClass: top
objectClass: organization

Please, HELP!!!

cmprmand
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: