Sumathi Gokul
Sumathi Gokul

Reputation: 111

Perl script to print lines between empty lines

i read a similar question at (How can I print all the lines between the previous and next empty lines when a match is found?)! and tried to print all lines between empty lines, but it is not printing so. Here is the script i tried, kindly correct it to work for my requirement.

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??
}
}

Input File:

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

Output file required:

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));

Upvotes: 1

Views: 119

Answers (1)

Dave Cross
Dave Cross

Reputation: 69264

Use a 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

I've used the DATA filehandle here to make it easy to demonstrate what's happening. It's easy to swap that out for another filehandle.

Upvotes: 1

Related Questions