需要帮助将值替换为零(启用)

Need help to replace value to zero(enable)

我需要帮助来替换下面的作业:

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

希望输出包含

 com.apple.ApplePay = {
      enabled = 0;

我试过

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

输出

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

使用sed替换多行字符串是notoriously difficult

我可以建议使用 perl。这将打印出修改后的文件。

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

我没有考虑输出中的空格。根据文件的缩进,您可以在 enabled\ =\ 0

之前设置空格

输入文件在我的示例中称为 file1


输入

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

输出

$ 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;
};
};`
};
};

就地替换使用

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

您可以使用以下 GNU sed 命令:

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

解释:

/apple\.ApplePay/,+1 是从模式 apple... 开始到下一行的行范围。

s/(enabled =).*/ 0;/' 替换 enabled = 之后的所有内容加上 0;