user1065101
user1065101

Reputation:

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) ?

Upvotes: 2

Views: 2699

Answers (1)

Toto
Toto

Reputation: 91385

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

Upvotes: 6

Related Questions