Sometimes there is a better way, and sometimes you even have to resort to perl, but for some quick bulk editing jobs sed is just excellent. Today I was faced with a bunch of xml files defining screen layouts. They contained references to properties using elements such as <property name="Foo"... and <propertyMember name="Bar".... Unfortunately all of the names were with an initial uppercase letter while the correct Java convention is initial lowercase for property names and this was stopping the property change listener hooking up correctly. So, how to make the necessary change easily?
Using the gnu version of sed it can be done in one line to edit all the xml files in a directory, and keep backups, as shown below.
sed --in-place=".bak" --expression='s/<property\(Member\)\? name="\([A-Z]\)/<property\1 name="\l\2/g' *.xml
Even though I had to download gnu sed and compile and install it on my box (Mac OS X) this was still much faster than any other solution. The actual script executes like lightning, you hit enter and it's already finished, amazing.
Posted by Alex at March 25, 2005 10:14 PM