Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi
I am parsing a pattern '<default>any text</default>' pattern in a file and wherever found, I want to replace that by '<default>enabled</default>'.
Now the problem is the above mentioned pattern may occur in a single line and in multiple line as well. So the input file may look something like

******* inputfile****************
this is line 1
this is line 2
<default>sometext</default>
this is line 3
<default>some more text
some more in next line
</default>
this is last line
*************************************
I am trying with this, which is working when the pattern is in single line but not for multiple line. The code for multiple line tried is commented out.

use strict; use warnings; use Tie::File; my $file = "inputfile"; my @array; tie @array , 'Tie::File', $file ; my $start = '<default>'; my $end = '</default>'; my $val = 'enabled'; my $replace = $start.$val.$end; print "replace $replace \n"; for (my $i = 0 ; $i <= $#array ; $i++) { #below if block is tried for multiple line pattern but not working # if ($array[$i] =~ /^<default>/../<^\/default>/) if($array[$i] =~ /^<default>/) #this is working { $array[$i] =~ s/^<default>.*<\/default>/$replace/g; } } untie @array;

seeking help on how can it be made to match both the patterns ?


In reply to parsing a multi-line pattern and replacing by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found