Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 ( #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


Comment on Re: help: extracting multiple lines from file based on match in one line
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-07-11 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (219 votes), past polls