inhwrbp
inhwrbp

Reputation: 609

Doing an accurate text replacement with sed in braces?

I am trying to modify an xcode project file from the command line as part of a build process.

I want to change:

...
SystemCapabilities = {
  com.apple.InAppPurchase = {
    enabled = 0;
  };
};
...

to this:

...
SystemCapabilities = {
  com.apple.InAppPurchase = {
    enabled = 1;
  };
};
...

The simplest solution I can use to solve this is by simply running:

sed -i '' 's/enabled = 0/enabled = 1/' myapp.pbxproj

However, I feel like this is kind of dangerous because even though it works, there could be an issue if there is an enabled = 0 somewhere else in the file. Is there a better way to use sed to make sure I only target that specific instance of enabled = 0 that is in com.apple.InAppPurchase object?

Upvotes: 0

Views: 34

Answers (1)

choroba
choroba

Reputation: 241758

You can use an address range to tell sed to only apply the substitution between com.apple.InAppPurchase and }:

sed '/com\.apple\.InAppPurchase/,/}/s/enabled = 0/enabled = 1/'

Upvotes: 1

Related Questions