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)
2
  • \{ 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. Commented Oct 26, 2017 at 11:18
  • @123 what would you recommend to use? Commented Oct 26, 2017 at 11:21

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.