This wouldn't delete lines from your original file, but at least splits it in two separate ones.
Note that it relies on your input being formatted as your example data.
use strict;
open OF1, ">var2_equal_to_3.txt";
open OF2, ">var2_not_equal_to_3.txt";
my ($flipflop, $flag, $prev);
while (<DATA>) {
chomp;
$flag = 1 if m/var2\s+=\s+3/i;
$flipflop = /<rec\s+[0-9]+>/i .. /<\/rec\s+[0-9]+>/i;
$prev = $_ if $flipflop == 1;
if ($flag) {
print OF1 "$prev\n" if $flipflop == 2;
print OF1 "$_\n";
}else{
print OF2 "$_\n";
}
$flag = $prev = undef if $flipflop =~ /E0$/;
}
close OF1;
close OF2;
__DATA__
<Rec 1>
var2 = 5
some text
some text
some text
some text
</Rec 1>
<rec 2>
var2 = 3
some text
some text
some text
some text
some text
some text
some text
some text
</Rec 2>
<Rec 3>
var2 = 7
some text
some text
some text
some text
some text
some text
</Rec 3>
<rec 4>
var2 = 3
some text
some text
some text
some text
some text
some text
some text
some text
</Rec 4>
-
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.
|