in reply to Re: delete lines till
in thread delete lines till

#!/usr/bin/perl while(<DATA>){ if (/\{TT\}/ .. /^\{TAG\}/) { unless (/^\{(TT|TAG)\}/) { $deletestrings = $_; #if($_ =~ m/^D$/){ print $_; open FILE, '>list.txt'; print FILE $_; close FILE; $_ = '' if index( $_, "$deletestrings" ) >= 0; } } } __DATA__ S 9912290449 00005941^B{TT} D {TAG} 9912290449 {PUBLICATION} THE OS {DATE} 000101 S 9912290450 00005941^B{TT} R {TAG} 9912290450 {DATE} 000101 {TDATE} Saturday, January 1, 2000 S 9912290451 00005941^B{TT} D {TAG} 9912290451 {DATE} 000101 {TDATE} Saturday, January 1, 2000
Now the above writes the lines which is between {TT} and {TAG}. The output is
S 9912290449 00005941^B{TT} D S 9912290450 00005941^B{TT} R S 9912290451 00005941^B{TT} D
How to write only two lines which has the character D to the file. In the file: only
S 9912290449 00005941^B{TT} D S 9912290451 00005941^B{TT} D
should be writeen

Replies are listed 'Best First'.
Re^3: delete lines till
by Anonymous Monk on Aug 26, 2009 at 03:59 UTC
    Actually the above statement prints all the lines
    S 9912290449 00005941^B{TT} D S 9912290450 00005941^B{TT} R S 9912290451 00005941^B{TT} D
    But I want to print only the statemtents which has 'D' and its above line. Yhe output should be something like this
    S 9912290449 00005941^B{TT} D S 9912290451 00005941^B{TT} D