Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:00:20+00:00 2026-05-16T07:00:20+00:00

the following script text.pl (described below) define to append the $insert text between $first_line

  • 0

the following script text.pl (described below) define to append the $insert text between $first_line and $second_line in the file – myfile.txt

While:

$first_line=A 
$second_line=B
$insert = "hello world"

for example

before test.pl running

A
B

After I run test.pl we get:

A
hello world
B

the problem: but if there line space between A line and B line then it doesn’t append the “hello world” as the following , what need to change in the script in order to append the $insert param also if I have in the file space line between A to B ?

A

B

test.pl script

use strict; 
use warnings; 

# Slurp file myfile.txt into a single string 
open(FILE,"myfile.txt") || die "Can't open file: $!"; 
undef $/; 
my $file = <FILE>; 

# Set strings to find and insert 
my $first_line = "A"; 
my $second_line = "B"; 
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,">output.txt") || die "Can't open file: $!"; 
print OUTPUT $file; 
close(OUTPUT); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:00:21+00:00Added an answer on May 16, 2026 at 7:00 am

    This would replace everything between Line 1 and 2 (even nothing) by insert:

    ...
    
    open my $in, '<', 'myfile.txt' or die "myfile.txt: $!";
    my $content = do { undef $/; <$in> };
    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/(?<=$first_line) .*? (?=$second_line)/\n$insert\n/xs; 
    
    # Write output to output.txt 
    open my $out, '>', 'output.txt' or die "output.txt: $!"; 
    print $out $content; 
    close $out;
    
    ...
    

    Edit/Addendum

    After reading your “enhanced specification”, 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):

    ...
    # 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{ (?<=^$first_line$)
                   (\s+) 
                   (?=^$second_line$)
                }xms;
    # Insert our text 
    $content =~ s/$expr/\n$insert\n/; 
    ...
    

    I created such a file:

    stuff
    stuff
    )
    NIC Hr_Nic (
    stuff
    stuff
    

    and it got inserted properly.

    Regards

    rbo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script : <script type=text/javascriptsrc = //www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/100080069921643878012/facebook.xml&amp;up_useNewFB_p=1&amp;up_showPopUp2_p=true&amp;synd=open&amp;w=320&amp;h=500&amp;title=Facebook&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js></script> to use the igoogle
I have following small script to preview some text before submitting it to store
The following is a script which empties the Username text field and the Password
The following code is having some problem with the jQuery. <script type="text/javascript"> $(window).load(function() {
The following script creates symlinks as expected, but the original file can never be
I have the following script from http://javascript.about.com/library/bljver.htm <script type=text/javascript> var jsver = 1.0; </script>
I have the following script: <script type=text/javascript> $(document).ready(function(){ document.write(<script type=\text/javascript\ src=\http://site.com/script.js\><\/script>); }); </script> When
I have the following script: <script type=text/javascript> $(document).ready(function(){ $(#ProjModelChoose).change(function() { $(#ProjModelChooseCOM).attr(name,form + $(this).val()); $(input[name='buyid']).attr(value,
I have the following script at the end of my page: <script type=text/javascript> jQuery.noConflict();
I have the following script which will change the background image of a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.