Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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 hdb (Monsignor)
on Jan 20, 2016 at 12:56 UTC ( [id://1153173]=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.

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-25 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found