You can simplify its use of the flags variable to find out whether to print a string:
while( <$input> ) {
$n=4 if /^abc/;
print FILE if ($n-- > 0);
}
Besides simplification, it also fixes the problem: in your version, the string abc will be printed twice.