I am trying to match two patterns in a file and insert a line. If the patterns matche then insert a line after the second matching pattern line.
for example,
I have the following content in a file:
//This is my source file
I have to match for two patterns "pin" and "direction:inpu t" and then insert a line after the second pattern match.
The above code did not work. Could you tell me what's wrong this program?
for example,
I have the following content in a file:
//This is my source file
Code:
pin(name1) {
clock;
capacitance;
direction: input;
//this is where i want to insert a line
min_pulse;
}
pin(name2) {
clock;
capacitance;
direction: input;
//this is where i want to insert a line
max_pulse;
}
Code:
#!/usr/bin/perl -w
open(FILE, " <sdv.lib") ¦ ¦die "can not open this file\n";
open(OUTFILE,">out_sdv.lib") ¦ ¦die "can not create outfie";
$i=0;
$search="pin(.+){";
$second="direction(.+)input(.+)";
$add="max_tran: 500;\n";
@source= <FILE>;
while(@source){
if($source[$i]=~/$search/){
$j=$i;
for($i;$i <=$j+3;$i++){
if($source[$i]=~/$second/){
print OUTFILE $source[$i]." ".$add;
}
else{
print OUTFILE $source[$i];
}
}
}
else{
print OUTFILE $_;
}
$i++;
}
close(FILE);
close(OUTFILE);
Comment