Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: how to check for a word in a file and if found remove previous and next -- 3 oneliner variations

by Discipulus (Abbot)
on Jan 26, 2016 at 11:50 UTC ( #1153667=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    # $y is <C>yet</C> set for the second read, @u is <C>unwanted</C> <P>
    
    ...
    33333
    44444<P>
    
  2. or download this
    perl -lnE "BEGIN{$r=shift;$r=qr/$r/;$n=shift@ARGV;push @ARGV,$ARGV[0]}
    +eof&&!$y?$y=close ARGV:/$r/&&!$y?push @u,$.-$n..$.+$n:$y&&!($.~~@u)?s
    +ay:0" XXXXX 1 next-and-previous.txt
    11111
    ...
    perl -lnE "BEGIN{$r=shift;$r=qr/$r/;$n=shift;push @ARGV,$ARGV[0]}eof&&
    +!$y?$y=close ARGV:/$r/&&!$y?push @u,$.-$n..$.+$n:$y&&!($.~~@u)?say:0"
    + XXXXX 2 next-and-previous.txt
    22222
    
  3. or download this
    perl -slnE "BEGIN{$r=qr/$r/;push @ARGV,$ARGV[0]}eof&&!$y?$y=close ARGV
    +:/$r&&!$y?push@u,$.-$n..$.+$n:$y&&!($.~~@u)?say:0"  
               -- -r=XXXXX -n=1 next-and-previous.txt
    ...
    33333
    44444
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2020-10-24 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?