Script for adding few methods to bunch of Java files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for adding few methods to bunch of Java files
# 1  
Old 06-10-2010
Script for adding few methods to bunch of Java files

Hi

I have around 1000+ java file under different folder in /home/raxit/source and in each file i want to add a fix method.

--------
/*
Some comment for few lines
like header block etc..
*/

package

import

class A {

method1 ()
{
}
method2 ()
{
}
..
..
method n()
{
}

/*----
i want to add method n1() here before class ends...
*/
}

Few blank lines/tabs/space/comment
--------End of file ----

Any perl/sed handy script to do this task ?

Raxit
# 2  
Old 06-10-2010
Assuming you want to add the method in class A and you have the new method in a file called method_n1:
Code:
awk '
/class A/ {f=1;c=1;next}
/\{/{c++}
/\}/ && f && !--c{system("cat method_n1");f=0}
1' file

# 3  
Old 06-10-2010
u mean to say u have 1000 files and all 1000 files have same structure.
and before the class ends u want to put 1 method in it.

is this what u exactly want. can u also clarify if its the same class in all the files and same method has to be added.
# 4  
Old 06-11-2010
Yeah, I want to add same method to each java file. Not all files are equal, but i want to add some tracking method, that i further use with.

---------- Post updated at 11:57 AM ---------- Previous update was at 11:56 AM ----------

class A /b/c are just example , it can be any class name !

---------- Post updated at 01:37 PM ---------- Previous update was at 11:57 AM ----------

More simplified way.... and more clearly something below...

1. In /home/raxit/SRC directory and sub-directories multiple source file including java source file which i want to edit.

2. script will go to each directory + sub-directory subsequently and open each java file

3. search for last closing curly bracket } . This may not be at last line ! because file may have white spaces after last closing }

4. replace it with public final string string1="MyString"; }
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to rename bunch of files on sftp?

Hi All, I am trying to move all processed .csv files on sftp to archive dir . I tried to use wildcard *.csv but its not working . Is there any way to do this. I appreciate your help. Regards, raj (1 Reply)
Discussion started by: rajeevm
1 Replies

2. Shell Programming and Scripting

Remove the first two records from a bunch of files

Hi, i have lots of single-column text files in a directory and i want to remove from each of them the first two lines and print the result in multiple new single-column files. i know that for one file the below tail command would just do the job : tail -n +3 filename > new_filename is there... (4 Replies)
Discussion started by: amarn
4 Replies

3. Shell Programming and Scripting

Multiple edits to a bunch of html files

I'm trying to upgrade a whole bunch of pages on my site to a new design. I thought one way of doing it would be to enclose the content in special comment tags and then use some form of script to wrap the new html around it. Like this: <!-- content start --> <h1>Blah blah blah</h1> yada yada... (9 Replies)
Discussion started by: dheian
9 Replies

4. Shell Programming and Scripting

Replace Characters for bunch of Files.

Hi, I am new to unix and looking out for some help in reading a file contents and replacing the characters, the requirement is I having a folder and having nearly 300 txt files, all the file contents contains some words we need to iterate all each and every files and need to find and replace it... (1 Reply)
Discussion started by: subrahmaniank
1 Replies

5. Shell Programming and Scripting

Renaming a bunch of files

This is possibly a FAQ, but I was unable to find an answer: let's say you have two files named "hello.txt" and "goodbye.txt" and you want them to be "hi.txt" and "seeyou.txt". The typical regular expressions renamer apps do not apply, as you want different new names for each one of the files. The... (2 Replies)
Discussion started by: tokland
2 Replies

6. UNIX for Dummies Questions & Answers

How do I rename a bunch of files at once?

I have about 3000+ files name P08DDD that I want to rename U08DDD. How can I do this using a single command? (8 Replies)
Discussion started by: bbbngowc
8 Replies

7. Shell Programming and Scripting

How to call Java classes/methods from ksh

Hi all, i am new to shell scripts and have one doubt. Can we call ava classes/methods from shell scripts? if yes how? (17 Replies)
Discussion started by: girish.sh
17 Replies

8. Shell Programming and Scripting

Renaming a bunch of files

Hi Can any body help me reg. this problem? The problem is the format of the shell script should be >renam old new rename: it renames all files in current directory from old extension to new extension old: it is the old extension of file name (including the '.' ) new: its the new extension ... (2 Replies)
Discussion started by: Prashanth.m
2 Replies

9. UNIX for Dummies Questions & Answers

grep'ing for text within a bunch of files...?

I have, say, a dozen files, and I want to grep for a string of text within them. I don't remember the exact syntax, but let me give it a shot and show you an idea here... find . -type f -exec grep thisword {} \; ...and there's a way to put more than one grep into the statement, so it will tell... (1 Reply)
Discussion started by: kitykity
1 Replies
Login or Register to Ask a Question