Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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.

by kcott (Archbishop)
on Jan 20, 2016 at 07:31 UTC ( [id://1153148]=note: print w/replies, xml ) Need Help??


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):

  • I originally used your data and got your expected output.
  • I then added "test 1" and got your expected output again.
  • I then added "test 2a" and "test 2b": your expected output again.
  • I then added "test 3": your expected output again.
  • I then added "test 4": your expected output PLUS the RRRRR line at the end.
#!/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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153148]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found