如何使用命令行工具替换 Unix/MacOS 上的多行正则表达式搜索模式选择的文件中的字符串
How to replace a string in a file selected by a multi-line regex search pattern on Unix/MacOS with command line tools
我正在努力寻找一个单行命令来用文本文件中的另一个包标识符替换一个包标识符。 (缩短的示例)文件如下所示:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!129 &1
PlayerSettings:
androidSupportedAspectRatio:
Android: 1
iPhone: 1
androidMaxAspectRatio: 2.1
applicationIdentifier:
Android: de.company.JenkinsBuildTest
Standalone: de.company.JenkinsBuildTest
iPhone: de.company.JenkinsBuildTest
buildNumber: {}
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 16
我之前的命令是:
sed -i -e '/applicationIdentifier/{n;n;n;s/iPhone:.*/iPhone: de.newcompany.JenkinsBuildTest/;}' ProjectSettings.asset
我想在"applicationIdentifier:"之后查找并替换"iPhone: "后面的字符串。我需要补充一点,applicationIdentifier 只存在一次,但 iPhone 不止一次。我的问题是 "iPhone: " 必须恰好在第三个换行符之后,如果 "Standalone: " 丢失,我的文件不会有任何变化。
如何使用正则表达式模式实现可靠的解决方案?
我用 sed、grep、awk 做了一切,但到目前为止没有成功。我查找包标识符的正则表达式模式应该是这样的:
(applicationIdentifier:[\s\S]+?iPhone: )([^\r\n]+)
但是如何将它与 MacOS (Mojave) 系统上的命令行工具结合起来呢?
我不知道 My problem is that "iPhone: " has to be exactly after this third line break, if "Standalone: " is missing, there won't be a change in my file.
是否意味着您只想修改 iPhone 如果 Standlone 存在,或者如果您的现有代码有问题而 iPhone 不存在当 Standalone 不存在但您希望它存在时进行修改。我假设是后者。
在每个 UNIX 机器上的任何 shell 中使用任何 awk:
$ awk '/applicationIdentifier:/{f=1} f && sub(/iPhone:.*/,"iPhone: de.newcompany.JenkinsBuildTest"){f=0} 1' file
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!129 &1
PlayerSettings:
androidSupportedAspectRatio:
Android: 1
iPhone: 1
androidMaxAspectRatio: 2.1
applicationIdentifier:
Android: de.company.JenkinsBuildTest
Standalone: de.company.JenkinsBuildTest
iPhone: de.newcompany.JenkinsBuildTest
buildNumber: {}
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 16
如果这不能完全满足您的要求,请编辑您的问题以提供更好的示例并阐明您的要求。
我正在努力寻找一个单行命令来用文本文件中的另一个包标识符替换一个包标识符。 (缩短的示例)文件如下所示:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!129 &1
PlayerSettings:
androidSupportedAspectRatio:
Android: 1
iPhone: 1
androidMaxAspectRatio: 2.1
applicationIdentifier:
Android: de.company.JenkinsBuildTest
Standalone: de.company.JenkinsBuildTest
iPhone: de.company.JenkinsBuildTest
buildNumber: {}
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 16
我之前的命令是:
sed -i -e '/applicationIdentifier/{n;n;n;s/iPhone:.*/iPhone: de.newcompany.JenkinsBuildTest/;}' ProjectSettings.asset
我想在"applicationIdentifier:"之后查找并替换"iPhone: "后面的字符串。我需要补充一点,applicationIdentifier 只存在一次,但 iPhone 不止一次。我的问题是 "iPhone: " 必须恰好在第三个换行符之后,如果 "Standalone: " 丢失,我的文件不会有任何变化。
如何使用正则表达式模式实现可靠的解决方案?
我用 sed、grep、awk 做了一切,但到目前为止没有成功。我查找包标识符的正则表达式模式应该是这样的:
(applicationIdentifier:[\s\S]+?iPhone: )([^\r\n]+)
但是如何将它与 MacOS (Mojave) 系统上的命令行工具结合起来呢?
我不知道 My problem is that "iPhone: " has to be exactly after this third line break, if "Standalone: " is missing, there won't be a change in my file.
是否意味着您只想修改 iPhone 如果 Standlone 存在,或者如果您的现有代码有问题而 iPhone 不存在当 Standalone 不存在但您希望它存在时进行修改。我假设是后者。
在每个 UNIX 机器上的任何 shell 中使用任何 awk:
$ awk '/applicationIdentifier:/{f=1} f && sub(/iPhone:.*/,"iPhone: de.newcompany.JenkinsBuildTest"){f=0} 1' file
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!129 &1
PlayerSettings:
androidSupportedAspectRatio:
Android: 1
iPhone: 1
androidMaxAspectRatio: 2.1
applicationIdentifier:
Android: de.company.JenkinsBuildTest
Standalone: de.company.JenkinsBuildTest
iPhone: de.newcompany.JenkinsBuildTest
buildNumber: {}
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 16
如果这不能完全满足您的要求,请编辑您的问题以提供更好的示例并阐明您的要求。