fnicholas has asked for the wisdom of the Perl Monks concerning the following question:
Please I need your help with the attached script (below). I am trying to compare two files (f1.txt, f2.txt) and output data that is not present in f1.txt but present in f2.txt to an output file -of.txt.
==> f1.txt <== Francis Josh Matt Sean ==> f2.txt <== Francis Sean Matt Martine Josh ==> of.txt <== Martine
The attached script(below) work just fine. The problem that i have is how do I update my script to achieve the following result below. I know I can use regular expression but I have been unsuccessful.
==> f1.txt <== Francis Josh Matt Sean ==> f2.txt <== FrancisApp1 application program Sean128 name number Matt Po125 Hai Martine Josh393 daily ==> of.txt <== Martine
I look forward to hearing from you. Thanks, Francis
#Program: compareTwoFiles.pl #The program compares two files and output the data in File2(f2.txt) #that is not present in File1(f1.txt) into OUTFILE(ot.txt) #!/usr/bin/perl use strict; use warnings; #declaration of variables my $file1 = '/opt/programming/work/f1.txt'; my $file2 = '/opt/programming/work/f2.txt'; my $outfile = '/opt/programming/work/of.txt'; my %results = (); open FILE1, "$file1" or die "Could not open file: $! \n"; while (my $line = <FILE1>){ chomp($line); $results{$line} = 1; } close (FILE1); open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing: +$! \n"; open FILE2, "$file2" or die "Could not open file: $! \n"; while (my $line = <FILE2>){ chomp($line); print (OUTFILE $line, "\n") unless defined($results{$l +ine}); } close (FILE2); close (OUTFILE);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: compare two files | Help
by JavaFan (Canon) on Mar 23, 2012 at 14:46 UTC | |
|
Re: compare two files | Help
by ww (Archbishop) on Mar 23, 2012 at 14:43 UTC | |
by fnicholas (Initiate) on Mar 23, 2012 at 18:04 UTC | |
|
Re: compare two files | Help
by Anonymous Monk on Mar 23, 2012 at 15:54 UTC | |
|
Re: compare two files | Help
by Anonymous Monk on Mar 23, 2012 at 15:59 UTC |