2009-01-05

Using sed to remove deprecated function calls

I am super lazy. As a programmer, this is a good thing. For a project that I work on, I was recently given a code maintenance task of cleaning up assert statements. ASSERT_S_#_MED had been deprecated in favor of just ASSERT_S_# (where # is the number of informational parameters for logging purposes).

The caveats are that I don't know how many files have the deprecated call, and I didn't want to do a blind stripping of _MED because I wasn't sure if it was legitimately somewhere else in the code; so I needed to make sure a digit existed before it.



1 #!/bin/sh
2 LIST="`grep '_[1-9]_MED' *.C | awk '{print $1}' | sed 's/://g' |uniq`"
3 echo $LIST
4 for i in $LIST
5 do
6 cleartool co -nc $i
7 sed -i 's/_\([1-9]\)_MED/_\1/g' $i
8 cleartool ci -nc $i
9 done


Line 2: Populate a list of all files with the deprecated call. The awk takes the first part of the grep, then the sed strips the colon, and then uniq removes duplicates.
Line 7: The sed matches _[1-9]_MED, captures the digit, and uses a backreference to put it in the replacement. The -i flag means it edits in place.
Line 6,8: Version control that I'm using for the project

Hopefully this will be useful to someone! If you have a better way of doing it, I encourage you to post in the comments. We're all learning, after all!

3 comments:

Ramanathan said...

Super .... Thanks a lot ... it saved lots of my time ....

shohn said...

This helped me with another script to remove the last line of every file in a directory. Perhaps there is a simpler way though.

shohn said...

This helped me with another script to remove the last line of every file in a directory. Perhaps there is a simpler way though.