1

Is there a way to prepend something to the first occurrence of a line of code ONLY after the occurrence of another pattern? I want to turn off the addTween line (by prepending a double slash comment) ONLY for "Group2", which is demarcated by an auto-generated comment "// Group2". In other words, I want to

  1. search for the occurrence of "// Group2"
  2. use sed to prepend a comment for the addTween line only for Group2 only.

Input file:

// Group1
this.shape = new cjs.Shape();
this.shape.graphics.f().s("#663300").ss(1,1,1).p("EBHXAAAQAAW105QIQ06QJ9kAAQ9jAA05wJQ06wIAA21QAA2zU6wJQU5wIdjAAQdkAAU6QIQU5QJAAWzg");
this.shape.setTransform(1005.55,596.9);

this.shape_1 = new cjs.Shape();
this.shape_1.graphics.f("#E5CCFF").s().p("EgydAm9Q05wJAA20QAA2zU5wJQU6wIdjgBQdkABU5QIQU6QJAAWzQAAW006QJQ05QJ9kgBQ9jAB06wJg");
this.shape_1.setTransform(1005.55,596.9);

this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_1},{t:this.shape}]}).wait(1));

// Group2
this.shape_2 = new cjs.Shape();
this.shape_2.graphics.f().s("#666600").ss(44.3,1,1).p("EhDmgf6MCHNAAAMAAAA/1MiHNAAAg");
this.shape_2.setTransform(833.25,400.65);

this.shape_3 = new cjs.Shape();
this.shape_3.graphics.f("#336699").s().p("EhDmAf7MAAAg/1MCHNAAAMAAAA/1g");
this.shape_3.setTransform(833.25,400.65);

this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_3},{t:this.shape_2}]}).wait(1));

Output file: (note only change is last line got a comment double slash prepended to it)

// Group1

this.shape = new cjs.Shape();
this.shape.graphics.f().s("#663300").ss(1,1,1).p("EBHXAAAQAAW105QIQ06QJ9kAAQ9jAA05wJQ06wIAA21QAA2zU6wJQU5wIdjAAQdkAAU6QIQU5QJAAWzg");
this.shape.setTransform(1005.55,596.9);

this.shape_1 = new cjs.Shape();
this.shape_1.graphics.f("#E5CCFF").s().p("EgydAm9Q05wJAA20QAA2zU5wJQU6wIdjgBQdkABU5QIQU6QJAAWzQAAW006QJQ05QJ9kgBQ9jAB06wJg");
this.shape_1.setTransform(1005.55,596.9);

this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_1},{t:this.shape}]}).wait(1));

// Group2

this.shape_2 = new cjs.Shape();
this.shape_2.graphics.f().s("#666600").ss(44.3,1,1).p("EhDmgf6MCHNAAAMAAAA/1MiHNAAAg");
this.shape_2.setTransform(833.25,400.65);

this.shape_3 = new cjs.Shape();
this.shape_3.graphics.f("#336699").s().p("EhDmAf7MAAAg/1MCHNAAAMAAAA/1g");
this.shape_3.setTransform(833.25,400.65);

//this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_3},{t:this.shape_2}]}).wait(1));

1 Answer 1

1
sed '/\/\/ Group2/,/addTween/ s/^.*addTween/\/\/&/' file

This searches for the text between // Group2 and the following addTween and adds // to the line containing addTween.

4
  • Freddy, thank you VERY MUCH! You are truly great.
    – mishawagon
    Jun 10, 2019 at 17:20
  • Actually, do you know a way to also undo this process? I realized I will have to comment out all the tweens and then selectively bring back some.
    – mishawagon
    Jun 10, 2019 at 20:05
  • Reverse operation: sed '/\/\/ Group2/,/\/\/.*addTween/ s/^\/\/\(.*addTween\)/\1/' file
    – Freddy
    Jun 10, 2019 at 20:18
  • wow, thank you very much!!
    – mishawagon
    Jun 10, 2019 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .