Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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

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 making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2016-07-27 23:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (251 votes). Check out past polls.