Hello sabas,
Welcome to the Monastery. The fellow Monks have already suggested solutions to your question. I just wanted to add one more by using a module.
A few days ago a similar question was asked find common data in multiple files. I think the best approach is to use the module setop to compare the files and to capture output easily you can do it with IPC::System::Simple.
Sample of code below:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use IPC::System::Simple 'capture';
my $cmd = "setop --intersect " . join " ", @ARGV;
chomp (my @output = capture( $cmd ));
my %hash;
$hash{$ARGV[1]} = \@output;
print Dumper \%hash;
__END__
$ perl test.pl file1.txt file2.txt
$VAR1 = {
'file2.txt' => [
'Sample line 1',
'Sample line 2',
'Sample line 3',
'Sample line 4',
'Sample line 5',
'Sample line 6',
'Sample line 7',
'Sample line 8',
'Sample line 9',
'Sample line 10'
]
};
I do not know if this module is the fastest or the more efficient solution but you can Benchmark.
Input data that I used to replicate your question, file1.txt
Sample line 1
Sample line 2
Sample line 3
Sample line 4
Sample line 5
Sample line 6
Sample line 7
Sample line 8
Sample line 9
Sample line 10
Input data that I used to replicate your question, file2.txt
Sample line 1
Sample line 2
Sample line 3
Sample line 4
Sample line 5
Sample line 6
Sample line 7
Sample line 8
Sample line 9
Sample line 10
Sample line 11
Sample line 12
Sample line 13
Sample line 14
Sample line 15
Sample line 16
Sample line 17
Sample line 18
Sample line 19
Sample line 20
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.