LanX++ gave a good algorithm.

I am not sure if this is homework or not? If so, you should tell us.
However, I will give you some actual code.
I process text files frequently - Perl is great at this.
Skipping blank lines in the input is a normal "reflex reaction" by me and I show a common way to do that.

#!/usr/bin/perl use warnings; use strict; use Inline::Files; my %File1Hash; while (my $line = <FILE1>) { next if $line =~ /^\s*$/; # skip blank lines $line =~ s/\s*$//; # remove all trailing space, # including the line ending $File1Hash{$line}++; } while (my $line = <FILE2>) { next if $line =~ /^\s*$/; # skip blank lines my ($id) = split /\|/,$line; # get the first field print $line if exists $File1Hash{$id}; } =Prints COA213345|a|b|c| COA213345|a|b|c| =cut __FILE1__ COA213345 COA213345 COA213445 DOB213345 EOA213345 __FILE2__ COA213345|a|b|c| COA213345|a|b|c| LOA213345|a|b|c| kOB213345|a|b|c| LOA213345|a|b|c|
Update: I read more of the posts in this thread. If file 1 is 700K lines, this should work just fine on a modern computer. My ancient (now dead) XP laptop would have had some issues with a hash of that size due to memory issues. A modern 64 bit computer won't even blink. If there are issues, there are ways to reduce the memory footprint. Let's not go there unless it is necessary.

In reply to Re: Filtering Output from two files by Marshall
in thread Filtering Output from two files by vighneshmufc

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.