Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Multiline Regex replacement in Multiline file

by akamboj84 (Novice)
on Sep 15, 2014 at 23:56 UTC ( [id://1100671]=perlquestion: print w/replies, xml ) Need Help??

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 ww (Archbishop) on Sep 16, 2014 at 01:11 UTC

    This would be a far better question -- i.e., one that would be easier for us to help you with -- had you included some explanation of what's going wrong -- error messages; erroneous output (that is, output other than what you expected and wanted); computer begins to smoke; etc.

    My guess, however, is that perhaps you saw something like this (where <t>nn is perhaps 14, 15, or 16, depending on what your header (hashbang, strict, warnings, etc) contains.):

    Use of uninitialized value within %dic in substitution iterator at 110 +0671.pl line nn, <F> line 3. Use of uninitialized value within %dic in substitution iterator at 110 +0671.pl line nn, <F> line 3.

    If so, Perl is basicly telling you it ran out of input at line 3 of your input file... which makes a lot of sense since your sample shows only two lines in the input file.

    An ordinary way to deal with this is to read from F in a WHILE construct; other would be to test the read result for content (ie, len != 0 or similar).

    UPDATE: PS: put code tags around data, as well as around code.


    ++$anecdote ne $data


      Hi Mate, I am new to Perl Monks and posted my first question so now onwards will give more details. Thanks anyway for pointing out.

        But this wasn't your first question. It was the second question, although nearly a duplicate of your first one with the same title: Multiline Regex replacement in Multiline file. That question already received responses. Perhaps you could let us know how the new question relates to the answers you got the first time around, so that the help we provide is closer to what you need this second time around.

        The first one is better formatted (the sample data should be in code tags, just like the code is in code tags)... but that's because I edited it for you. This time it's your turn. ;) Writeup Formatting Tips.


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100671]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 19:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found