Monday, March 9, 2009

Dirty scripting

Java permits both
new Object[][] { new Object[] { ... } }
and
new Object[][] { { ... } }
Suppose we wanted to transform the former to the more concise latter form. Naturally, going through all of the source files is not an option, so we use the tools at hand:
grep -lr 'new Object\[\] {' . | xargs sed -i 's/new Object\[\] {/{/g'
grep locates files matching the pattern (note that this command might introduce errors in case of:
Object[] wontCompile() {
return new Object[] { };
}
). -r tells grep to recurse into subdirectories and -l - to output only the filenames. Sed takes the filenames and performs a substitution writing result to the same file (-i flag).

No comments: