Hi sabas,

assuming you want to find the lines of file1 that are in file2 (that's what you say at the beginning of your post), perhaps something like this:

my %hash; open my $IN2, "<", $file2 or die "could not open $file2 $!"; while (my $line = <$IN2>) { chomp $line; next if $line =~ /^\s*$/; # skip empty line $hash{$line} = 1; } close $IN2; open my $IN1, "<", $file1 or die "could not open $file1 $!"; open my $OUT, ">", $file3 or die "could not open $file3 $!"; while (my $line = <IN1>) { chomp $line; next if $line =~ /^\s*$/; print $OUT "$line\n" if exists $hash{$line}; } close $IN1; close $OUT;
If you want to find the lines of file2 that are in file1 (as you seem to imply later in your post), then just swap file1 and file2.

In both cases, it will be pretty fast because a hash lookup is fast (much faster than scanning an entire array each time through the input loop). You could probably do it without the two chomps, but I feel it's a bit safer to have them.


In reply to Re: searching in large file by Laurent_R
in thread searching in large file by sabas

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.