Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could read the file in as a single string and then use a regular expression to do the search and replace:</p> <pre><code>use strict; use warnings; # Slurp file myfile.txt into a single string open(FILE,"myfile.txt") || die "Can't open file: $!"; undef $/; my $file = &lt;FILE&gt;; # Set strings to find and insert my $first_line = ")"; my $second_line = "NIC Hr_Nic ("; my $insert = "hello world"; # Insert our text $file =~ s/\Q$first_line\E\n\Q$second_line\E/$first_line\n$insert\n$second_line/; # Write output to output.txt open(OUTPUT,"&gt;output.txt") || die "Can't open file: $!"; print OUTPUT $file; close(OUTPUT); </code></pre> <p><a href="http://oreilly.com/catalog/cookbook/chapter/ch08.html" rel="nofollow noreferrer">By unsetting <code>$/</code> we put Perl into "slurp mode"</a> and so can easily read the whole file into <code>$file</code>.</p> <p>We use <a href="http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators" rel="nofollow noreferrer">the <code>s///</code> operator</a> to do a search and replace using our two search lines as a pattern.</p> <p>The <code>\Q</code> and <code>\E</code> tell Perl to escape the strings between them, i.e. to ignore any special characters that happen to be in <code>$first_line</code> or <code>$second_line</code>.</p> <p>You could always write the output over the original file if desired.</p> <p>The problem as you state it is not solvable using <a href="http://perldoc.perl.org/perlrun.html#Command-Switches" rel="nofollow noreferrer">the <code>-i</code> command line option</a> since this option processes a file one line at a time; to insert text between two specific lines you'll need to know about two lines at once.</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.
 

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