linux - 在最后一次出现模式后使用 sed 删除特定行
问题描述
我有一个看起来像的文件:
this name
this age
Remove these lines and space above.
Remove here too and space below
Keep everything below here.
我不想硬编码 2,因为包含“this”的行数可以改变。如何在最后一次出现字符串后删除 4 行。我正在尝试sed -e '/this: /{n;N;N;N;N;d}'
,但它在第一次出现字符串后被删除。
解决方案
请您尝试以下操作。
awk '
FNR==NR{
if($0~/this/){
line=FNR
}
next
}
FNR<=line || FNR>(line+4)
' Input_file Input_file
输出将如下所示,示例如下。
this: name
this: age
Keep everything below here.
推荐阅读
- mariadb - Galera 集群数据库同步,但不是表数据问题
- c++ - 用 || 折叠函数 操作员
- reporting-services - SSRS 条形图标签位置
- kotlin - 是否可以将从弱引用获得的实体实例传递给其他类实例并存储在那里?
- spring - 使用 spring 创建会话工厂对象时显示错误“
- java - 如何使用 Volley 发送文件格式
- angular - 在 Angular 中,使用 PrimeNG,如何设置禁用一些 p-selectButton 选项?
- elixir - Comeonin.Bcrypt checkpw 方法在我重置密码后返回 false - Elixir
- node.js - 在 Heroku 中每 30 秒运行一次任务
- java - Android MVP 我应该在哪里有一个 TextWatcher