I'm quite new to Perl and I have some problems in skipping lines using a foreach
loop. I want to copy some lines of a text file to a new one.
When the first words of a line are FIRST ITERATION
, skip two more lines and print everything following until the end of the file or an empty line is encountered.
I've tried to find out a similar post but nobody talks about working with text files.
This is the form I thought of
use 5.010;
use strict;
use warnings;
open( INPUT, "xxx.txt" ) or die("Could not open log file.");
open( OUT, ">>yyy.txt" );
foreach my $line (<INPUT>) {
if ( $line =~ m/^FIRST ITERATION/ ) {
# print OUT
}
}
close(OUT);
close(INFO);
I tried using next
and $line++
but my program prints only the line that begins with FIRST ITERATION
.
I may try to use a for
loop but I don't know how many lines my file may have, nor do I know how many lines there are between "First Iteration" and the next empty line.