#!/usr/bin/perl $/ = '..DN'; while(){ chomp; if($output =~ m/\.\.DN:(.*)/) { open($fh, '>', "$1.xml") or die "$1.xml: $!"; print $fh, $_; } # End of sub sroutine }