Block of code replacement in Java source code through Unix script

Hi,

I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory.

finally {
			if (null != hibernateSession && hibernateSession.isOpen()) {
				//hibernateSession.close();
			}
		}

It would be great if the script has the following capability.

It should identify
a) finally block and its {
b) after that it should ignore { } combinations until it gets its closing }
for ex: in the above, inside finally , there is an if and it has { and } which are to be ignored.
3) and once it identified whole block, it has to be removed.

Many thanks for your help.

See if this works for you:

#!/usr/bin/ksh
typeset -i mCnt
typeset -i mFlag
rm -f File.out
while read mLine; do
  mCnt=$(echo ${mLine} | grep -c 'finally[ ]*{')
  if [[ ${mCnt} != 0 ]]; then
    mFlag=1
    while [[ ${mFlag} != 0 ]]; do
      read mLine
      mCnt=$(echo ${mLine} | grep -c '{')
      if [[ ${mCnt} != 0 ]]; then
        mFlag=${mFlag}+1
      fi
      mCnt=$(echo ${mLine} | grep -c '}')
      if [[ ${mCnt} != 0 ]]; then
        mFlag=${mFlag}-1
      fi
    done
  else
    echo ${mLine} >> File.out
  fi
done < File.in

Thanks for your time. However, this is failing while handling while there are comments with /* and other stuff with special characters possible in java.