Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

compare two files | Help

by fnicholas (Initiate)
on Mar 23, 2012 at 14:27 UTC ( #961217=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: compare two files | Help
by JavaFan (Canon) on Mar 23, 2012 at 14:46 UTC
    Untested:
    use autodie; my %file1; my $max = 0; my $min = 0xFFFFFFFF; # Or 0xFFFFFFFFFFFFFFFFF with 64 bit ints open my $fh1, $file1; while (<$fh1>) { chomp; $min = length if $min > length; $max = length if $max < length; $file{$file1} = 1; } open my $fh2, $file2; LINE: while (<$fh2>) { for (my $l = $min; $l <= $max; $l++) { next LINE if $file{substr $_, 0, $l}; } print; }
Re: compare two files | Help
by ww (Archbishop) on Mar 23, 2012 at 14:43 UTC
    Discussed in Q&A, because this is a FAQ.

    Update: Caveat: depending on how "strictly" you are speaking, output which included everything in f2.txt ... or everything other than the bare name "Matt" (but including the modifiers to "Matt" in f2) could also be a correct answer.

    Also, since this smells strongly of 'homework', please note that we like to have such questions so labeled if that's the case.

      It is not a home work. this is for something I need on the job.
Re: compare two files | Help
by Anonymous Monk on Mar 23, 2012 at 15:54 UTC
    Fixed:
    #!/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 my $f1_fh, '<', $file1 or die "Could not open $file1: $!\n"; while (defined(my $line = <$f1_fh>)) { chomp $line; $results{$line} = (); } close $f1_fh; open my $output_fh, '>', $outfile or die "Cannot open $outfile for wri +ting: $!\n"; open my $f2_fh, '<', $file2 or die "Could not open $file2: $!\n" +; while (defined(my $line = <$f2_fh>)) { chomp $line; if (not exists $results{$line}) { print {$output_fh} $line, "\n"; } } close $f2_fh; close $output_fh;
Re: compare two files | Help
by Anonymous Monk on Mar 23, 2012 at 15:59 UTC
    Maybe next if $line =~ / /; #?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961217]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2020-11-27 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?