简体   繁体   中英

Flip flop usage during multi “START” pattern in Perl

I have a Test.txt file as shown below:

some data1 func-name1 SECTION.
data to be extracted
data to be extracted
some data2 func-name2 SECTION.
data to be extracted
data to be extracted
func-name2-EXIT. EXIT.

Now, I am using flip-flop operator to extract the ranges, where my START= SECTION. and END= -EXIT. EXIT. as below:

open FILE1, "<Test.txt" 
    or die "Cannot open File1.txt!";

open FINAL, ">Final.txt" 
    or die "Cannot open Finall.txt!";

my $START = 'SECTION.';
my $END   = '-EXIT EXIT.';

while (<FILE1>) {
    if (/$START/ .. /$END|$START/) {

        if ($_ =~ m/$END|$START/) {
            $flag = 1;
        }

        print FINAL $_;

        if ($flag == 1) {          
            print FINAL "\n\n";
            $flag = 0;
        }
    }
}
close FINAL;
close FILE1; 

What I am trying to do is that, I need two next line as delimiters between the SECTION. ranges also and my Final.txt should look like as below:

some data1 func-name1 SECTION.
data to be extracted
data to be extracted


some data2 func-name2 SECTION.
data to be extracted
data to be extracted
func-name2-EXIT. EXIT.

Please guide me on this.

You don't need flip-flop for that:

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

my $first = 0;

while (<DATA>) {
    print /SECTION/ && $first++ ? "\n\n$_" : $_;
}

__DATA__
some data1 func-name1 SECTION.
data to be extracted
data to be extracted
some data2 func-name2 SECTION.
data to be extracted
data to be extracted
func-name2-EXIT. EXIT.

I count sections with $first to make sure no line breaks are inserted before the first section, but if the first section starts on line one, you can just use the special variable $. and replace $first++ with $. > 1 $. > 1 .

The first part of the expression /SECTION/ && $first++ is evaluated for every line. /SECTION/ is shorthand for $_ =~ /SECTION/ which means that if the string SECTION is not found the line is printed as-is ( $_ ).

If SECTION is found $first++ is evaluated. Note that postfix incrementation is used because we want $first to evaluate to 0 ( false ) for the first section so that it's printed as-is ( $_ ) and two line breaks are only prepended to following sections ( "\\n\\n$_" ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM