while($line=<>) { chomp $line; if($line=~/^\*\*\* DOCUMENT BOUNDARY \*\*\*/) { check(); print "$line\n"; next; } if($line=~/^FORM=/) { check(); print "$line\n"; next; } if($line=~/\.\d\d\d\./) { check(); $started_tag = 1; } $tag.=$line; } sub check() { if($started_tag) { print "$tag\n"; undef $tag; $started_tag = 0; } }