Perl compare two file and print the matching lines?

This isn't the cleanest way to do things... but the hard work has been done. Reverse the logic to make it print everything unless $results{$line} == 1 or if $results{$line}! = 1 .

Up vote 1 down vote favorite share g+ share fb share tw.

I have this script which is compare 2 files and print out the diff result. Now I want to change the script instead of print out the diff lines, I want to print the matching lines. And also to count how many time matched every time running the script.

Would you please any one can give me a suggestion. Thanks! #!

/usr/local/bin/perl # compare my $f1 = "/opt/test. Txt"; my $f2 = "/opt/test1. Txt"; my $outfile = "/opt/final_result.

Txt"; my %results = (); open FILE1, "$f1" or die "Could not open file: $! \n"; while(my $line = ){ $results{$line}=1; } close(FILE1); open FILE2, "$f2" or die "Could not open file: $! \n"; while(my $line =) { $results{$line}++; } close(FILE2); open (OUTFILE, ">$outfile") or die "Cannot open $outfile for writing \n"; foreach my $line (keys %results) { print OUTFILE $line if $results{$line} == 1; } close OUTFILE; perl perl-module link|improve this question asked Jul 28 '11 at 16:04eli103.

This isn't the cleanest way to do things... but the hard work has been done. Reverse the logic to make it print everything unless $results{$line} == 1, or if $results{$line}! = 1.

To add the count: print OUTFILE "Count: $results{$line} - $line" if $results{$line}! = 1; Alternatively, you could filter out the unwanted with a grep, avoiding the if condition totally: foreach my $line ( grep { $results{$_}! = 1 } keys %results ) { print OUTFILE "Count: $results{$line} - $line"; }.

Thanks a lot your answer too full fill my primary objective but not the second one. I think I wasn't clear enough. Sorry about that.

I want the counter to tell me how many times they are matched. Example. The script run every week so the counter adding 1 number every run.

So let say after 4 weeks if I see '4' next to the line that means the device out there 4 weeks and if the second line match 3 times that means the device there for 3 weeks and so on. Simply my objective is to know for how many weeks each devices are matched. – eli Jul 28 '11 at 17:00.

Print OUTFILE $line if $results{$line} == 1; This will print lines that occur only one time. Print OUTFILE $line if $results{$line} > 1; One small change (== to >), and it will now print lines that occur more than one time. That should print identical duplicate lines.

Oh, also if you want the count, simply do: if ( $results{$line} > 1 ) { print OUTFILE "$results{$line}: ", $line; } I wrote a more concise and more flexible version here. It takes optional filenames and prints to STDOUT. You can put 0 in place of one of the names to compare one of the files against another.

Use shell redirection to save it to a file. Usage: $ script. Pl file1.

Txt file2. Txt > outfile. Txt Code: use strict; use warnings; use autodie; my $f1 = shift || "/opt/test.

Txt"; my $f2 = shift || "/opt/test1. Txt"; my %results; open my $file1, ') { $results{$line} = 1 } open my $file2, ') { $results{$line}++ } foreach my $line (sort { $results{$b} $results{$a} } keys %results) { print "$results{$line}: ", $line if $results{$line} > 1; }.

Thank you so very much! My primary objective is met. My secondary objection is the device matched should be removed from the file.

So I want the counter to tell me how many times they are matched. Example. The script run every week so the counter adding 1 number every run.

So let say after 4 weeks if I see '4' next to the line that means the device out there 4 weeks and if the second line match 3 times that means the device there for 3 weeks and so on. Simply my objective is to know for how many weeks each devices are matched. – eli Jul 28 '11 at 16:58 1 I'm not quite sure what you are asking here and how it is different from what you already have.

It is generally speaking better to ask for all the objectives at once here at StackOverflow, rather than trying to puzzle together the solution piece by piece. I think what you are asking for requires a new question, preferably with some sample input/output. – TLP Jul 28 '11 at 17:05 the counter on you solution show how many items are matched my objective is the counter for how long it's matched.

The counter on your solution show me "2" even though I did run the script 10 times. My expectation was to show me "10" since the script run 10 times and matched the current list. Sorry about the confusion but this was my original objective I am not added new objective.

Also English is my 3rd language so take that for consideration! – eli Jul 28 '11 at 17:15.

Try Test::Differences. See here for code sample and how the output would look like: search.cpan.org/~ovid/Test-Differences-0....

This will print lines that occur only one time.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions