binmode STDIN; binmode STDOUT; # Could read in the entire file, # but read in chunks ending with # 00h to save memory. $/ = "\x00"; while (<>) { s/(\x31\x33\x39\x37.*?\x00)/"\x00" x length($1)/e; # Or the following if you don't want # to replace the start and the end: #s/(\x31\x33\x39\x37)(.*?)(\x00)/ # $1 . ("\x00" x length($2)) . $3 #/e; print; }