Replacing System.out.println with Logger.println in *.java using SED (spanned over multiple lines)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing System.out.println with Logger.println in *.java using SED (spanned over multiple lines)
# 1  
Old 08-10-2012
MySQL Replacing System.out.println with Logger.println in *.java using SED (spanned over multiple lines)

Hi,

Can anyone help me out for my below problem.

I need to replace all System.out.println with Logger.println in *.java using SED (spanning multiple lines)
including current & sub-directories.

I tried with below command. But it is not replacing when source text is spanned over multiple lines.

Code:
 find -name '*.java' -print -exec sed -i.bak 's/System.*println/Logger.println/g' {} \;

Code:
System.out.println
System . out . println

It is able to replace above two statements. But below statement is not able to.

Code:
System    .
out
. 
println

Also the command needs to be displayed only the list of files which were replaced.

Thanks in advance.

Last edited by Scott; 08-13-2012 at 03:25 AM.. Reason: Code tags
# 2  
Old 08-11-2012
Let me know if the following command does the replacement job for you:
Code:
sed ':strt
s/System[[:space:]]*\.[[:space:]]*out[[:space:]]*\.[[:space:]]*println/Logger.println/g
/System[[:space:].]*\(out\)*[[:space:].]*\(println\)*[[:space:]]*$/ {
N
b strt
}' infile


Last edited by elixir_sinari; 08-11-2012 at 08:13 AM..
# 3  
Old 08-13-2012
Thanks for your reply.

After running the command, it displays the below message.

Code:
 sed: -e expression #1, char 169: extra characters after command

can you please recheck it.

Last edited by Scott; 08-13-2012 at 03:25 AM.. Reason: Code tags
# 4  
Old 08-13-2012
Can you post the exact command you typed in? Most probably you've put everything on one line without using semicolons.
That aside, I never post some solution without verifying that it really does the job (at least on systems available to me at that moment).

Last edited by elixir_sinari; 08-13-2012 at 03:58 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 08-13-2012
Thanks elixir.

I have run the command as below.

Code:
  
sed -i.bak ':strt;
s/System[[:space:]]*\.[[:space:]]*out[[:space:]]*\.[[:space:]]*println/Logger.println/g;
/System[[:space:].]*\(out\)*[[:space:].]*\(println\)*[[:space:]]*$/ {;
N;
b strt;
}' *.java;

Now it is able to replace all kind of statements (System.out.println) in *.java in the current directory only.

How can i make it work for all sub-directories as well?
Also it should display the list of all replaced files (with complete directory structure) for my further processing.

Assume, my directory structure looks like the following:

a
|
- I.java
- X.java
- b
|
- J.java
- Y.java
- c
|
- K.java
- Z.java

I am running the command from 'a' directory.

Last edited by Bhanu Dhulipudi; 08-13-2012 at 09:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

Bash Scripting Help to automate replacing multiple lines

Background: I am writing a script to help me automate tweaks and things I apply to a custom Android rom I developed. I am on the very last part of my script, and I am stuck trying to find the right command to do what I seek. When I build roms from source, a file called updater-script is... (8 Replies)
Discussion started by: Silverlink34
8 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. Shell Programming and Scripting

searching multiple lines and replacing in shell scripting

Hi, I have a file with below contents, ssenthil = rw anilkg = rw I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "" respectively and blank line immediately and third line starts with " anilkg = rw Please help me . Great day... (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

Replacing pattern spanning multiple lines

Hi. I have input like this: <tr> <td class="logo1" rowspan="2"><a href="index.html"><img src="images/logo.png" /></a></td> <td class="pad1" rowspan="2">__</td> <td class="userBox"><img src="images/person.png"/> <a href="http://good.mybook.com/login.jsp">Sign In</a></td> <td... (5 Replies)
Discussion started by: zorrox
5 Replies

6. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

7. Shell Programming and Scripting

Awk println

How do you print variables and your own words on the same line ie in bash I would write something like name="Calypso" echo "your name is $Calypso" how does awk differentiate between variables and just plain text ie print "your name is name" ?? Thanks Calypso (2 Replies)
Discussion started by: Calypso
2 Replies

8. Shell Programming and Scripting

sed: Replacing two lines of text

I just created a file in vi, looks like this: Hello nobody nobody What I need is to have sed change the file so it looks like this: Hello everybody In other words, the sed query needs to look for both instances of "nobody" before it puts the word "everybody" on the first line. ... (2 Replies)
Discussion started by: calrog
2 Replies

9. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

10. Shell Programming and Scripting

replacing multiple lines

i have a file : sample1.txt OBJECT="POINT" ACTION="REDEFINE" POINT_NAME="ABCD001G " GHYT_POPRIORITY_1="1" GHYT_POPRIORITY_2="1" GHYT_POPRIORITY_3="1" GHYT_POPRIORITY_4="1" GHYT_POPRIORITY_USER="1" HIGH_ALARM_PRIORITY_1="1" HIGH_ALARM_PRIORITY_2="1" HIGH_ALARM_PRIORITY_3="1" ... (1 Reply)
Discussion started by: ajnabi
1 Replies
Login or Register to Ask a Question