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.

G'day Ganesh Bharadwaj1,

Below is my take on a solution. I did some additional testing as follows (all successful):

#!/usr/bin/env perl use strict; use warnings; my ($skip, @buf); while (<DATA>) { if ($skip) { $skip = 0; next; } if (/XXXXX/) { @buf = (); $skip = 1; } else { push @buf, $_; $skip = 0; } if ($. > 1 && @buf >= 2) { print for @buf[0 .. $#buf - 1]; @buf = ($buf[-1]); } } print for @buf; __DATA__ XXXXX - additional test 1 PPPPP XXXXX is my name YYYYY KKKKK UUUUU BBBBB CCCCCC XXXXX is what I play KKKKK NNNNN ZZZZZ - additional test 2b XXXXX - additional test 2a QQQQQ - additional test 3 RRRRR - additional test 4

— Ken

  • 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.
  • Select or Download Code