$/ = '1397'; # You don't have to specify the hex numbers; it's all the same to Perl while (<>) { # 1397 appears at the end of the record, if at all # so an anchored search-and-replace handles it s/1397$/\0\0\0\0/; }