akamboj84 has asked for the wisdom of the Perl Monks concerning the following question:
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"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multiline Regex replacement in Multiline file
by Athanasius (Archbishop) on Sep 15, 2014 at 07:34 UTC | |
by akamboj84 (Novice) on Sep 16, 2014 at 01:11 UTC | |
by Athanasius (Archbishop) on Sep 16, 2014 at 03:57 UTC | |
by akamboj84 (Novice) on Sep 17, 2014 at 03:22 UTC | |
by choroba (Cardinal) on Sep 17, 2014 at 11:47 UTC | |
|
Re: Multiline Regex replacement in Multiline file
by Grimy (Pilgrim) on Sep 15, 2014 at 09:19 UTC | |
by Athanasius (Archbishop) on Sep 15, 2014 at 09:37 UTC | |
by akamboj84 (Novice) on Sep 16, 2014 at 01:19 UTC |