No such thing as a small change | |
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 johngg (Canon) |
on Jan 20, 2016 at 11:59 UTC ( [id://1153168]=note: print w/replies, xml ) | Need Help?? |
Here's a slightly different approach that runs a while loop checking that end of file hasn't been reached rather than reading each line directly. Inside the loop a line is pushed onto the buffer then tested for the "reject" condition. If it is a reject then the next line is also added to the buffer then the whole buffer is discarded so as to reject all three lines. If the most recent line was not a reject then the previous line is shifted off the buffer and printed.
The output.
I hope this is of interest. Update: Added the output. Update: 2: Inserted push @buffer, scalar <$inFH> while ... line and added some paired "XXXXX" lines to address the point raised by mr_mischief. The output is unchanged. Cheers, JohnGG
In Section
Seekers of Perl Wisdom
|
|