To compare the content in two files..

R

Rider

Hi experts,

I have two files, each one with 100's of entries. Each line in each
file has two id's, seperated by a single.

I want to compare both files and need to print which ones are matching
and which ones are not matching.

I am looking out for a simple code that just compares the whole lines
in both files and throw the differences in another file,
non_matching.txt and matched ones in to matching.txt

Can some one suggest a simple way of achieving this?

Example:

file1.txt
+++++++++++
bsmith \t rmiller
rcarter \t bsmith
qyang \t rsingh

file2.txt
+++++++++++
bsmith \t rmiller
rcarter \4 sdung
qyang \t sreddy


matching.txt should be (only first id is required in the output, as it
is common in both both files and only the second id per line might
change in the second file).
+++++++++++++++++
bsmith
++++++++++++++++

non_matching.txt should be
++++++++++++++
rcarter \4 sdung
qyang \t sreddy
++++++++++++++
This is what I am thinking:
+++++++++++++++++++++++++
my %myconfig = (
input1 => 'file1.txt.',
input2 => 'file2.txt.',
matching => 'matching.txt',
non_matching => 'not_matchingtxt',
);

my %fields2;

{
open my $input, '<', $myconfig{input1} or die "Cannot open
'$myconfig{input1}': $!";

while ( <$input> )
{
.......
}
 
C

Chris

Have you seen the List::Compare module? It is good at providing the
intersection and difference between two arrays.
 
C

C.DeRykus

Hi experts,

I have two files, each one with 100's of entries. Each line in each
file has two id's, seperated by a single.

I want to compare both files and need to print which ones are matching
and which ones are not matching.

I am looking out for a simple code that just compares the whole lines
in both files and throw the differences in another file,
non_matching.txt and matched ones in to matching.txt

Can some one suggest a simple way of achieving this?

Example:

file1.txt
+++++++++++
bsmith \t rmiller
rcarter \t bsmith
qyang \t rsingh

file2.txt
+++++++++++
bsmith \t rmiller
rcarter \4 sdung
qyang \t sreddy

matching.txt should be (only first id is required in the output, as it
is common in both both files and only the second id per line might
change in the second file).
+++++++++++++++++
bsmith
++++++++++++++++

non_matching.txt should be
++++++++++++++
rcarter \4 sdung
qyang \t sreddy
++++++++++++++
This is what I am thinking:
+++++++++++++++++++++++++
my %myconfig = (
    input1       => 'file1.txt.',
    input2       => 'file2.txt.',
    matching     => 'matching.txt',
    non_matching => 'not_matchingtxt',
);

my %fields2;

{
    open my $input, '<', $myconfig{input1}  or die "Cannot open
'$myconfig{input1}': $!";

    while ( <$input> )
     {
       .......

}

For one approach, see:

Tie::File (included in core with current
distro's)

perlfaq4 ("How do I compute the difference
of two arrays?")
 
C

ccc31807

I want to compare both files and need to print which ones are matching
and which ones are not matching.
Can some one suggest a simple way of achieving this?

my (%first, %second);

open FIRST, '<', 'first.csv' or die "$!";
while (<FIRST>) { $first{$_}++; }
close FIRST;

open SECOND, '<', 'second.csv' or die "$!";
while (<SECOND>) { $second{$_}++; }
close SECOND;

open MATCH, '>', 'match.csv' or die "$!";
foreach my $key (keys %first)
{
if (exists $second{$key}) #row exists in both files/hashes
{
print MATCH "$key";
delete $first{$key};
delete $second{$key};
}
}
close MATCH;

open DIFF, '>' 'diff.csv' or die "$!";
foreach my $key (keys %first) { print DIFF "$key"; }
foreach my $key (keys %second) { print DIFF "$key"; }
close DIFF;

exit(0);
 
D

Dr.Ruud

I have two files, each one with 100's of entries. Each line in each
file has two id's, seperated by a single.

I want to compare both files and need to print which ones are matching
and which ones are not matching.

I am looking out for a simple code that just compares the whole lines
in both files and throw the differences in another file,
non_matching.txt and matched ones in to matching.txt

See diff(1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top