Я в целом согласен с @montonero, что способ сделать это с помощью XML-инструментов -
... но быстрый взлом вещей с помощью RegEx может сэкономить время.
Вот этот вкладыш PowerShell one, использующий RegEx'ы с обходными путями должен делать (при условии кодировки utf8):
(gc .\sample.xml -raw -enc utf8) -replace '(?<=\<managedobject)(?!.*?operation="create")([^\>]*?)\>','$1 operation="create">'|Set-Content new_sample.xml -enc utf8
Где
(?<=\<managedobject)
- утверждение с нулевой длиной, совпадающее с началом тега.
(?!.*?operation="create")
является отрицательным прогнозом нулевой длины, гарантирующим, что не вставляется operation="create"
дважды
([^\>]*?)
- это группа захвата, которую нужно сохранить перед вставкой.
Используется в примере файла, содержащего приведенные выше вопросы:
I would like to find an easyer solution in inserting a string at the end of a tag (before the closing tag) in a xml file in windows.
This is a huge XML file with over 4000 lines.
At almost all lines there is an managedObject tag, and at the end i would like to insert a string: operation="create".
So it looks like this:
<managedObject class="blabla" ... id="1234" operation="create">
..and want to make it like this:
<managedObject class="blabla" ... id="1234" operation="create">
As I said the XML file is huge, and not all lines have managedObject tags. I only want to insert the string were the managedObject tags are.
Up till now i did it manually..but it takes a lot of time..