首页 > 解决方案 > 在最后一次出现模式后使用 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}',但它在第一次出现字符串后被删除。

标签: linuxawksed

解决方案


请您尝试以下操作。

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.

推荐阅读