首页 > 解决方案 > 如何使用 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 平台上找到的示例通常会尝试按模式删除几行。有人有办法实现这一点,能够处理特殊字符并保持输出文件与以前相似吗?

标签: bashawkgreptext-parsing

解决方案


使用 GNU sed

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

包含delete.txt要从file


推荐阅读