2

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?

For_A_While
  • 315
  • 2
  • 18

2 Answers2

1

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

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.

Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45
  • Sir, sorry. I am actually trying to achieve this using sed command. I was failing using diffrent sed commands and was not sure about its relation to java file. Thats why I tagged java. I corrected the tag in the post. @Raghu K Nair – For_A_While Mar 03 '16 at 05:31