imposing some of my programming style on a whole source tree of files (muahahaha!)
I inherited a project at my current job that had only been worked on by one person before, over a long period of time. I've been refactoring it for quite a while, now, and one thing that I've done a lot of during this is reformat into a style that I'm used to/more comfortable with. I've done this largely with the help of various sets of regular expressions, using either search and replace in TextPad (my text editor of choice) or a combination of find/grep/sed/etc.
Here's one that I did fairly recently, which, while not incredibly useful or complicated, did what I needed it to do. I also had fun figuring out the right expressions, and enforcing my programming style on hundreds of files with one commandline. :)
for f in `find . -name '*.jsp'`; do echo $f; sed -e 's/^\([ \t]\+\)\(.*[^ \t]\+.*\)[ \t]*{$/\1\2\n\1{/g' -e 's/^\([ \t]*\)}[ \t]*\([^ \t]\+.*\)$/\1\}\n\1\2/g' $f >$f.2; diff $f $f.2; mv $f.2 $f; chmod 755 $f; done
I created this in two parts, originally in textpad, then decided I wanted to put them together and run them on all of the JSPs in the source tree. Here are the two parts, as I documented them originally, which should make it easier to understand the final command above:
----
move {'s from ends of code lines to blank lines,
for example:
try {
would change to:
try
{
search for:
^([ \t]*)(.*[^ \t]+.*)\{\n
replace with:
\1\2\n\1{\n
sed -e 's/^\([ \t]\+\)\(.*[^ \t]\+.*\)[ \t]*{$/\1\2\n\1{/g' FileName.jsp
----
move }'s from beginnings of code lines to blank lines,
for example:
} catch (Exception e)
would change to:
}
catch (Exception e)
search for:
^([ \t]*)\}[ \t]*([^ \t]+.*)\n
replace with:
\1\}\n\1\2\n
sed -e 's/^\([ \t]*\)}[ \t]*\([^ \t]\+.*\)$/\1\}\n\1\2/g' FileName.jsp
----
-spugbrap
