Monday, July 24, 2006

 

The Power of sed

So here is the Scenario, I have a large inherted code base where I want to change the package name to match the new owners. How big you ask? Well how about over 1800 Java files and about 3700 jsp's. Well I tried using Netbean's "rafator" tool but Netbeans and choked. And if I broke the task down it to smaller tasks it would take for ever. So I pulled out good ole sed with something like this for the Java files:

find . -type f -name '*.java' -print |
while read filename
do
(
(cat $filename; echo "") | sed 's/[old package goes here]/[new package goes here]/g' > $filename.tmp
mv $filename.tmp $filename
)
done

The whole thing took less than 30 seconds. IDE are good but don't forget you shell, that is where the real power is!

Comments:
Although I don't like perl, I think it offers a clean way to do this. (Same functionality, less typing)

find . -type f -name '*.java' | perl -i -pe 's/old/new/g'

You don't have to create temp files like with sed.
 
You also need an 'xargs' there to get the filename on the command line.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?