For a second I was hoping to get away with sed‘s a\ command, but sed (in my hands, anyway) isn’t really a fan of keeping state (inserts after every #include).
So is there a way to do this with sed? Is there a smart way of doing this?
I’ll resort to writing a regular Python/Ruby script if that’s the way to go, but this seems to be a problem someone has probably bumped into somewhere, sometime.
Before applying your sed you can reverse the file line by line with the command tac and then let sed do an insert instead of append. And of course make sure that sed only does this insert once. Like this:
That should do the trick.