#!/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.5v45c" ;'; 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 ;', ' }', '', '}' ];