-2

I need help to replace an assignment below:

  12345678 = {
  CreatedOnToolsVersion = *.*.*;
  DevelopmentTeam = *;
  LastSwiftMigration = *;
  ProvisioningStyle = Manual;
  SystemCapabilities = {
  com.apple.ApplePay = {
  enabled = 1;
  };
  com.apple.SafariKeychain = {
  enabled = 0; };
  com.apple.SafariKeychain = {
  enabled = 0; 
  };
  };`
  };
  };

want the output to contain

 com.apple.ApplePay = {
      enabled = 0;

I tried with

sed '/SystemCapabilites = {\n/ com.apple.ApplePay = {\n/     enabled =0 ;\'  

outputs

error:- sed: 1: "/SystemCapabilites = {\ ...": command c expects \ followed by text

SLePort
  • 15,211
  • 3
  • 34
  • 44
att
  • 41
  • 2
  • 8
  • 1
    `value com.apple.Push` doesn't appear in your input so how can you change it? Clarify and fix that and [edit] your question to show the full desired output given your posted sample input plus what you have tried so far. – Ed Morton Aug 15 '17 at 18:30
  • sorry I want to change change com.apple.ApplePay = { enabled = 1; – att Aug 15 '17 at 18:44
  • 1
    please fix your question above and include your best attempt to solve your problem. StackOverflow is about helping people fix their programming code. Please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Aug 15 '17 at 18:45
  • Again, include in your question the full expected output given the input you provided. Also format your code just like your formatted your input data so it's legible. – Ed Morton Aug 15 '17 at 18:56
  • I presume there are multiple `enabled = 1;` in your file. So a `sed 's/enabled\ =\ 1;/enabled\ =\ 0;/g'` is not applicable for your use case. – Hakan Baba Aug 15 '17 at 19:36
  • Also please re-format the sed command you have tried already. It is not easy to parse what was the command and what was the output. – Hakan Baba Aug 15 '17 at 19:37

2 Answers2

0

Using sed to replace multi line strings is notoriously difficult

I can suggest to use perl. This prints out the modified file.

perl -0777 -pe 's/com.apple.ApplePay = {\s*\n\s*enabled\ =\ 1;/com.apple.ApplePay = {\nenabled\ =\ 0;/g' file1 2> /dev/null

I have not taken into consideration the white spaces in the output. Depending on the indentation of your file you can set spaces before enabled\ =\ 0

The input file is called file1 in my example.


Input

$ cat file1
12345678 = {
CreatedOnToolsVersion = *.*.*;
DevelopmentTeam = *;
LastSwiftMigration = *;
ProvisioningStyle = Manual;
SystemCapabilities = {
com.apple.ApplePay = {
enabled = 1;
};
com.apple.SafariKeychain = {
enabled = 0; };
com.apple.SafariKeychain = {
enabled = 0;
};
};`
};
};

Output

$ perl -0777 -pe 's/com.apple.ApplePay = {\s*\n\s*enabled\ =\ 1;/com.apple.ApplePay = {\nenabled\ =\ 0;/g' file1 2> /dev/null
12345678 = {
CreatedOnToolsVersion = *.*.*;
DevelopmentTeam = *;
LastSwiftMigration = *;
ProvisioningStyle = Manual;
SystemCapabilities = {
com.apple.ApplePay = {
enabled = 0;
};
com.apple.SafariKeychain = {
enabled = 0; };
com.apple.SafariKeychain = {
enabled = 0;
};
};`
};
};

For in place replacement use

 perl -0777 -pi -e 's/com.apple.ApplePay = {\s*\n\s*enabled\ =\ 1;/com.apple.ApplePay = {\nenabled\ =\ 0;/g' file1  2> /dev/null
Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
  • Thanks a lot, I can see the output in terminal but when I open the file again I see the original value only – att Aug 15 '17 at 20:04
  • @att I have updated the answer. You need to pass one more parameter to perl for inplace replacement. – Hakan Baba Aug 15 '17 at 20:10
  • @att Some best practices to address received answers: If any answer (not necessarily this one) has worked for you, you can think about these [steps](https://stackoverflow.com/help/someone-answers). – Hakan Baba Aug 16 '17 at 00:15
0

You can use the following GNU sed command:

sed -r '/apple\.ApplePay/,+1s/(enabled =).*/\1 0;/' file

Explanation:

/apple\.ApplePay/,+1 is a range of lines starting with the pattern apple... until the next line.

s/(enabled =).*/\1 0;/' replaces everything after enabled = by itself plus 0;

hek2mgl
  • 152,036
  • 28
  • 249
  • 266