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 | |
|
Re: Diff 2 files and print output to file
by pc88mxer (Vicar) on May 23, 2008 at 16:55 UTC |