0

I have the following code:

    this.directives.forEach(function (dir) {
        var myVar = "hello";
        if (control.text !== myVar) {
            cleanUp(control);
            if (dir)
                setUpControl(myVar, dir);
            control = dir;
        }
    });

And need to replace everything that is between: if (control !== myVar) { and }. I have followed this answer and tried the following:

sed -i 's/(if \(control\.text !== myVar\) \{).*?(\})/\1 REPLACEMENT_STRING \2/is' myFile.js

which returns

sed: 1: "s/(if \(control\.text ! ...": RE error: invalid repetition count(s)
Nate
  • 7,606
  • 23
  • 72
  • 124
  • `\{` is opening repetition but then you follow it with a `(` which is not a valid argument, should be like `{n,m}`. You want a literal `{` which you should not escape. Even fixing that though your command won't work since sed edits line by line. – 123 Oct 26 '17 at 11:18
  • @123 what would you recommend to use? – Nate Oct 26 '17 at 11:21

1 Answers1

1

You can try this one but it's not really efficient if some '{' appear in your function...

sed '/if (control\.text !== myVar) {/!b;p;s/.*/REPLACEMENT_STRING/p;:A;N;/}/!{s/.*//;bA};D' myFile.js
ctac_
  • 2,413
  • 2
  • 7
  • 17
  • Seems to work but my replacement string messes everything up. My whole sed is: `sed -i '/if (dir\._control !== newCtrl) {/!b;p;s/.*/\t\t\t\tcleanUpControl(dir\._control, dir);\n\t\t\t\tif (newCtrl \&\& newCtrl instanceof FormControl) {\n\t\t\t\t\tsetUpControl(newCtrl, dir);\n\t\t\t\t\tdir\._control = newCtrl;\n\t\t\t\t} else {\n\t\t\t\t\tdir\._control = null;\n\t\t\t\t}/p;:A;N;/}/!{s/.*//;bA};D' forms.umd.js` but it copies twice what I need and doesn't replace but add. any idea? – Nate Oct 27 '17 at 14:21