I have two files, File1 and File2. I would like to compare lines between these two files and print. However I do not find a way to match specific string. I tried the coding below, but it prints out unexpected result.

I would like to print File2 data (eg. b05ldt10ud0e0) when it matches to File1's names(eg. ldt). For those data with asterisk * in File1 (eg. b05can03*n0b5), * could be any character. For matching, It must match head and tail of an * in File2.Thus, it will print out b05can03un0b5. And printing must be follow the sequence of File1. Could anyone give me advice on this?

File1

--------------------------

ldt

b05dcc00

mny

b05can03*n0b5

b05mdd04*n9c9

File2

---------------------------

/* To start: b05afn10ud0b0 */

/* To start: b05dcc00ud0c0 */

/* To start: b05ldt10ud0e0 */

/* To start: b05dcc10ud0i0 */

/* To start: b05afn10ud0m0 */

/* To start: b05afn10ud0s0 */

/* To start: b05mny00ud0b5 */

/* To start: b05mny00ud0d3 */

/* To start: b05mdd04un9c9 */

/* To start: b05ahn00ud0j5 */

/* To start: b05mny00ud0m7 */

/* To start: b05can03un0b0 */

/* To start: b05can03un0b5 */

Coding:

-----------------------

my (@arr1,@arr2); @arr1=<File1>; @arr2=<File2>; foreach my $line1 (@arr1) { foreach my $line2 (@arr2){ if ($line1 =~ $line2 && $line1 !~ m/^\w+(\W)\w+(.*)/) { print "$line2\n";` } } }

expected result:

b05ldt10ud0e0

b05dcc00ud0c0

b05mny00ud0b5

b05mny00ud0d3

b05mny00ud0m7

b05can03un0b5

b05mdd04un9c9


In reply to How to match specific character in a string? by WWq

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.