Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: help: extracting multiple lines from file based on match in one line

by olivierp (Hermit)
on Nov 16, 2004 at 23:55 UTC ( [id://408294]=note: print w/replies, xml ) Need Help??


in reply to help: extracting multiple lines from file based on match in one line

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>
HTH
--
Olivier

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found