Hello anirbanphys,

One possible way is:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use feature 'say'; my @lines = io('file.txt')->chomp->slurp; print Dumper \@lines; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # Insert at position 12, replace 0 elements. splice @lines, $i + 5, 0, 'default_operating_conditions : "AB0.5v4 +5c" ;'; last; # break loop } next; } print Dumper \@lines; __END__ $ perl test.pl $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ]; $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', 'default_operating_conditions : "AB0.5v45c" ;', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ];

Update: I have hard-coded the data but you can do the parsing. My approach is to read the file in an array so you can know the exact position of the line that you want to alter. Next step insert a new line without altering the array using splice.

Sample of code to split string and insert it:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use feature 'say'; my @lines = io('file.txt')->chomp->slurp; print Dumper \@lines; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, "default_".$elements[1]." : ".$elements[2].""; last; # break loop } next; } print Dumper \@lines; __END__ $ perl test.pl $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ]; $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', 'default_operating_conditions : "AB0.5v45c" ;', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ];

Update 2: All the commends together in one solution:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('file.txt')->chomp->slurp; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, " default_".$elements[1]." : ".$elements[2].""; last; # break loop } next; } # print Dumper \@lines; io('out.txt')->appendln($_) for @lines; __END__ $ perl test.pl $ cat out.txt library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } default_operating_conditions : "AB0.5v45c" input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }

Update 3: Adding a better (more strict) regex to the solution:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('file.txt')->chomp->slurp; for my $i (0 .. $#lines) { if ($lines[$i] =~ m/\s\soperating_conditions*/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, " default_".$elements[1]." : ".$elements[2]." ;"; last; # break loop } next; } # print Dumper \@lines; io('out.txt')->appendln($_) for @lines; __END__ $ perl test.pl $ cat out.txt library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } default_operating_conditions : "AB0.5v45c" ; input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: How to add a new line after three or four lines from a pattern after getting that pattern by thanos1983
in thread How to add a new line after three or four lines from a pattern after getting that pattern by anirbanphys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.