I have a directory containing many subdirectories, each containing a config.xml
file I want to edit. Like:
../jobs/foo_bar-v1.2_west/config.xml
../jobs/foo_bar-v1.3_west/config.xml
../jobs/foo_stuff-v1.3_east/config.xml
../jobs/foo_foo-v9.8_north/config.xml
../jobs/NOT_FOO-v0.1_whatev/config.xml
etc.
I need a way to insert multiple lines of text into several of the ../jobs/foo*/config.xml
files, after matching the first instance of a specific line, <properties>
.
Text to insert looks like:
<a.bunch.of.TextGoesHere>
<permission>one.foo.Items.Foo:person.name</permission>
<permission>two.foo.Items.Foo:person.name</permission>
<permission>three.foo.Items.Foo:person.name</permission>
</a.bunch.of.TextGoesHere>
Each ../jobs/foo*/config.xml
looks like:
<?xml version='1.0' encoding='UTF-8'?>
<foo1>
<actions/>
<description>foo2</description>
<keepDependencies>false</keepDependencies>
<properties>
<foo3/>
</properties>
...
<lots_of_other_stuff>
<properties>
<junk>
</properties>
Final output for each config.xml
should look like:
<?xml version='1.0' encoding='UTF-8'?>
<foo1>
<actions/>
<description>foo2</description>
<keepDependencies>false</keepDependencies>
<properties>
<a.bunch.of.TextGoesHere>
<permission>one.foo.Items.Foo:person.name</permission>
<permission>two.foo.Items.Foo:person.name</permission>
<permission>three.foo.Items.Foo:person.name</permission>
</a.bunch.of.TextGoesHere>
<foo3/>
</properties>
...
<lots_of_other_stuff>
<properties>
<junk>
</properties>
I've tried using sed
to insert after a specific line, like
#!/bin/bash
find ../jobs/run* -name config.xml -exec sed -i '6a\
<text to insert>' {} \;
but occasionally, long <description>
text from the config.xml
results in an unpredictable line number on which to insert.
Next I tried using sed
to search for the first instance of <properties>
and inserting after, like
sed -i '0,/properties/a test' config.xml
but this resulted in adding the test
test after EVERY line until <properties>
was found. Using sed -i '1,/
had similar results. It was ugly.
I'm unsure if I'm using sed
properly on this Amazon Linux box, and am thinking awk
might work better here. Can anyone assist? Thanks.