Monks:

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);

In reply to compare two files | Help by fnicholas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.