in reply to How to add a new line after three or four lines from a pattern after getting that pattern

This appears to do what you require:

#!/usr/bin/perl use strict; use warnings; if ( @ARGV != 1 ) { print "USAGE :: perl default_operating_condition_update.pl <<FILE> +>\n\n"; exit 1; } my $inputfile = $ARGV[ 0 ]; # input FILE mkdir 'finallib' unless -d 'finallib'; open my $INFILE, '<', $inputfile or die "Can not open '$inputfile' bec +ause: $!"; open my $OPFILE, '>', "finallib/$inputfile" or die "Can not open 'fina +llib/$inputfile' because: $!"; while ( my $iline = <$INFILE> ) { print $OPFILE $iline; if ( $iline =~ /^ ( \s* ) operating_conditions \s* \( \s* ( .* ) \ +s* \) \s* { \s* $/x ) { my ( $leading_space, $operating_cond ) = ( $1, $2 ); until ( $iline =~ /^ \s* } \s* $/x ) { $iline = <$INFILE>; print $OPFILE $iline; } print $OPFILE "${leading_space}default_operating_conditions : +$operating_cond ;\n"; } }
  • Comment on Re: How to add a new line after three or four lines from a pattern after getting that pattern
  • Download Code

Replies are listed 'Best First'.
Re^2: How to add a new line after three or four lines from a pattern after getting that pattern
by anirbanphys (Beadle) on Apr 03, 2019 at 19:48 UTC
    Hello jwkrahn,

    I got all the correct answer after posting my request. Yes all the MONKS had given me the right answer, but I really like to appreciate and thank you for doing the correction of my posted code and doing the modification in the accurate way. This is going to be an example for me to write the code for this kind of situation if I face again.

    Thank you :) for this great help