×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

File comparision

File comparision

File comparision

(OP)
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.

RE: File comparision

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.

RE: File comparision

I cannot now use a database to store the contents of these files. Is there a way in Perl?

RE: File comparision

Try something like this.


# 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,"file1") || die;
while(<f>){
    chomp;
    unless ($file_two($_)) {
        print "$_\n";
    }
}
close f;
exit 0;

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.

RE: File comparision

(OP)
MikeLacey,

It works!.

Thanks,

RE: File comparision

<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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close