Assuming that you have a file like
<root>
<bean:write name='iframesrcUrl'/>
<bean:write name="iframesrcUrl"/>
<bean:write name="currentPage" property="title" filter="false"/>
<foo><bar/></foo>
</root>
you can do replacements with this sed
command (using GNU sed):
sed "s/<bean:write name=[\'\"]\?iframesrcUrl[\'\"]\?\/>/\${ iframesrcUrl }/g; \
s/<bean:write name=[\'\"]\?currentPage[\'\"]\?.*\/>/\${ currentPage.title }/g;" \
input.xml
which produces:
<root>
${ iframesrcUrl }
${ iframesrcUrl }
${ currentPage.title }
<foo><bar/></foo>
</root>
Is it what you need? Or do you want to replace attributes' values? Or do you want to put your substitution text into these tags?
To find and edit all files in-place (attention! changes your files, please test without -i
before use, put your file mask instead of '*.jsp'):
find . -type f -name '*.jsp' -print0 | xargs -0 sed -i "..."
UPDATE
To replace attribute values, not the lines of file themselves, I would strongly recommend using xmlstarlet
instead of sed
/awk
. It is much more reliable and flexible. I cannot post solution exactly for your case, because xmlstarlet
needs a complete (valid) file to process, but this is an idea:
Given a file:
<a>
<b>
<c name="foo"/>
<c name="bar"/>
</b>
</a>
Let say we want replace foo
with SPAM
and bar
with EGGS
. Then this command will do it (splitted lines for readability):
$ printf '<a><b><c name="foo"/><c name="bar"/></b></a>' | \
xmlstarlet ed --update "//c[@name='foo']/@name" -v SPAM \
--update "//c[@name='bar']/@name" -v EGGS
<?xml version="1.0"?>
<a>
<b>
<c name="SPAM"/>
<c name="EGGS"/>
</b>
</a>
I used XPath syntax to select an element to replace (in the first case it is name
attribute which belongs to any c
tag and is equal to foo
). ed
subcommand of xmlstarlet
allows various transformations, replacing (updating) an element is just on of them.
In real-life examples you will need to specify also bean
workspace, i.e. add something like
-N bean=urn:...
to the list of xmlstarlet
's options. You can find the correct URI in the first lines of your .jsp file (I don't have any to look at).