%length = (HDR => 5, FTR => 10); while() { while(/(HDR|FTR)/) # find either HDR or FTR { substr($_, $-[1], $length{$1}) = ''; # $-[1] is the position of the first capture groups (parenthesis) } print; } __DATA__ HDR--Hello this is a test FTR-------Should there be text here? HDR--Two records on the same line FTR------- HDR--and here an incomplete footer FTR--