如何使用 bash... 从脚本中删除已知行块(整个函数)?

How to remove a block of known lines (an entire function) from a script using bash...?

您将如何删除文件的已知部分,例如下面的部分:

func1() {
  # some code with conditions
  if [...]; then...
  # with some new lines too

  # some code again
}

来自脚本文件?

我这样试过:

grep -xvF "$(cat <<'HEREDOC'
func1() {
  # some code with conditions
  if [...]; then...
  # with some new lines too

  # some code again
}
HEREDOC
)" file > ouput

它有效,只是它删除了任何新行。我还没有找到解决这个问题的方法。 我尝试使用 awk,但它在方括号和 = 块中发现的符号上抛出语法错误。 我在 StackExchange 平台上找到的示例通常会尝试按模式删除几行。 有人有办法实现这一点,能够处理特殊字符并使输出文件与以前的文件相似吗?

使用 GNU sed:

sed -z -f <(sed 's|[.*^$/\[]|\&|g; s|$|\n|; 1s|^|s/|; $a //g' delete.txt | tr -d '\n') file

delete.txt 包含要从 file

中删除的连续部分