Hello Experts, I am kind of stuck in code. I am trying to do multi regex replacement in multiline file. However my scripts is unable to do replacement, I am not able to figure out why? Can someone help me please

->>Script Code

my $def="hashpatterns.txt"; my %dic=(); open(D, $def) || die "can't open definition file:$def\n"; while (<D>) { my ($oldp, $newp) = split /#/; $dic{$oldp}=$newp; } close(D); my $file="input.txt"; open(F, $file) || die "can't open definition file:$file\n"; open(W, '>out.txt') or die "can't write to file:$!\n"; my $line=join "", <F>; my $matchkey=join "|", keys %dic; $matchkey=qr /$matchkey/; $line =~ s%$matchkey%$dic{$matchkey}%g; print W $line; close(F);

--> hashpatterns.txt

\s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+console){2}(\ +s+new-password-at-login)?(\s+member\s"(default|engineer|networktest)" +){2}(\s+exit){0,2}#REPLACE1

\s+user\s[^"]+"\s+password\s[^"]+"\s+hash2\s+access(\s+console){2}(\s+ +new-password-at-login)?(\s+member\s"(default|READ-ONLY)"){2}(\s+exit) +{0,2}#REPLACE2

\s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+(console|snmp +|li)){3}\s+console(\s+new-password-at-login)?(\s+member\s"(default|LI +|li-prof1)"){2}(\s+exit){0,2}#REPLACE3

-->input.txt

user "testuser1" password "08Cl3V.leJKU/GskqArA0Yp4MFo" hash2 access console console new-password-at-login member "default" member "engineer"

user "v-test" password "VCp0GjSBK/KiWW.PgkQp7swXVMZ" hash2 access console console new-password-at-login member "default" member "READ-ONLY"


In reply to Multiline Regex replacement in Multiline file by akamboj84

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.