Block of code replacement in Java source code through Unix script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Block of code replacement in Java source code through Unix script
# 1  
Old 01-25-2012
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.
Code:
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.

Last edited by Franklin52; 01-26-2012 at 03:42 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-25-2012
See if this works for you:
Code:
#!/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

# 3  
Old 01-30-2012
Thanks for your time.

Thanks for your time. However, this is failing while handling while there are comments with /* and other stuff with special characters possible in java.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Where can i get unix source code?

Sir please tell me where can i get source code for some unix kernal and shell also. (1 Reply)
Discussion started by: VIPUL15
1 Replies

2. UNIX for Advanced & Expert Users

Source code for Unix commands

hi Folks!! Just got a question in mind.... Is it possible for us to read the c code of the commands in the bin directory? I work on a LINUX server and i only see executables in the bin directory which i obviously can't read. Please help me regarding this. Thanks in advance and wishing you a very... (2 Replies)
Discussion started by: bdiwakarteja
2 Replies

3. Programming

I have C++ exe file( no source code) and need to run many large dataset under unix, b

I have C++ exe file( no source code) and need to run many large dataset under unix, but how to know the memeroy usage for one dataset?http://www.codeproject.com/script/Forums/Images/New.gif I think "top" is not good and if using the profiler, it seems no free download, any ideas? (1 Reply)
Discussion started by: Danielwang1986
1 Replies

4. Programming

Exception Signal 11 while running JAVA code in UNIX

Hi, when i compile my java code in UNIX using javac, the class file is getting created. But when i try to run the code using java - classpath command, i get the following error. ---------------------------------------- /u/up11/sample/request:>java -classpath /u/up11/sample/request... (0 Replies)
Discussion started by: satish2712
0 Replies

5. Linux

how to get source code of Unix commands?

Can anyone tell me how/where to get the source code for the unix commands like "ls ,finger,cp....etc" commands . I have tried very hard but failed so far. Thanks JAGDISH MACHHI (4 Replies)
Discussion started by: jagdish.machhi@
4 Replies

6. Post Here to Contact Site Administrators and Moderators

Source Code Of Unix

Do you have the source code of UNIX? If yes, can you please send it to me on my email email address deleted For your this help I shall be obliged to you. With Regards rajesh ***phone removed*** (1 Reply)
Discussion started by: rajesh_ranjan26
1 Replies

7. Post Here to Contact Site Administrators and Moderators

the source code of unix

tha ithela na moy peite an mporo apo kapoy na vro ton pigaio kvdika toy unix se glossa c .efxaristo. (1 Reply)
Discussion started by: matsoyka
1 Replies
Login or Register to Ask a Question