Dravidan has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, How can i wrire an output of a variable ( which contain more than 200 lines) to immediate next line of a file after detecting particular line. For example I want to wrire the variable contents to the target file after detecting the line </TARGET>,consider this line don't have any other value than </TARGET>, and the line number will not be always same for the line </TARGET>.

  • Comment on How to write at immediate next line of detecting a particular pattern

Replies are listed 'Best First'.
Re: How to write at immediate next line of detecting a particular pattern
by Random_Walk (Prior) on Jan 26, 2014 at 10:29 UTC

    You will read your file line at a time, and write the results to a new file. When you hit your pattern in the input stream, write your 200+line variable. Once this is done you can contiue reading and adding the rest of the original file, either line at a time, or by undefining the input field seperator: $/, as one block. Finally copy the new output file over the original

    As the tag <TARGET> looks rather like an XML or HTML like thing, perhaps you should look at proper *ML parsers, to acheive your aim more reliably

    When you have some code do post it, and we will be glad to help further

    # pseudo code that will not run unless you do some homework... use strict; use warnings; my $big_var = "Some massive\nstring\n"; # that big 200 line var goes h +ere my $pattern = "<TARGET>"; open my $input input file open my $output output file while read line from input { print to output if pattern is matched { print $output $big_var } } close both files copy output over input

    PS What will happen if TARGET tag is not on the end of a line?

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: How to write at immediate next line of detecting a particular pattern
by Anonymous Monk on Jan 26, 2014 at 08:42 UTC
    "wire" is generally not a verb in the programmers vocabulary -- what does it mean?