$field_start = 8; # 0-based $field_length = 8; # Get current value: $old_value = substr($s, $field_start, $field_length); $old_value =~ s/\s+$//; # Change the value: $new_value = 'bla'; substr($s, $field_start, $field_length) = substr($new_value . (' 'x$field_length), 0, $field_length);