use strict; open( FILE, '1.dat') or die "1.dat: $!"; binmode FILE; $/ = "AZII*"; my $num = 0; while () { chomp; next unless length(); # skip the initial (empty) input record $num++; open (DES, ">out/$num.txt") or die ( "out/$num.tiff" ); binmode DES; print DES "AZII*".$_; close DES; last if ( $num == 5 ); }