perl -pi -e 'chomp;s/^(HEADER.*)$/${1}42/;s/^(REMARK.*)$/Extra line\n$1/;$_.="\n"' whateveryourfileiscalled.txt