使用 bash 从文件中删除文本块

Removing blocks of text from a file with bash

我正在寻找一种理智的方法来挖空 Bash 中的函数,我不确定如何使用 sed 删除这么多数据(尽管我觉得 sed 或 awk 是这里最好的解决方案).

我有一个包含这些功能块的文件

....

function InstallationCheck(prefix) {
 if (system.compareVersions(system.version.ProductVersion, '10.10') < 0 || system.compareVersions(system.version.ProductVersion, '10.11') >= 0) {
  my.result.message = system.localizedStringWithFormat('ERROR_0', '10.10');
  my.result.type = 'Fatal';
 return false;
 }
 return true;
}

function VolumeCheck(prefix) {
 if (system.env.OS_INSTALL == 1) return true;
 var hasOS = system.files.fileExistsAtPath(my.target.mountpoint + "/System/Library/CoreServices/SystemVersion.plist");
 if (!hasOS || system.compareVersions(my.target.systemVersion.ProductVersion, '10.10') < 0 || system.compareVersions(my.target.systemVersion.ProductVersion, '10.11') >= 0) {
  my.result.message = system.localizedStringWithFormat('ERROR_0', '10.10');
  my.result.type = 'Fatal';
  return false;
 }
 if (compareBuildVersions(my.target.systemVersion.ProductBuildVersion, '14A388a') < 0) {
  my.result.message = system.localizedString('ERROR_2');
  my.result.type = 'Fatal';
  return false;
 }
 if (compareBuildVersions(my.target.systemVersion.ProductBuildVersion, '14B24') > 0) {
  my.result.message = system.localizedString('ERROR_2');
  my.result.type = 'Fatal';
  return false;
 }
 return true;
}

....

虽然我希望他们能像这样结束

function InstallationCheck(prefix) {
 return true;
}

function VolumeCheck(prefix) {
 return true;
}

实现此目标的最佳方法是什么?

编辑

所以大家知道了,这个文件里面还有其他函数应该保持不变。

使用 GNU sed:

sed '/^function \(InstallationCheck\|VolumeCheck\)(/,/^ return true;/{/^function\|^ return true;/p;d}' file

输出:

....

function InstallationCheck(prefix) {
 return true;
}

function VolumeCheck(prefix) {
 return true;
}

....

或具有相同的输出:

# first line (string or regex)
fl='^function \(InstallationCheck\|VolumeCheck\)('

# last line (string or regex)
ll='^ return true;'

sed "/${fl}/,/${ll}/{/${fl}/p;/${ll}/p;d}" file
$ cat tst.awk
inFunc && /^}/ { print "  return true;"; inFunc=0 }
!inFunc
[=10=] ~ "function[[:space:]]+(" fns ")[[:space:]]*\(.*" { inFunc=1 }

$ awk -v fns='InstallationCheck|VolumeCheck' -f tst.awk file
....

function InstallationCheck(prefix) {
  return true;
}

function VolumeCheck(prefix) {
  return true;
}

....