2016-04-27 65 views
1

我在(How can I print all the lines between the previous and next empty lines when a match is found?)讀過類似的問題!並嘗試在空行之間打印所有行,但不打印。這是我嘗試過的腳本,請正確處理它以符合我的要求。Perl腳本在空行之間打印行

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 "\[email protected][$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

請包括一個示例輸入文件和所需的輸出。你可以[編輯]你的問題。 – simbabque

+0

另外,請'使用嚴格'和'使用警告'。代碼中有幾個語法問題。數學運算符不能用字符串插值。你的'print'語句不會做你認爲它的作用。 – simbabque

+0

解析Verilog:https://metacpan.org/pod/Verilog-Perl – toolic

回答

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使它容易證明發生了什麼。把它換成另一個文件句柄很容易。

+0

我試過一樣,它不工作....它輸出空文件... –

+1

當你說「我試過同樣的事情」 ,你究竟做了什麼**? –

+0

手段我試圖fipflops指向空行.. –