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.

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

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

System    .
out
. 
println

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

Thanks in advance.

Let me know if the following command does the replacement job for you:

sed ':strt
s/System[[:space:]]*\.[[:space:]]*out[[:space:]]*\.[[:space:]]*println/Logger.println/g
/System[[:space:].]*\(out\)*[[:space:].]*\(println\)*[[:space:]]*$/ {
N
b strt
}' infile

Thanks for your reply.

After running the command, it displays the below message.

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

can you please recheck it.

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).

1 Like

Thanks elixir.

I have run the command as below.

  
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.