Vineet Deoraj Vineet Deoraj - 3 months ago 13
Perl Question

Ignore lines in a file till match and process lines after that

I am looping over lines in a file and when matched a particular line, i want to process the lines after the current (matched) line. I can do it :-

open my $fh, '<', "abc" or die "Cannot open!!";
while (my $line = <$fh>){
next if($line !~ m/Important Lines below this Line/);
last;
}
while (my $line = <$fh>){
print $line;
}


Is there a better way to do this (code needs to be a part of a bigger perl script) ?

Answer

I'd use flip-flop operator:

while(<DATA>) {
    next if 1 .. /Important/;
    print $_;
}
__DATA__
skip
skip
Important Lines below this Line
keep
keep

output:

keep
keep