in reply to how to check for a word in a file and if found remove that line, the above line and the below line from the file.

Here is my proposal which has a somewhat simpler logic utilizing the empty string to print nothing:

use strict; use warnings; my $previous=""; while(<DATA>){ if(/XXXXX/){ <DATA>; $previous=""; }else{ print $previous; $previous=$_; } } print $previous; __DATA__ PPPPP XXXXX is my name YYYYY KKKKK UUUUU BBBBB CCCCCC XXXXX is what I play KKKKK NNNNN
  • Comment on Re: how to check for a word in a file and if found remove that line, the above line and the below line from the file.
  • Download Code

Replies are listed 'Best First'.
Re^2: how to check for a word in a file and if found remove that line, the above line and the below line from the file.
by ikegami (Patriarch) on Jan 22, 2016 at 16:27 UTC

    That fails if there are two lines in a row that matches.

    That can fail if the last line matches. You can't rely on a file handle return EOF more than once.

      Damn! Life is just too complicated. Thanks for pointing this out.