my $buf=""; #append to buffer until headers are found and removed while (!($buf=~s/^.*?#### end_ascii_header\n//)) { sysread(INP,$buf,1024,length($buf)); } syswrite(OUT,$buf,$length($buf)); #copy the rest of the file while (sysread(INP,$buf,16384)) { syswrite(OUT,$buf,length($buf)); }