Extracting specific lines with Perl

前端 未结 4 774
孤城傲影
孤城傲影 2021-01-20 20:14

I am writing a perl program to extract lines that are in between the two patterns i am matching. for example the below text file has 6 lines. I am matching load balancer and

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-20 20:31

    You can use the flip-flop operator.

    Additionally, you can also use the return value of the flipflop to filter out the boundary lines. The return value is a sequence number (starting with 1) and the last number has the string E0 appended to it.

    # Define the marker regexes separately, cuz they're ugly and it's easier
    # to read them outside the logic of the loop.
    my $start_marker = qr{^ \s* \*\*load \s balancer\*\* \s* $}x;
    my $end_marker   = qr{^ \s* \*\*end\*\* \s* $}x;
    
    while(  ) {
        # False until the first regex is true.
        # Then it's true until the second regex is true.
        next unless my $range = /$start_marker/ .. /$end_marker/;
    
        # Flip-flop likes to work with $_, but it's bad form to
        # continue to use $_
        my $line = $_;
    
        print $line if $range !~ /^1$|E/;
    }
    
    __END__
    foo
    bar
    **load balancer** 
    new 
    old
    good
    bad
    **end**
    baz
    biff
    

    Outputs:

    new 
    old
    good
    bad
    

提交回复
热议问题