Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File comparision

Status
Not open for further replies.

madhusn

Programmer
Joined
Jul 2, 2001
Messages
7
Location
US
Hi,

I need to check the contents of two files line by line.
For example:
File1 contains the following
Jim@tek.com
Tom@tek.com
Tim@tek.com

File2 contains:
Sam@tek.com
oprah@tek.com
Tim@tek.com
Tom@tek.com

I need to compare first File1 with File2 and find if there is any thing in File1 that is not in File2.
In this example, we've Jim@tek.com which is not in File2.
Then Jim@tek.com needs to be written to a DELETE file.

The, i need to compare File2 with File1 to see if there's anything in File2 not found in FIle1.
In this example, we've Sam@tek.com & oprah@tek.com.
Both these entries should be writtento another file called ADD.

Is there a way to accomplish this in Perl?

Thanks for any help.

 
You can do it with perl, but I would suggest using mysql or another database. This will be much easier to do with it, using a join statement.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
I cannot now use a database to store the contents of these files. Is there a way in Perl?
 
Try something like this.

[tt]
# read file 2 into an hash array
open(F,"file2") || die;
while(<f>){
chomp;
$file_two{$_)=1;
}
close f;

# now work through file1 searching through
# file2's hash array and printing out lines
# from file1 not in file2
open(F,&quot;file1&quot;) || die;
while(<f>){
chomp;
unless ($file_two($_)) {
print &quot;$_\n&quot;;
}
}
close f;
exit 0;
[/tt]

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
MikeLacey,

It works!.

Thanks,
 
<grin> working programs are the only kind I write madhusn...

(actually, technically speaking -- that's a lie)

Glad you're sorted -- and we'll see you back here I hope? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top