I am using sed to replace an attribute in an XML file and up until now it was working just fine.
I have an XML file that contains a tag like this:
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160406155451.eba"
name="com.mycompany.site.app" />
Whenever a new EBA file is deployed, the location attribute needs to be replaced. For example: (different build timestamp)
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160410173452.eba"
name="com.mycompany.site.app" />
I was doing this by using a sed
expression with labels:
:a;N;$!ba;0,s|<osgiApplication id="com.mycompany.site.app".*\/>|<osgiApplication id="com.mycompany.site.app" location="com.mycompany.site.app-1.0.0.20160406155451.eba"
It was working perfectly, until I came across another situation when I actually have other tags under <osgiApplication>
. For example:
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160406155451.eba"
name="com.mycompany.site.app" />
<anotherTag />
When this happens, due to the .*\/>
criterion in the sed expression, everything is replaced until the very last />
. I only want the first one occurrence to be replaced.
In other words, if I have:
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160406155451.eba"
name="com.mycompany.site.app" />
<anotherTag />
<anotherTag />
<anotherTag />
I want the replacement to be:
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160410173452.eba"
name="com.mycompany.site.app" />
<anotherTag />
<anotherTag />
<anotherTag />
But what I'm currently getting is:
<osgiApplication id="com.mycompany.site.app"
location="com.mycompany.site.app-1.0.0.20160410173452.eba"
name="com.mycompany.site.app" />
I've been searching around for a solution but found none :( Any suggestions will be really appreciated. Thanks
xmlstarlet
andxpath
. Notsed
. This is why.sed
command doesn't make sense. It's a weird combination - half range:0,
and half unterminateds
command:s|...|...
:a;N;$!ba;
you could try:a;N;/\/>/!ba;
.