Yes, i got fixed with this and i made unsuccesfull tries using
eof..eof flip-flop to process even files..
anyway now i get two semi-obfuscated oneliners, the first, simpler, is called with a single arg (the name of file):
# $y is <C>yet</C> set for the second read, @u is <C>unwanted</C> <P>
perl -lnE "BEGIN{push @ARGV,$ARGV[0]}eof&&!$y?$y=close ARGV:/XXXXX/&&!
+$y?push @u,$.-1..$.+1:$y&&!($.~~@u)?say:0" next-and-previous.txt
11111
22222
33333
44444<P>
while the latter is parametrizable one: firts arg is the regex used to match unwanted strings, the second is the number of lines to be removed after and before and then the filename:
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
22222
33333
44444
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
I promise i'll try to no bother you anymore with this.. :=)
update the parametrized oneliner can be shortend using -s switch (that has some problem anyhow)
It is the second time i use -- too.
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
11111
22222
33333
44444
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.