在某些特殊情况下需要从删除中保存一行

时间:2019-03-26 08:24:04

标签: regex perl

我写了一个perl代码,当满足“ direction:output”条件时,它可以从包含“ max_transition”的文件(IN_FILE)中搜索和删除一行。删除该行后,代码将修改后的文件写入其他位置。

现在,当满足“方向:输出”条件以及在该特定引脚组中找到“时序”的位置时,我需要删除“ max_transition”行。

IN_FILE,具有输出引脚“ HIZIBI_79”,HIZIBI_78和HIZIBI。运行脚本后,不应从引脚“ HIZIBI_79”,HIZIBI_78”中删除“ max_transition”行,因为该引脚组没有“ timing”。只有“ max_transition”行将从“ HIZIBI”引脚中删除,它具有“时间”组。

有什么想法要实现吗?

输出文件应类似于OUT_FILE

use warnings;
use strict;
my $inputfile = $ARGV[0]; # input lib FILE

if ($#ARGV!=0)
{
  print "USAGE :: perl max_tran_update.pl <<LIB_FILE>>  \n\n" ;
  exit(1);   
}

my $cmd = "mkdir  tmpdir;";
system ($cmd);

my $iline;
my $flag_outpin = 0;
my $out_pin_flag = 0;

open (INFILE,"<","$inputfile") || die "Can not open Input LIB File";
open (my $OPFILE,">","tmpdir/input_lib.lib") || die "Can not open Input Text File";

while ($iline = <INFILE>)
{
  chomp $iline;
  print $OPFILE "$iline\n";
  if (($iline =~m/^\s*direction\s*:\s*output\s*;/g))
  {
    $flag_outpin=1;
    while ($iline = <INFILE>)
    {
      if (($iline =~m/^\s*direction\s*:\s*input\s*;/g))
      {
        $flag_outpin=0;
      }
      if (($iline =~m/^\s*direction\s*:\s*output\s*;/g))
      {
        $flag_outpin=1;
      }
      if (($iline =~m/^\s*max_transition\s*:/g) && ($flag_outpin == 1))
      {
        $iline =~ s/$iline//g ;
      }
      else 
      {
        print $OPFILE "$iline";
      }
    }
  }
}      
close INFILE;
close $OPFILE;

IN_FILE

  cell (lib_1) {
    dont_use : true ;
    dont_touch : true ;
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 12 ;
    }  
    pin ("HIZIBI_79")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 3 ;
    }              
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 1 ;
    }  
    pin ("HIZIBI_78")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 34 ;
      capacitance : 34 ;
    }            
    pin ("HIZIBI")    {
      direction : output ;
      clock : true ;
      max_transition : 20;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  
      }  
    }
  }

OUT_FILE


  cell (lib_1) {
    dont_use : true ;
    dont_touch : true ;
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 12 ;
    }  
    pin ("HIZIBI_79")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 3 ;
    }              
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 1 ;
    }  
    pin ("HIZIBI_78")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 34 ;
      capacitance : 34 ;
    }            
    pin ("HIZIBI")    {
      direction : output ;
      clock : true ;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }  
      }  
    }
  }

2 个答案:

答案 0 :(得分:1)

尝试使用此命令行Perl

 perl -0777 -ne ' s/(pin\s*\(".+?"?\)\s+\{.+?\})/$x=$1;if($x=~m!timing\s*\(\)! and $x=~m!direction : output!){$x=~s!^\s*max_transition.+?\n!!mg};$x/gse ; print ' anny_in.txt

结果:

 cell (lib_1) {
    dont_use : true ;
    dont_touch : true ;
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 12 ;
    }
    pin ("HIZIBI_79")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 3 ;
    }
    pin ("HIZIBI_IN_1") {
      direction : input ;
      clock : true ;
      max_transition : 1 ;
      capacitance : 1 ;
    }
    pin ("HIZIBI_78")    {
      direction : output ;
      max_transition : 10;
      min_capacitance : 34 ;
      capacitance : 34 ;
    }
    pin ("HIZIBI")    {
      direction : output ;
      clock : true ;
      related_power_pin : VDD ;
      related_ground_pin : VSS ;
      timing () {
        cell_fall (into_f1) {
          index_1("1,2,3,4,5") ;
          index_2("1,2,3,4,5") ;
          values("13, 13, 14, 16, 18",\
                 "13, 14, 15, 16, 19",\
                 "14, 15, 16, 17, 20",\
                 "15, 15, 16, 18, 20",\
                 "15, 16, 17, 18, 21") ;
        }
      }
    }
  }

答案 1 :(得分:1)

如果您一次分析该文件以处理一个 async componentDidMount() { const response = await fetch(MY_WEBHOOK); const json = await response.json(); this.setState({ docs: json }); } 部分,这将变得容易得多。这段代码似乎可以解决问题,但是它可能很脆弱,很可能会在更复杂的输入上中断。

pin { ... }

我也将其写为Unix过滤器。也就是说,它从#!/usr/bin/perl use strict; use warnings; # $counter will contain the number of unmatched { characters # we have seen. If it's a positive number then we are in a # pin section and we won't output anything until we get to # the end of that section. my $counter; # Contains the contents of our current pin section. my $pin = ''; # Read a line at a time from STDIN while (<>) { # If we're not in a pin block, then # just print the line. if (!$counter and !/\bpin\b/) { print; next; } # Increment $counter by the number of { characters in the line $counter += tr/{/{/; # Decrement $counter by the number of } characters in the line $counter -= tr/}/}/; # Append the current line to $pin $pin .= $_; # If $counter is 0 then we've just got to the end of a pin # block. The text of that block will be in $pin. if (!$counter) { # If $pin contains "directions : output" and "timings"... if ($pin =~ /direction\s*:\s*output/ and $pin =~ /timing\s*/) { # ... then remove the "max_transitions" line from $pin $pin =~ s/\s*max_transition\s*:.*\n//; } # Print the current $pin section print $pin; # And reset $pin to start the next section. $pin = ''; } } 读取并写入STDIN。这比硬编码文件名更加灵活。您可以这样运行它:

STDOUT