Nate
Nate

Reputation: 7866

Replace javascript code between two patterns using bash

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)

Upvotes: 0

Views: 102

Answers (1)

ctac_
ctac_

Reputation: 2491

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

Upvotes: 1

Related Questions