open(FH,"test.txt");
$i=0;
$j1=0;
$j2=0;
while ( <FH> ) {
## If the line matches for this pattern, I should read 4th line from current line
if ( $_ =~/you have to read the fourth line/ ){
$j1=-4;
}
elsif ( $j1<0 ){
$j1++;
print $_ if $j1==0;
}
## If the line matches for this pattern, I should read 2nd line from current line
elsif ( $_ =~/you have to read the second line/ ){
$j2=-2;
}
elsif ( $j2<0 ){
$j2++;
print $_ if $j2==0;
}
print $i++,"\n";
}
In reply to Re: Reading nth line from a file
by gmontema
in thread Reading nth line from a file
by perlthirst
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |