Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 files and i want to diff the two files such that i have 3 files at the end of the diff
1. UniqueDataInFile1

2. UniqueDataInFile2

3. CommonData
This is the code that i have come up with so far...it is not right and i am not clear on how i can iterate till eof of first file and look for the item in file1 in file2. Will appreciate any pointers, help here and no i am not trying to get someone to do my homework. TIA

my $infile1="infile1.txt"; my $infile2="infile2.txt"; open INFILE1, '<', $infile1 or die "could not open '$infile1' $!"; open INFILE2, '<', $infile2 or die "could not open '$infile2' $!"; open (FileA, ">DataFromFile1.txt") || die "Unable to open TextInFile1O +nly.txt for writing \n"; #open (FileB, ">DataFromFile2.txt") || die "Unable to open TextInFile1 +Only.txt for writing \n"; open (FileC, ">CommonDataFromTwoFiles.txt") || die "Unable to open Tex +tInFile2Only.txt for writing \n"; my @dataInFile1=<INFILE1>; my $val; my @ArrayA; my $last_element; while(my $line=<INFILE2>) { if ($line=~ m/$dataInFile1[0]/i) { print FileC $dataInFile1[0]; push @dataInFile1, shift @dataInFile1; } }

infile1
--------

yahoo

google

msn,/br.
abc

infile2
--------

yahoo

google

safeway

walmart

kmart

output1
--------

msn

abc

output2
--------

safeway

walmart

kmart

output3
--------
yahoo
google

Replies are listed 'Best First'.
Re: Diff 2 files and print output to file
by toolic (Bishop) on May 23, 2008 at 16:55 UTC
Re: Diff 2 files and print output to file
by almut (Canon) on May 23, 2008 at 17:10 UTC

    A typical approach would be to use a hash for such tasks. For example:

    • Read in file 1 and put every item into a hash (as key — e.g. $hash{$item} = $linenumber;)

    • Next, read file 2 and check for every item if it's found in the hash (if (exists $hash{$item}) ...). If found, write it to the file which will contain the common items, and set the value to zero ($hash{$item} = 0;) to mark it being common. Otherwise, write it to the file holding the items only found in file 2.

    • Lastly, write the items in the hash with values other than zero into the file holding the items only found in file 1. (Depending on whether you need the latter items to be in the original order, you might want to sort them by line number, or go through file 1 another time, or some such.)

    (The details may vary slightly depending on whether the same item can occur multiple times in one or both input files...)

Re: Diff 2 files and print output to file
by pc88mxer (Vicar) on May 23, 2008 at 16:55 UTC
    If this isn't a homework assignment, just use the Unix comm utility.