taj_ritesh has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to print the lines from one file between two patterns only once, but similar patterns are present multiple times How I can configure the code to print only once I am trying following code :-
#!/usr/bin/perl use strict; my $i = 1; while (<>) { if (/Startpoint/ ... /slack/ && $i < 2 ) { print $_; $i++; }
But it is printing all the lines..
  • Comment on How to print lines between two patern only one time, not multiple timese as pattern exists in this file multiple times.
  • Download Code

Replies are listed 'Best First'.
Re: How to print lines between two patern only one time, not multiple timese as pattern exists in this file multiple times.
by choroba (Cardinal) on Apr 14, 2014 at 07:56 UTC
    The problem is that the right part of the flip-flop is never true, as it contains the $i < 2 condition together with the regex. If you want to stay with the flip-flop, I'd modify it to something like the following:
    my $i = 1; while (<DATA>) { if ($i and /Startpoint/ ... /slack/ && $i--) { print $_; } }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How to print lines between two patern only one time, not multiple timese as pattern exists in this file multiple times.
by Laurent_R (Canon) on Apr 14, 2014 at 09:33 UTC
    Hmm, it would be good to have an idea on the content of the file. Maybe something like this:
    while (<>) { print $_ if /Startpoint/ ... /slack/ ; last if /slack/; }