in reply to inline replace

Is there an even cooler way to this?

You mean, is there some other way that is less brittle, ad-hoc and prone to outrageous failure? I should hope so.

But it would be hard to suggest decent improvements, because we don't have any reason to believe that line 4 of  $F[0].xml must always be the place where your new  <keywords> $F[1] </keywords> string should properly replace the original line content.

But a few things that would simplify your approach:

then hope you didn't just obliterate your xml data, given that you haven't done any error checking, XML parsing, yadda yadda...