For once I agree with
Tuxman Though I used AWK more because AWK scripts are easier to maintain. Another option is
Minitrue by Andrew Pipkin, which is specifically designed for the job. It's worth reading
Eric Pement's sed page for more details on this; keep paging down to the "related batch editing files" section which mentions Minitrue amongst others. On no account overlook Mr. Pement's supplementary page on
Minitrue workarounds. Mr. Pement appears unaware that Jason Hood has made an updated version of
MiniTrue that fixes some issues, though I haven't tried it.