Hello mao9856,

Just to add some minor ideas here on the answer of the fellow monk toolic. I would also add a regex to skip the blank lines with next, where it seems to exist on your file(s) with data.

Also I would change the die statements of close file to warn. For me it is not necessary to stop the whole script in case a file can not close but I would like to know it, this is why I would use warn and not die.

I would also suggest mao9856 to read this article Don't Open Files in the old way.

Sample of code including output based on all the minor modifications:

#!/usr/bin/perl use strict; use warnings; my (%patts, $f2_rec, $f2_field); my $f1 = $ARGV[0]; my $f2 = $ARGV[1]; open(my $fh1,"<", $f1) or die "Failled to open '$f1' $!"; while (<$fh1>) { chomp; next if /^\s*$/; $patts{substr $_, 0, 3} = 1; } close($fh1) or warn "Failled to close '$f1' $!"; open(my $fh2,"<", $f2) or die "Failled to open '$f2' $!"; while (defined ($f2_rec = <$fh2>)) { chomp $f2_rec; next if $f2_rec =~ /^\s*$/; $f2_field = (split(/ /,$f2_rec))[1]; $f2_field = substr $f2_field, 0, 3; if(exists($patts{$f2_field})) { print "$f2_rec\n"; } } close($fh2) or warn "Failled to close '$f2' $!"; # update changing open to clo +se thank to Laurent_R for pointing out __END__ $ perl test.pl file1.txt file2.txt ID121 ABC14 ID122 EFG87 ID145 XYZ43 ID157 TSR11 ID181 ABC31 ID529 EFG56 ID684 TSR07

Hope this helps, BR.

Update: Thanks to fellow monk Laurent_R for noticing a typo I have update the sample of code.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: print all data matching identical three alphabets from two different files by thanos1983
in thread print all data matching identical three alphabets from two different files by mao9856

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.