Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This would replace everything between Line 1 and 2 (even <strong>nothing</strong>) by insert:</p> <pre><code>... open my $in, '&lt;', 'myfile.txt' or die "myfile.txt: $!"; my $content = do { undef $/; &lt;$in&gt; }; close $in; # Set strings to find and insert my $first_line = quotemeta 'A'; my $second_line = quotemeta 'B'; my $insert = 'hello world'; # Insert our text $content =~ s/(?&lt;=$first_line) .*? (?=$second_line)/\n$insert\n/xs; # Write output to output.txt open my $out, '&gt;', 'output.txt' or die "output.txt: $!"; print $out $content; close $out; ... </code></pre> <hr /> <p><strong>Edit/Addendum</strong></p> <p>After reading your <a href="https://stackoverflow.com/questions/3458194/sed-or-perl-inserting-text-between-two-lines">"enhanced specification"</a>, its much clearer how to solve this. You include the Start (^) and End ($) of the lines into the regular expression. In order to keep this maintainable, I did take out the expression and made a variable of it. I tested it and it seems to work (even with '(') and stuff):</p> <pre><code>... # modified part # Set strings to find and insert my $first_line = quotemeta ')'; my $second_line = quotemeta 'NIC Hr_Nic ('; # you won't need an array here, just write the lines down my $insert = 'haattr -add RVG StorageRVG -string haattr -add RVG StorageDG -string haattr -add RVG StorageHostIds -string haattr -delete RVG Primary haattr -delete RVG SRL haattr -delete RVG RLinks'; my $expr = qr{ (?&lt;=^$first_line$) (\s+) (?=^$second_line$) }xms; # Insert our text $content =~ s/$expr/\n$insert\n/; ... </code></pre> <p>I created such a file:</p> <pre><code>stuff stuff ) NIC Hr_Nic ( stuff stuff </code></pre> <p>and it got inserted properly.</p> <p>Regards</p> <p>rbo</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload