It's really simple using "sed".
There was a time long ago when I had a similar issue and it double dawned on me that some of the best tools in NIX are the oldest AND best which are "sed" & "awk".
I could write a bash script for you but as I did many years ago before there were great tools to post like this one, just IRC that I found learning how to use "sed" and "awk" on my own has kept the knowledge intact for a few decades.
Hope this helps?
And forgive the bad pun following:
"Just listen to what the man sed".
Paul McCartney & Wings 1976