Fabian Beier-Trampusch Blog

» Knowledge through passion. «

Mass case conversion of wrong cased XML content with sed
#Projects 

I recently had the problem of converting a huge XML containing liquibase database migrations. The problem was, that the database migration failed in our CI environment. The problem was caused by the ignored casing in our development databases, while the CI database operated case sensitive.

The following sed command takes all XML attributes and converts them to lowercase. sed -r 's/(.*?)(tableName|name)="([^"]*?)"(.*)/\1\2="\L\3\E"\4/' < file.xml > file-out.xml

The following XML has problematic lines highlighted.

 1<databaseChangeLog>
 2	<changeSet author="Someone" id="1" failOnError="true" context="test">
 3		<insert tableName="mappingSomeThings">
 4			<column name="MAC_ADDRESS" value="56:CC:EF:84:66:CF"/>
 5			<column name="ipv4_address" value="10.23.1.1"/>
 6			<column name="vlan_id" value="1"/>
 7			<column name="hostname" value="lin1.jstage.de"/>
 8			<column name="comment_1" value="test11"/>
 9			<column name="comment_2" value=""/>
10			<column name="last_edit_by" valueNumeric="1"/>
11		</insert>
12		<insert tableName="MAPPINGSOMETHINGS">
13			<column name="MAC_ADDRESS" value="56:CC:EF:84:66:CF"/>
14			<column name="ipv4_address" value="10.23.1.1"/>
15			<column name="vlan_id" value="1"/>
16			<column name="hostname" value="lin1.jstage.de"/>
17			<column name="comment_1" value="test11"/>
18			<column name="comment_2" value=""/>
19			<column name="last_edit_by" valueNumeric="1"/>
20		</insert>
21		<insert tableName="mappingsomethings">
22			<column name="MAC_ADDRESS" value="56:CC:EF:84:66:CF"/>
23			<column name="ipv4_address" value="10.23.1.1"/>
24			<column name="vlan_id" value="1"/>
25			<column name="hostname" value="lin1.jstage.de"/>
26			<column name="comment_1" value="test11"/>
27			<column name="comment_2" value=""/>
28			<column name="last_edit_by" valueNumeric="1"/>
29		</insert>
30	</changeSet>
31</databaseChangeLog>

Woohoo, sed saved the day!

Questions? Comments? Suggestions? I would love to hear from you!