Perl脚本在空行之间打印行

时间:2016-04-27 10:02:19

标签: regex perl

我在(How can I print all the lines between the previous and next empty lines when a match is found?)读了一个类似的问题!并试图打印空行之间的所有行,但它不打印。这是我试过的脚本,请根据我的要求纠正它。

my @file = <IN>;
for (0 .. $#file) {
if ($file[$_] =~ /Match/){
    my $start = $_;
    while ($start >= 0 && $file[$start] !~ /^$/) {
        $start--; # $start points to first empty line
    }
    my $end = $_;
    while ($end <= $#file && $file[$end] !~ /^$/) {
        $end++; # $end points to next empty line
    }
 print OUT "\n@file[$start+1..$end-1]"; #it should print between two empty lines right??
}
}

输入文件:

wire enable,GSMC_G8,mkf_g,un1_G11_0, GND_net_1, VCC, G8, G16, Q_RNIUQAA,        CK_c, reset_c, 
    G0_c, G1_c, G17_c, G2_c, G3_c, G17_c_i, GND_1, VCC_0;

INBUF G3_pad (.PAD(G3), .Y(G3_c));
dff_0_1 DFF_1 (.G17_c(G17_c), .reset_c(reset_c), .CK_c(CK_c), 
    .G0_c(G0_c), .G8(G8));
GND GND_i_0 (.Y(GND_1));
NOR2 G3_pad_RNIUUQF (.A(G8), .B(G3_c), .Y(G16));
INV G17_pad_RNO (.A(G17_c), .Y(G17_c_i));
VCC VCC_i (.Y(VCC));
CLKBUF CK_pad (.PAD(CK), .Y(CK_c));

endmodule

需要输出文件:

INBUF G3_pad (.PAD(G3), .Y(G3_c));
dff_0_1 DFF_1 (.G17_c(G17_c), .reset_c(reset_c), .CK_c(CK_c), 
    .G0_c(G0_c), .G8(G8));
GND GND_i_0 (.Y(GND_1));
NOR2 G3_pad_RNIUUQF (.A(G8), .B(G3_c), .Y(G16));
INV G17_pad_RNO (.A(G17_c), .Y(G17_c_i));
VCC VCC_i (.Y(VCC));
CLKBUF CK_pad (.PAD(CK), .Y(CK_c));

1 个答案:

答案 0 :(得分:1)

使用flip-flop operator

#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>) {
  # Turn flip-flop on at the first empty line
  # And then off at the next empty line
  if (/^$/ ... /^$/) {
    # Ignore the two empty lines
    next unless /\S/;
    print;
  }
}

__DATA__
wire enable,GSMC_G8,mkf_g,un1_G11_0, GND_net_1, VCC, G8, G16, Q_RNIUQAA,        CK_c, reset_c, 
    G0_c, G1_c, G17_c, G2_c, G3_c, G17_c_i, GND_1, VCC_0;

INBUF G3_pad (.PAD(G3), .Y(G3_c));
dff_0_1 DFF_1 (.G17_c(G17_c), .reset_c(reset_c), .CK_c(CK_c), 
    .G0_c(G0_c), .G8(G8));
GND GND_i_0 (.Y(GND_1));
NOR2 G3_pad_RNIUUQF (.A(G8), .B(G3_c), .Y(G16));
INV G17_pad_RNO (.A(G17_c), .Y(G17_c_i));
VCC VCC_i (.Y(VCC));
CLKBUF CK_pad (.PAD(CK), .Y(CK_c));

endmodule

我在这里使用了DATA文件句柄,以便于演示正在发生的事情。将其换成另一个文件句柄很容易。