Hello akamboj84, and welcome to the Monastery!

You first create a dictionary mapping each search regex to its corresponding replacement text. Then, before searching, you change the search regex in two ways: (1) by concatenating all the search strings with |; (2) by applying the qr operator. So when you get to the substitution, if a match is found the dictionary lookup $dic{$matchkey} is guaranteed to fail, since the new value of $matchkey does not match any of the keys in the %dic hash!

But even when this problem has been fixed,1 you will still be replacing the whole of the first user entry, including everything through to the final "engineer", with the single text REPLACE1. I doubt this is what you want. For the monks to help you further, you will need to specify the output you expect/desire.

Update: 1For example:

use strict; use warnings; my @regexen = ( q{\s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+console +){2}} . q{(\s+new-password-at-login)?} . q{(\s+member\s"(default|engineer|networktest)"){2}(\s+exit){0,2}}, q{\s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access(\s+console +){2}} . q{(\s+new-password-at-login)?} . q{(\s+member\s"(default|READ-ONLY)"){2}(\s+exit){0,2}}, q{\s+user\s"[^"]+"\s+password\s"[^"]+"\s+hash2\s+access} . q{(\s+(console|snmp|li)){3}\s+console(\s+new-password-at-login)?} +. q{(\s+member\s"(default|LI|li-prof1)"){2}(\s+exit){0,2}}, ); my $n = 1; my %dic = map { qr{$_} => 'REPLACE' . $n++ } @regexen; my $line = join "", <DATA>; for my $matchkey (keys %dic) { $line =~ s%$matchkey%$dic{$matchkey}%g; } print $line; __DATA__ 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"

Output:

18:42 >perl 1011_SoPW.pl REPLACE1REPLACE2 18:43 >

Note that I had to add two doublequote characters to the second regex to get it to match.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Multiline Regex replacement in Multiline file by Athanasius
in thread 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.