我需要帮助来替换下面的作业:
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 ;\'
输出
错误: - sed:1:" / SystemCapabilites = {\ ...":命令c期望\ 然后是文字
答案 0 :(得分:0)
使用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
答案 1 :(得分:0)
您可以使用以下GNU sed
命令:
sed -r '/apple\.ApplePay/,+1s/(enabled =).*/\1 0;/' file
说明:
/apple\.ApplePay/,+1
是从模式apple...
开始直到下一行的一系列行。
s/(enabled =).*/\1 0;/'
替换enabled =
之后的所有内容以及0;