AWK script for programatically modifying java files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK script for programatically modifying java files
# 1  
Old 06-09-2011
AWK script for programatically modifying java files

Hi,
I want to add a String variable to all java classes in my project. Assuming a class like
Code:
public class Random
{
String var="Constant string";
...
...
...
}

The text in bold is what I want to add to all java files in my workspace.
I am an absolute newbie to AWK, and read somewhere that its easily doable in AWK, and am currently hurrying through the online tutorials.. could anyone please help me with a script if its not really complex?

Thanks in advance!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 06-09-2011 at 12:35 PM.. Reason: code tags, please!
# 2  
Old 06-09-2011
Code:
 
 
awk '/public class/ { print;getline;print;getline;print "String var=\"Constant string\";"; }1' input_file > out_file;
 
mv out_file in_file

loop thorugh
Code:
"for file_name in `ls *.java`"

to do the change on all files.
# 3  
Old 06-09-2011
Code:
nawk -v ins='String var="Constant string";' '/public class/{c=2;next} c-- && !c{print ins}1' myClassFile

# 4  
Old 06-09-2011
Quote:
Originally Posted by panyam
loop thorugh
Code:
"for file_name in `ls *.java`"

to do the change on all files.
This is a combo useless use of ls * and useless use of backticks. Why do 'for file in `ls *.java` when you can do for file in *.java ?
# 5  
Old 06-09-2011
Code:
awk '{if($0~/\{/ && !b){a=1;b=1;print $0;next;} if(a){print "String abc=\"xxxxx\";";a=0}print $0}'  yourFile.java

then you can
Code:
 find . -name *.java|xargs ...

to do it on all your java files.
# 6  
Old 06-09-2011
Thanks for the quick reply guys,

Quote:
Originally Posted by sk1418
Code:
awk '{if($0~/\{/ && !b){a=1;b=1;print $0;next;} if(a){print "String abc=\"xxxxx\";";a=0}print $0}'  yourFile.java

then you can
Code:
 find . -name *.java|xargs ...

to do it on all your java files.
I tried running this, and it gave me this error:

Code:
awk: Syntax error  Context is:
>>>     {if($0~/\{/     <<<

The following worked perfectly however for me,

Code:
awk '/public class/ { print;getline;print;getline;print "String var=\"Constant string\";"; }1' input_file > out_file;
 
mv out_file in_file

thanks!

Last edited by rocker86; 06-09-2011 at 02:53 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying an awk script for syllable splitting

I have found this syllable splitter in awk. The code is given below. Basically the script cuts words and names into syllables. However it fails when the word contains 2 consonants which constitute a single syllable. An example is given below ashford raphael The output is as under: ... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Modifying Variables in Files

hi list, I am currently looking to develop an installation script which writes out .conf files based on existing .conf files according to variables which are set in a settings file. For example I have a settings file like so: ip=192.168.1.1 hosts=localAnd I want to read a file which... (3 Replies)
Discussion started by: landossa
3 Replies

3. Shell Programming and Scripting

how to delete certain java script from html files using sed

I am cleaning forum posts to convert them in offline reading version with clean html text. All files are with html extension and reside in one folder. There is some java script i would like to remove, which looks like <script LANGUAGE="JavaScript1.1"> <!-- function mMz() { var mPz = "";... (2 Replies)
Discussion started by: georgi58
2 Replies

4. Programming

modifying 2 or more values using method in Java

i wanna make modification in two or more identifiers,using a method in JAVA , but the only way to do this is by returning a value ,then also only one variable value can be modified .. one way 2 do do this is by use of array and passing its name in method ,, please suggest a different way of doing... (2 Replies)
Discussion started by: upvan111
2 Replies

5. Shell Programming and Scripting

awk script for modifying the file

I have the records in the format one row 0009714494919I MY010727408948010 NNNNNN N PUSAAR727408948010 R007YM08705 9602002 S 111+0360832-0937348 I want to get it int the format 0009714494919I MY010727408948010 NNNNNN N PUSAAR727408948010 R007YM08705 9602002 S ... (2 Replies)
Discussion started by: sonam273
2 Replies

6. UNIX for Dummies Questions & Answers

Understanding / Modifying AWK command

Hey all, So I have an AWK command here awk '{if(FNR==NR) {arr++;next} if($0 in arr) { arr--; if (arr == 0) delete arr;next}{print $0 >"list2output.csv"}} END {for(i in arr){print i >"list1output.csv"}}' list1 list2 (refer to image for a more readable format) This code was submitted... (1 Reply)
Discussion started by: Aussiemick
1 Replies

7. HP-UX

[Solved] Not able to call a script from another programatically

Hi, I need to call a shell script from another. Let me explain you with an example. a1.sh and b1.sh are two scripts. a1.sh calls b1.sh internally. If i run a1.sh manually, it internally calls b1.sh but the problem is There is a process that calls this a1.sh at regular intervals,... (1 Reply)
Discussion started by: hadarapu
1 Replies

8. Shell Programming and Scripting

Help with modifying files

Hello everyone, I have some data files, with mixed header formats. the sample for the same is: >ABCD76567.x1 AGTCGATCGTAGTCGTAGCTGT >ABCD76567.y1 AGTCGATCGTAGTCGTAGCTGT >ABCD76568.x1 pair_info:898989 AGTCGATCGTAGTCGTAGCTGT >ABCD76568.y1 pair_info:893489 AGTCGATCGTAGTCGTAGCTGT... (2 Replies)
Discussion started by: ad23
2 Replies

9. Shell Programming and Scripting

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 () (3 Replies)
Discussion started by: raxitsheth
3 Replies

10. Shell Programming and Scripting

modifying a awk line

Hi, I want to print specific columns (from 201 to 1001). The line that I am using is listed below. However I also want to print column 1. So column 1 and 201 to 1001. What modifcations do I need to make? Code: awk -F'\t' 'BEGIN {min = 201; max = 1001 }{for (i=min; i<=max; i++) printf... (5 Replies)
Discussion started by: phil_heath
5 Replies
Login or Register to Ask a Question