open(FH,"test.txt"); while ( ) { ## If the line matches for this pattern, I should read 4th line from current line if ( $_ =~/you have to read the fourth line/ ){ $i=0; while ($i<4) { $line = ; $i++; } print $line; } ## If the line matches for this pattern, I should read 2nd line from current line elsif ( $_ =~/you have to read the second line/ ){ $i=0; while ($i<2) { $line = ; $i++; } print $line; } }