Extracting specific lines with Perl

前端 未结 4 770
孤城傲影
孤城傲影 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条回答
  •  被撕碎了的回忆
    2021-01-20 20:48

    You can use the flip-flop operator to tell you when you are between the markers. It will also include the actual markers, so you'll need to except them from the data collection.

    Note that this will mash together all the records if you have several, so if you do you need to store and reset @array somehow.

    use strict;
    use warnings;
    
    my @array;
    while () {
        if (/^load balancer$/ .. /^end$/) {
            push @array, $_ unless /^(load balancer|end)$/;
        }
    }
    
    print @array;
    
    __DATA__
    load balancer
    new 
    old
    good
    bad
    end
    

提交回复
热议问题