For_A_While
For_A_While

Reputation: 315

Replacing the lines between two commented lines using sed commands in a java file

I am using Ubuntu 14.04. I need to replace this section of code in java (using JDK-8) file using sed commands -

//START_EDIT1
some lines
//END_EDIT1

with this lines of codes.

//START_EDIT1
System.out.println("Done");
//END_EDIT1

I am a newbie and can not find the right commands to find this two comments in java code and replace the code between them using sed commands.I can not change the java comment format. Is there a way to find this using sed commands? or simply what is the sed command to find double forward slash comment?

Upvotes: 2

Views: 260

Answers (2)

Cyrus
Cyrus

Reputation: 88766

With GNU sed:

sed '/\/\/START_EDIT1/,/\/\/END_EDIT1/c\//START_EDIT1\nSystem.out.println("Done");\n//END_EDIT1' file

If you want to edit your file "in place" use sed's option -i.

Upvotes: 1

Raghu K Nair
Raghu K Nair

Reputation: 3942

Why cant do you a vi and ESC :%s/START_EDIT1/System.out.println\(\"Done\"\)\;/g again for :%s/END_EDIT1//g

You could achieve the samething with sed as well. But this has nothing to do with Java.

Upvotes: 0

Related Questions