bash - 如何使用 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
推荐阅读
- android - Android Studio - Android 模拟器以不同的方式呈现布局。模拟器运行程序时高度似乎较小
- ios - Apple TV 第 4 代中的 Powerpoint
- java - 根据长度乘以数字
- ios - 单击图像时如何添加操作?
- c# - 如何将 Localhost(网站)连接到 UWP 应用程序?
- sql - 评估多个列并返回与值条件匹配的列的 SQL 案例语句?
- python - 从 Python 获取参数的类中调用方法具有意外类型
- c# - 在 VFP 中完成 dll 的 WCF 中的 com 引用出错
- python - 使用 Astropy 打开 FITS 文件会导致 FileNotFoundError
- c - 返回值时赋值